Skip to content

Commit

Permalink
add prevAll, nextAll traversing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lessmind authored and davidchambers committed Jun 6, 2013
1 parent 2925cb3 commit 877fd34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ $('.apple').next().hasClass('orange')
//=> true
```

#### .nextAll()
Gets all the following siblings of the first selected element.

```js
$('.apple').nextAll()
//=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
```

#### .prev()
Gets the previous sibling of the first selected element.

Expand All @@ -259,6 +267,14 @@ $('.orange').prev().hasClass('apple')
//=> true
```

#### .prevAll()
Gets all the preceding siblings of the first selected element.

```js
$('.pear').prevAll()
//=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]
```

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

Expand Down
12 changes: 12 additions & 0 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ var next = exports.next = function() {
return this.make([]);
};

var nextAll = exports.nextAll = function(selector) {
var elems = [], elem = this[0];
while ((elem = elem.next)) if (isTag(elem)) elems.push(elem);
return this.make(selector ? select(selector, elems) : elems);
};

var prev = exports.prev = function() {
var elem = this[0];
while ((elem = elem.prev)) if (isTag(elem)) return this.make(elem);
return this.make([]);
};

var prevAll = exports.prevAll = function(selector) {
var elems = [], elem = this[0];
while ((elem = elem.prev)) if (isTag(elem)) elems.push(elem);
return this.make(selector ? select(selector, elems) : elems);
};

var siblings = exports.siblings = function(selector) {
var elems = _.filter(
this.parent() ? this.parent().children() : this.siblingsAndMe(),
Expand Down
30 changes: 30 additions & 0 deletions test/api.traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ describe('$(...)', function() {

});

describe('.nextAll', function() {

it('() : should return all following siblings', function() {
var elems = $('.apple', fruits).nextAll();
expect(elems).to.have.length(2);
expect(elems[0].attribs['class']).to.equal('orange');
expect(elems[1].attribs['class']).to.equal('pear');
});

it('(no next) : should return empty for last child', function() {
expect($('.pear', fruits).nextAll()).to.have.length(0);
});

});

describe('.prev', function() {

it('() : should return previous element', function() {
Expand All @@ -92,6 +107,21 @@ describe('$(...)', function() {

});

describe('.prevAll', function() {

it('() : should return all preceding siblings', function() {
var elems = $('.pear', fruits).prevAll();
expect(elems).to.have.length(2);
expect(elems[0].attribs['class']).to.equal('orange');
expect(elems[1].attribs['class']).to.equal('apple');
});

it('(no prev) : should return empty for first child', function() {
expect($('.apple', fruits).prevAll()).to.have.length(0);
});

});

describe('.siblings', function() {

it('() : should get all the siblings', function() {
Expand Down

0 comments on commit 877fd34

Please sign in to comment.