Skip to content

Commit

Permalink
Merge pull request #47 from refractproject/smizell/more-finders
Browse files Browse the repository at this point in the history
More Find Methods
  • Loading branch information
smizell committed Jun 17, 2015
2 parents 76bdab4 + 71b64e6 commit 14de9c0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ var numbers = arrayElement.find(function(el) {
}).toValue(); // [1, 2, 3]
```

##### findByClass

The `findByClass` method traverses the entire descendent element tree and returns an `ArrayElement` of all elements that match the given element name.

##### findByElement

The `findByElement` method traverses the entire descendent element tree and returns an `ArrayElement` of all elements that match the given class.

##### children

The `children` method traverses direct descendants and returns an `ArrayElement` of all elements that match the condition function given.
Expand Down
12 changes: 12 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,18 @@ var Collection = BaseElement.extend({
return newArray;
},

findByElement: function(element) {
return this.find(function(item) {
return item.element === element;
});
},

findByClass: function(className) {
return this.find(function(item) {
return item.class.contains(className);
});
},

/*
* Search all direct descendents using a condition function.
*/
Expand Down
37 changes: 36 additions & 1 deletion test/primitives-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ describe('Minim Primitives', function() {
content: [
{
element: 'string',
meta: {
'class': ['test-class']
},
content: 'baz'
}, {
element: 'boolean',
Expand Down Expand Up @@ -591,14 +594,46 @@ describe('Minim Primitives', function() {

describe('#find', function() {
it('returns the correct number of items', function() {
expect(recursiveStrings.length).to.equal(4);
expect(recursiveStrings).to.have.lengthOf(4);
});

it('returns the correct values', function() {
expect(recursiveStrings.toValue()).to.deep.equal(['foobar', 'hello world', 'baz', 'bar']);
});
});

describe('#findByElement', function() {
var items;

before(function() {
items = doc.findByElement('number');
});

it('returns the correct number of items', function() {
expect(items).to.have.lengthOf(1);
});

it('returns the correct values', function() {
expect(items.toValue()).to.deep.equal([4]);
});
});

describe('#findByClass', function() {
var items;

before(function() {
items = doc.findByClass('test-class');
});

it('returns the correct number of items', function() {
expect(items).to.have.lengthOf(1);
});

it('returns the correct values', function() {
expect(items.toValue()).to.deep.equal(['baz']);
});
});

describe('#first', function() {
it('returns the first item', function() {
expect(doc.first()).to.deep.equal(doc.content[0]);
Expand Down

0 comments on commit 14de9c0

Please sign in to comment.