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

Commit

Permalink
Remove argument verification and add further utilities (#81)
Browse files Browse the repository at this point in the history
* Remove argument verification and add further utilities

* Some small updates to filterQuery

* Update filterQuery

* Add ACTIVATE_HOOKS to commons
  • Loading branch information
daffl authored Aug 13, 2018
1 parent 69eb808 commit b649b45
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 185 deletions.
41 changes: 0 additions & 41 deletions lib/arguments.js

This file was deleted.

3 changes: 1 addition & 2 deletions lib/commons.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const utils = require('./utils');
const hooks = require('./hooks');
const args = require('./arguments');
const filterQuery = require('./filter-query');

module.exports = Object.assign({}, utils, args, { hooks, filterQuery });
module.exports = Object.assign({}, utils, { hooks, filterQuery });
33 changes: 16 additions & 17 deletions lib/filter-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ function convertSort (sort) {
return sort;
}

const result = {};

Object.keys(sort).forEach(key => {
return Object.keys(sort).reduce((result, key) => {
result[key] = typeof sort[key] === 'object'
? sort[key] : parseInt(sort[key], 10);
});

return result;
return result;
}, {});
}

function cleanQuery (query, operators) {
if (Array.isArray(query)) {
return query.map((query) => cleanQuery(query, operators));
}

if (query && query.constructor === Object) {
if (_.isObject(query)) {
const result = {};
_.each(query, (query, key) => {
if (key[0] === '$' && operators.indexOf(key) === -1) return;
Expand All @@ -62,6 +56,8 @@ function assignFilters (object, query, filters, options) {
object[key] = converter(query[key], options);
});
}

return object;
}

const FILTERS = {
Expand All @@ -77,15 +73,18 @@ const OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or'];
// and returns them separately a `filters` and the rest of the query
// as `query`
module.exports = function filterQuery (query, options = {}) {
let { filters: additionalFilters = {}, operators: additionalOperators = [] } = options;
let result = {};
const {
filters: additionalFilters = {},
operators: additionalOperators = []
} = options;
const result = {};

result.filters = {};
assignFilters(result.filters, query, FILTERS, options);
assignFilters(result.filters, query, additionalFilters, options);
result.filters = assignFilters({}, query, FILTERS, options);
result.filters = assignFilters(result.filters, query, additionalFilters, options);

let operators = OPERATORS.concat(additionalOperators);
result.query = cleanQuery(query, operators);
result.query = cleanQuery(query, OPERATORS.concat(additionalOperators));

return result;
};

Object.assign(module.exports, { OPERATORS, FILTERS });
9 changes: 5 additions & 4 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { each, pick } = require('./utils')._;
const { _: { each, pick }, createSymbol } = require('./utils');

// To skip further hooks
const SKIP = exports.SKIP = typeof Symbol !== 'undefined'
? Symbol('__feathersSkipHooks')
: '__feathersSkipHooks';
const SKIP = createSymbol('__feathersSkipHooks');

exports.SKIP = SKIP;
exports.ACTIVATE_HOOKS = createSymbol('__feathersActivateHooks');

exports.createHookObject = function createHookObject (method, data = {}) {
const hook = {};
Expand Down
8 changes: 8 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const _ = exports._ = {
return (typeof item === 'object' && !Array.isArray(item) && item !== null);
},

isObjectOrArray (value) {
return typeof value === 'object' && value !== null;
},

extend (...args) {
return Object.assign(...args);
},
Expand Down Expand Up @@ -130,6 +134,10 @@ exports.makeUrl = function makeUrl (path, app = {}) {
return `${protocol}://${host}${port}/${exports.stripSlashes(path)}`;
};

exports.createSymbol = name => {
return typeof Symbol !== 'undefined' ? Symbol(name) : name;
};

// Sorting algorithm taken from NeDB (https://github.com/louischatriot/nedb)
// See https://github.com/louischatriot/nedb/blob/e3f0078499aa1005a59d0c2372e425ab789145c1/lib/model.js#L189

Expand Down
118 changes: 0 additions & 118 deletions test/arguments.test.js

This file was deleted.

7 changes: 6 additions & 1 deletion test/filter-query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ const expect = chai.expect;
describe('.filterQuery', function () {
describe('$sort', function () {
it('returns $sort when present in query', function () {
const { filters, query } = filter({ $sort: { name: 1 } });
const originalQuery = { $sort: { name: 1 } };
const { filters, query } = filter(originalQuery);

expect(filters.$sort.name).to.equal(1);
expect(query).to.deep.equal({});
expect(originalQuery).to.deep.equal({
$sort: { name: 1 }
}, 'does not modify original query');
});

it('returns $sort when present in query as an object', function () {
Expand Down
1 change: 0 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('module', () => {
expect(typeof commons.stripSlashes).to.equal('function');
expect(typeof commons.sorter).to.equal('function');
expect(typeof commons.select).to.equal('function');
expect(typeof commons.validateArguments).to.equal('function');
expect(typeof commons.hooks).to.equal('object');
expect(typeof commons._).to.equal('object');
});
Expand Down
13 changes: 12 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {
stripSlashes,
select,
isPromise,
makeUrl
makeUrl,
createSymbol
} = require('../lib/utils');

describe('@feathersjs/commons utils', () => {
Expand All @@ -29,13 +30,23 @@ describe('@feathersjs/commons utils', () => {
expect(isPromise(null)).to.equal(false);
});

it('createSymbol', () => {
expect(typeof createSymbol('a test')).to.equal('symbol');
});

describe('_', () => {
it('isObject', () => {
expect(_.isObject({})).to.equal(true);
expect(_.isObject([])).to.equal(false);
expect(_.isObject(null)).to.equal(false);
});

it('isObjectOrArray', () => {
expect(_.isObjectOrArray({})).to.equal(true);
expect(_.isObjectOrArray([])).to.equal(true);
expect(_.isObjectOrArray(null)).to.equal(false);
});

it('each', () => {
_.each({ hi: 'there' }, (value, key) => {
expect(key).to.equal('hi');
Expand Down

0 comments on commit b649b45

Please sign in to comment.