Skip to content
This repository has been archived by the owner on Jan 12, 2018. It is now read-only.

Commit

Permalink
Make sure to not modify the original query object (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Jul 9, 2016
1 parent 93efb99 commit 69d87f9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 63 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 4 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) };
}
89 changes: 41 additions & 48 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

Expand All @@ -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);
});
});
});
Expand All @@ -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);
});
});

Expand All @@ -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;
});
});

Expand All @@ -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;
});
});
});

0 comments on commit 69d87f9

Please sign in to comment.