diff --git a/package.json b/package.json index 1225ec2..c9899e2 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,9 @@ "directories": { "lib": "lib" }, - "dependencies": {}, + "dependencies": { + "lodash.omit": "^4.3.0" + }, "devDependencies": { "babel-cli": "^6.4.5", "babel-core": "^6.4.5", diff --git a/src/index.js b/src/index.js index c42d723..752ae8b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,6 @@ -/** - * - * Sets up the query properly if $limit, $skip, $sort, or $select is passed in params. - * Those same parameters are then removed from _conditions so that we aren't searching - * for data with a $limit parameter. - */ +const omit = require('lodash.omit'); +const PROPERTIES = ['$sort', '$limit', '$skip', '$select', '$populate']; + function parse(number) { if(typeof number !== 'undefined') { return parseInt(number, 10); @@ -27,12 +24,5 @@ export default function(query, paginate) { $populate: query.$populate }; - // Remove the params from the query's conditions. - delete query.$sort; - delete query.$limit; - delete query.$skip; - delete query.$select; - delete query.$populate; - - return filters; + return { filters, query: omit(query, ...PROPERTIES) }; } diff --git a/test/index.test.js b/test/index.test.js index 8993dce..b5b237b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -16,19 +16,15 @@ describe('Feathers Query Filters', function() { }); it('returns $sort when present in query', function() { - let result = filter(this.query); - expect(result.$sort).to.equal(1); - }); - - it('removes $sort from query when present', function() { - filter(this.query); - expect(this.query).to.deep.equal({}); + const { filters, query } = filter(this.query); + expect(filters.$sort).to.equal(1); + expect(query).to.deep.equal({}); }); it('returns undefined when not present in query', function() { - let query = { $foo: 1 }; - let result = filter(query); - expect(result.$sort).to.be.undefined; + const query = { $foo: 1 }; + const { filters } = filter(query); + expect(filters.$sort).to.be.undefined; }); }); @@ -38,35 +34,35 @@ describe('Feathers Query Filters', function() { }); it('returns $limit when present in query', function() { - let result = filter(this.query); - expect(result.$limit).to.equal(1); + const { filters, query } = filter(this.query); + expect(filters.$limit).to.equal(1); + expect(query).to.deep.equal({}); }); - it('removes $limit from query when present', function() { - filter(this.query); - expect(this.query).to.deep.equal({}); + it('returns undefined when not present in query', function() { + const query = { $foo: 1 }; + const { filters } = filter(query); + expect(filters.$limit).to.be.undefined; }); - it('returns undefined when not present in query', function() { - let query = { $foo: 1 }; - let result = filter(query); - expect(result.$limit).to.be.undefined; + it('removes $limit from query when present', function() { + expect(filter(this.query).query).to.deep.equal({}); }); it('parses $limit strings into integers (#4)', function() { - let result = filter({ $limit: '2' }); - expect(result.$limit).to.equal(2); + const { filters } = filter({ $limit: '2' }); + expect(filters.$limit).to.equal(2); }); describe('pagination', function() { it('limits with default pagination', function() { - let result = filter({}, { default: 10 }); - expect(result.$limit).to.equal(10); + const { filters } = filter({}, { default: 10 }); + expect(filters.$limit).to.equal(10); }); it('limits with max pagination', function() { - let result = filter({ $limit: 20 }, { default: 5, max: 10 }); - expect(result.$limit).to.equal(10); + const { filters } = filter({ $limit: 20 }, { default: 5, max: 10 }); + expect(filters.$limit).to.equal(10); }); }); }); @@ -77,24 +73,23 @@ describe('Feathers Query Filters', function() { }); it('returns $skip when present in query', function() { - let result = filter(this.query); - expect(result.$skip).to.equal(1); + const { filters } = filter(this.query); + expect(filters.$skip).to.equal(1); }); it('removes $skip from query when present', function() { - filter(this.query); - expect(this.query).to.deep.equal({}); + expect(filter(this.query).query).to.deep.equal({}); }); it('returns undefined when not present in query', function() { - let query = { $foo: 1 }; - let result = filter(query); - expect(result.$skip).to.be.undefined; + const query = { $foo: 1 }; + const { filters } = filter(query); + expect(filters.$skip).to.be.undefined; }); it('parses $skip strings into integers (#4)', function() { - let result = filter({ $skip: '33' }); - expect(result.$skip).to.equal(33); + const { filters } = filter({ $skip: '33' }); + expect(filters.$skip).to.equal(33); }); }); @@ -104,19 +99,18 @@ describe('Feathers Query Filters', function() { }); it('returns $select when present in query', function() { - let result = filter(this.query); - expect(result.$select).to.equal(1); + const { filters } = filter(this.query); + expect(filters.$select).to.equal(1); }); it('removes $select from query when present', function() { - filter(this.query); - expect(this.query).to.deep.equal({}); + expect(filter(this.query).query).to.deep.equal({}); }); it('returns undefined when not present in query', function() { - let query = { $foo: 1 }; - let result = filter(query); - expect(result.$select).to.be.undefined; + const query = { $foo: 1 }; + const { filters } = filter(query); + expect(filters.$select).to.be.undefined; }); }); @@ -126,19 +120,18 @@ describe('Feathers Query Filters', function() { }); it('returns $populate when present in query', function() { - var result = filter(this.query); - expect(result.$populate).to.equal(1); + const { filters } = filter(this.query); + expect(filters.$populate).to.equal(1); }); it('removes $populate from query when present', function() { - filter(this.query); - expect(this.query).to.deep.equal({}); + expect(filter(this.query).query).to.deep.equal({}); }); it('returns undefined when not present in query', function() { - var query = { $foo: 1 }; - var result = filter(query); - expect(result.$populate).to.be.undefined; + const query = { $foo: 1 }; + const { filters } = filter(query); + expect(filters.$populate).to.be.undefined; }); }); });