@feathersjs/commons
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -6,2 +6,24 @@ # Change Log | ||
<a name="4.0.0"></a> | ||
# [4.0.0](https://github.com/feathersjs/feathers/compare/@feathersjs/commons@3.0.1...@feathersjs/commons@4.0.0) (2018-12-16) | ||
### Bug Fixes | ||
* Make Mocha a proper devDependency for every repository ([#1053](https://github.com/feathersjs/feathers/issues/1053)) ([9974803](https://github.com/feathersjs/feathers/commit/9974803)) | ||
### Features | ||
* Common database adapter utilities and test suite ([#1130](https://github.com/feathersjs/feathers/issues/1130)) ([17b3dc8](https://github.com/feathersjs/feathers/commit/17b3dc8)) | ||
### BREAKING CHANGES | ||
* Move database adapter utilities from @feathersjs/commons into its own module | ||
<a name="3.0.1"></a> | ||
@@ -8,0 +30,0 @@ ## [3.0.1](https://github.com/feathersjs/feathers/compare/@feathersjs/commons@3.0.0...@feathersjs/commons@3.0.1) (2018-09-17) |
@@ -10,3 +10,3 @@ const assert = require('assert'); | ||
return getService().find().then(todos => | ||
assert.deepEqual(todos, [{ | ||
assert.deepEqual(todos, [{ // eslint-disable-line | ||
text: 'some todo', | ||
@@ -23,7 +23,7 @@ complete: false, | ||
other: ['one', 'two'], | ||
nested: {a: {b: 'object'}} | ||
nested: { a: { b: 'object' } } | ||
}; | ||
return getService().get(0, { query }) | ||
.then(todo => assert.deepEqual(todo, { | ||
.then(todo => assert.deepEqual(todo, { // eslint-disable-line | ||
id: 0, | ||
@@ -38,3 +38,3 @@ text: 'some todo', | ||
getService().once('created', function (data) { | ||
assert.equal(data.text, 'created todo'); | ||
assert.strictEqual(data.text, 'created todo'); | ||
assert.ok(data.complete); | ||
@@ -44,3 +44,3 @@ done(); | ||
getService().create({text: 'created todo', complete: true}); | ||
getService().create({ text: 'created todo', complete: true }); | ||
}); | ||
@@ -50,3 +50,3 @@ | ||
getService().once('updated', data => { | ||
assert.equal(data.text, 'updated todo'); | ||
assert.strictEqual(data.text, 'updated todo'); | ||
assert.ok(data.complete); | ||
@@ -56,3 +56,3 @@ done(); | ||
getService().create({text: 'todo to update', complete: false}) | ||
getService().create({ text: 'todo to update', complete: false }) | ||
.then(todo => getService().update(todo.id, { | ||
@@ -66,3 +66,3 @@ text: 'updated todo', | ||
getService().once('patched', data => { | ||
assert.equal(data.text, 'todo to patch'); | ||
assert.strictEqual(data.text, 'todo to patch'); | ||
assert.ok(data.complete); | ||
@@ -72,4 +72,4 @@ done(); | ||
getService().create({text: 'todo to patch', complete: false}) | ||
.then(todo => getService().patch(todo.id, {complete: true})); | ||
getService().create({ text: 'todo to patch', complete: false }) | ||
.then(todo => getService().patch(todo.id, { complete: true })); | ||
}); | ||
@@ -79,8 +79,8 @@ | ||
getService().once('removed', data => { | ||
assert.equal(data.text, 'todo to remove'); | ||
assert.equal(data.complete, false); | ||
assert.strictEqual(data.text, 'todo to remove'); | ||
assert.strictEqual(data.complete, false); | ||
done(); | ||
}); | ||
getService().create({text: 'todo to remove', complete: false}) | ||
getService().create({ text: 'todo to remove', complete: false }) | ||
.then(todo => getService().remove(todo.id)).catch(done); | ||
@@ -90,5 +90,5 @@ }); | ||
it('.get with error', () => { | ||
let query = {error: true}; | ||
let query = { error: true }; | ||
return getService().get(0, {query}).catch(error => | ||
return getService().get(0, { query }).catch(error => | ||
assert.ok(error && error.message) | ||
@@ -95,0 +95,0 @@ ); |
@@ -77,8 +77,8 @@ const assert = require('assert'); | ||
find (data) { | ||
assert.deepEqual(findAllData, data, 'Data as expected'); | ||
assert.deepStrictEqual(findAllData, data, 'Data as expected'); | ||
}, | ||
get (id, data) { | ||
assert.equal(data.id, id, 'Got id in data'); | ||
assert.equal(data.description, `You have to do ${id}!`, 'Got description'); | ||
assert.strictEqual(data.id, id, 'Got id in data'); | ||
assert.strictEqual(data.description, `You have to do ${id}!`, 'Got description'); | ||
}, | ||
@@ -91,3 +91,3 @@ | ||
}); | ||
assert.deepEqual(expected, current, 'Data ran through .create as expected'); | ||
assert.deepStrictEqual(expected, current, 'Data ran through .create as expected'); | ||
}, | ||
@@ -100,3 +100,3 @@ | ||
}); | ||
assert.deepEqual(expected, current, 'Data ran through .update as expected'); | ||
assert.deepStrictEqual(expected, current, 'Data ran through .update as expected'); | ||
}, | ||
@@ -109,8 +109,8 @@ | ||
}); | ||
assert.deepEqual(expected, current, 'Data ran through .patch as expected'); | ||
assert.deepStrictEqual(expected, current, 'Data ran through .patch as expected'); | ||
}, | ||
remove (id, data) { | ||
assert.deepEqual({ id }, data, '.remove called'); | ||
assert.deepStrictEqual({ id }, data, '.remove called'); | ||
} | ||
}; |
120
lib/utils.js
@@ -91,29 +91,2 @@ // Removes all leading and trailing slashes from a path | ||
// Return a function that filters a result object or array | ||
// and picks only the fields passed as `params.query.$select` | ||
// and additional `otherFields` | ||
exports.select = function select (params, ...otherFields) { | ||
const fields = params && params.query && params.query.$select; | ||
if (Array.isArray(fields) && otherFields.length) { | ||
fields.push(...otherFields); | ||
} | ||
const convert = result => { | ||
if (!Array.isArray(fields)) { | ||
return result; | ||
} | ||
return _.pick(result, ...fields); | ||
}; | ||
return result => { | ||
if (Array.isArray(result)) { | ||
return result.map(convert); | ||
} | ||
return convert(result); | ||
}; | ||
}; | ||
// Duck-checks if an object looks like a promise | ||
@@ -141,94 +114,1 @@ exports.isPromise = function isPromise (result) { | ||
}; | ||
// Sorting algorithm taken from NeDB (https://github.com/louischatriot/nedb) | ||
// See https://github.com/louischatriot/nedb/blob/e3f0078499aa1005a59d0c2372e425ab789145c1/lib/model.js#L189 | ||
exports.compareNSB = function (a, b) { | ||
if (a < b) { return -1; } | ||
if (a > b) { return 1; } | ||
return 0; | ||
}; | ||
exports.compareArrays = function (a, b) { | ||
var i, comp; | ||
for (i = 0; i < Math.min(a.length, b.length); i += 1) { | ||
comp = exports.compare(a[i], b[i]); | ||
if (comp !== 0) { return comp; } | ||
} | ||
// Common section was identical, longest one wins | ||
return exports.compareNSB(a.length, b.length); | ||
}; | ||
exports.compare = function (a, b, compareStrings = exports.compareNSB) { | ||
const { compareNSB, compare, compareArrays } = exports; | ||
// undefined | ||
if (a === undefined) { return b === undefined ? 0 : -1; } | ||
if (b === undefined) { return a === undefined ? 0 : 1; } | ||
// null | ||
if (a === null) { return b === null ? 0 : -1; } | ||
if (b === null) { return a === null ? 0 : 1; } | ||
// Numbers | ||
if (typeof a === 'number') { return typeof b === 'number' ? compareNSB(a, b) : -1; } | ||
if (typeof b === 'number') { return typeof a === 'number' ? compareNSB(a, b) : 1; } | ||
// Strings | ||
if (typeof a === 'string') { return typeof b === 'string' ? compareStrings(a, b) : -1; } | ||
if (typeof b === 'string') { return typeof a === 'string' ? compareStrings(a, b) : 1; } | ||
// Booleans | ||
if (typeof a === 'boolean') { return typeof b === 'boolean' ? compareNSB(a, b) : -1; } | ||
if (typeof b === 'boolean') { return typeof a === 'boolean' ? compareNSB(a, b) : 1; } | ||
// Dates | ||
if (a instanceof Date) { return b instanceof Date ? compareNSB(a.getTime(), b.getTime()) : -1; } | ||
if (b instanceof Date) { return a instanceof Date ? compareNSB(a.getTime(), b.getTime()) : 1; } | ||
// Arrays (first element is most significant and so on) | ||
if (Array.isArray(a)) { return Array.isArray(b) ? compareArrays(a, b) : -1; } | ||
if (Array.isArray(b)) { return Array.isArray(a) ? compareArrays(a, b) : 1; } | ||
// Objects | ||
const aKeys = Object.keys(a).sort(); | ||
const bKeys = Object.keys(b).sort(); | ||
let comp = 0; | ||
for (let i = 0; i < Math.min(aKeys.length, bKeys.length); i += 1) { | ||
comp = compare(a[aKeys[i]], b[bKeys[i]]); | ||
if (comp !== 0) { return comp; } | ||
} | ||
return compareNSB(aKeys.length, bKeys.length); | ||
}; | ||
// An in-memory sorting function according to the | ||
// $sort special query parameter | ||
exports.sorter = function ($sort) { | ||
const criteria = Object.keys($sort).map(key => { | ||
const direction = $sort[key]; | ||
return { key, direction }; | ||
}); | ||
return function (a, b) { | ||
let compare; | ||
for (let i = 0; i < criteria.length; i++) { | ||
const criterion = criteria[i]; | ||
compare = criterion.direction * exports.compare(a[criterion.key], b[criterion.key]); | ||
if (compare !== 0) { | ||
return compare; | ||
} | ||
} | ||
return 0; | ||
}; | ||
}; |
{ | ||
"name": "@feathersjs/commons", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Shared Feathers utility functions", | ||
@@ -26,5 +26,5 @@ "homepage": "https://feathersjs.com", | ||
}, | ||
"main": "lib/commons.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "../../node_modules/.bin/mocha --opts ../../mocha.opts" | ||
"test": "mocha --opts ../../mocha.opts" | ||
}, | ||
@@ -38,5 +38,6 @@ "directories": { | ||
"devDependencies": { | ||
"chai": "^4.1.2" | ||
"chai": "^4.1.2", | ||
"mocha": "^5.2.0" | ||
}, | ||
"gitHead": "da6e2732874c9c7834cb4a53526787218d62e4a3" | ||
"gitHead": "3bbba49daf1e62f358b426002ec9684f4bcaac89" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36633
2
70746
9
426