Skip to content

Commit

Permalink
Add slice method
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Feb 22, 2013
1 parent dab9e9e commit a0a8c8c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ $('.orange').prev().hasClass('apple')
//=> true
```

#### .slice( start, [end] )
Gets the elements matching the specified range

```js
$('li').slice(1).eq(0).text()
//=> 'Orange'

$('li').slice(1, 2).length
//=> 1
```

#### .siblings()
Gets the first selected element's siblings, excluding itself.

Expand Down
4 changes: 4 additions & 0 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@ var eq = exports.eq = function(i) {
if (i < 0) i = this.length + i;
return this[i] ? this.make(this[i]) : this.make([]);
};

var slice = exports.slice = function() {
return this.make([].slice.apply(this, arguments));
};
28 changes: 28 additions & 0 deletions test/api.traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,32 @@ describe('$(...)', function() {

});

describe('.slice', function() {

function getText(el) {
if(!el.length) return '';
return el[0].children[0].data;
}

it('(start) : should return all elements after the given index', function() {
var sliced = $('li', fruits).slice(1);
expect(sliced).to.have.length(2);
expect(getText(sliced.eq(0))).to.equal('Orange');
expect(getText(sliced.eq(1))).to.equal('Pear');
});

it('(start, end) : should return all elements matching the given range', function() {
var sliced = $('li', fruits).slice(1, 2);
expect(sliced).to.have.length(1);
expect(getText(sliced.eq(0))).to.equal('Orange');
});

it('(-start) : should return element matching the offset from the end', function() {
var sliced = $('li', fruits).slice(-1);
expect(sliced).to.have.length(1);
expect(getText(sliced.eq(0))).to.equal('Pear');
});

});

});

0 comments on commit a0a8c8c

Please sign in to comment.