-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Avocarrot/refactor/improvements
Refactor/improvements
- Loading branch information
Showing
8 changed files
with
170 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
module.exports = require('./find'); | ||
const filterArr = require('./filterArr'); | ||
|
||
module.exports = (arr) => { | ||
if (!Array.isArray(arr)) { | ||
throw new Error('`arr` must be an array of objects'); | ||
} | ||
return { | ||
every: filters => filterArr(arr, filters, 'every'), | ||
some: filters => filterArr(arr, filters, 'some'), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const should = require('chai').Should(); | ||
const query = require('../../lib'); | ||
|
||
describe('Module Tests', () => { | ||
|
||
const people = [ | ||
{ | ||
name: 'George', | ||
lastName: 'Eracleous', | ||
age: 28 | ||
}, | ||
{ | ||
name: 'Erica', | ||
lastName: 'Archer', | ||
age: 20 | ||
}, | ||
{ | ||
name: 'George', | ||
lastName: 'Makkoulis', | ||
age: 57 | ||
}, | ||
{ | ||
name: 'Bob', | ||
lastName: 'Smith', | ||
age: 32 | ||
} | ||
]; | ||
|
||
it('query(arr) should return a query object', () => { | ||
query([]).should.have.property('every'); | ||
query([]).should.have.property('some'); | ||
}); | ||
|
||
it('throw an error if `arr` is not an array', () => { | ||
(() => { | ||
query(); | ||
}).should.throw('`arr` must be an array of objects'); | ||
|
||
(() => { | ||
query('this is a string'); | ||
}).should.throw('`arr` must be an array of objects'); | ||
|
||
(() => { | ||
query(1234); | ||
}).should.throw('`arr` must be an array of objects'); | ||
}); | ||
|
||
it('query(arr).every(filters) should return all results that satisfy all filters', () => { | ||
const filters = [ | ||
{ | ||
field: 'name', | ||
value: 'George', | ||
operator: 'equals' | ||
}, | ||
{ | ||
field: 'age', | ||
value: 30, | ||
operator: 'lt' | ||
} | ||
]; | ||
const expected = [people[0]]; | ||
query(people).every(filters).should.eql(expected); | ||
}); | ||
|
||
it('query(arr).some(filters) should return all results that satisfy at least one of the filters', () => { | ||
const filters = [ | ||
{ | ||
field: 'name', | ||
value: 'George', | ||
operator: 'equals' | ||
}, | ||
{ | ||
field: 'age', | ||
value: 30, | ||
operator: 'lt' | ||
} | ||
]; | ||
const expected = [people[0], people[1], people[2]]; | ||
query(people).some(filters).should.eql(expected); | ||
}); | ||
|
||
}); |