diff --git a/index.js b/index.js index d879c2f..8f7b896 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,16 @@ module.exports = class Query { return this.data; }; + filter (func) { + this.data = this.data.filter(func); + return this; + }; + + filterBy (key, func) { + this.data = this.data.filter(item => func(item[key])); + return this; + }; + search (key, term, score = 0) { switch (typeof term) { case 'boolean': diff --git a/index.test.js b/index.test.js index 9b3921f..4351cd0 100644 --- a/index.test.js +++ b/index.test.js @@ -66,6 +66,26 @@ test('should sort by string name', () => { expect(query[0].name).toBe('Dudley Conner'); }); +test('should filter', () => { + let isAgeOver33 = a => a.age > 33; + + let query = new Query(TestData) + .filter(isAgeOver33) + .results; + + expect(query[0].name).toBe('Howard Buckley'); +}); + +test('should filter by key', () => { + let isNumGT33 = num => num > 33; + + let query = new Query(TestData) + .filterBy('age', isNumGT33) + .results; + + expect(query[0].name).toBe('Howard Buckley'); +}); + test('should chain everything together', () => { let query = new Query(TestData) .search('isActive', true)