Skip to content

Commit

Permalink
feat(Collection): Put an Element on find method to have a new Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaperret committed Aug 24, 2017
1 parent a04e917 commit c344ea7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ Execute all handlers to each elements



#### Collection.find(selector)
#### Collection.find(selectorOrElement)

Returns a collection of matched elements either found in the DOM based on passed argument

Expand All @@ -677,7 +677,7 @@ Returns a collection of matched elements either found in the DOM based on passed

| Name | Type | Description | |
| ---- | ---- | ----------- | -------- |
| selector | `string` | Accepts a string containing a CSS selector which is then used to match a set of elements |   |
| selectorOrElement | `string` `Element` | Accepts a string containing a CSS selector which is then used to match a set of elements, or an Element |   |



Expand Down
36 changes: 20 additions & 16 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,25 @@ export default class Collection {
/**
* Returns a collection of matched elements either found in the DOM based on passed argument
* @see {@link selectors/find}
* @param {string} selector Accepts a string containing a CSS selector which is then used to match a set of elements
* @return {Collection} Collection containing elements
*/
find (selector) {
return this.elements
.map(element => find(selector, element))
.filter(element => element.length || element instanceof Element === true)
.reduce((accumulator, element) => accumulator.concat(element), [])
.reduce((accumulator, element) => {
if (~accumulator.elements.indexOf(element)) {
return accumulator
} else {
accumulator.elements.push(element)
return accumulator
}
}, new Collection())
* @param {(string|Element)} selectorOrElement Accepts a string containing a CSS selector which is then used to match a set of elements, or an Element
* @return {Collection} Collection containing elements
*/
find (selectorOrElement) {
if (selectorOrElement instanceof Element === true) {
return new Collection(selectorOrElement)
} else {
return this.elements
.map(element => find(selectorOrElement, element))
.filter(element => element.length || element instanceof Element === true)
.reduce((accumulator, element) => accumulator.concat(element), [])
.reduce((accumulator, element) => {
if (~accumulator.elements.indexOf(element)) {
return accumulator
} else {
accumulator.elements.push(element)
return accumulator
}
}, new Collection())
}
}
}
6 changes: 6 additions & 0 deletions test/specs/collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ describe('Collection', () => {
expect(newCollection).toBeInstanceOf(Collection)
expect(newCollection.elements.length).toBe(1)
})
test('Existing element', () => {
const paragraph = document.createElement('p')
const newCollection = collection.find(paragraph)
expect(newCollection).toBeInstanceOf(Collection)
expect(newCollection.elements.length).toBe(1)
})
test('Unexisting', () => {
const newCollection = collection.find('#testId')
expect(newCollection.elements.length).toBe(0)
Expand Down

0 comments on commit c344ea7

Please sign in to comment.