@feathersjs/adapter-commons
Advanced tools
Comparing version 5.0.25 to 5.0.26
@@ -6,2 +6,8 @@ # Change Log | ||
## [5.0.26](https://github.com/feathersjs/feathers/compare/v5.0.25...v5.0.26) (2024-06-09) | ||
### Bug Fixes | ||
- **adapter-commons:** Faster sorter ([#3495](https://github.com/feathersjs/feathers/issues/3495)) ([22243e4](https://github.com/feathersjs/feathers/commit/22243e4d92edc1a7343b4cf42be6dfb22e8b86d5)) | ||
## [5.0.25](https://github.com/feathersjs/feathers/compare/v5.0.24...v5.0.25) (2024-05-03) | ||
@@ -8,0 +14,0 @@ |
@@ -1,6 +0,6 @@ | ||
export declare function compareNSB(a: any, b: any): 1 | -1 | 0; | ||
export declare function compareArrays(a: any[], b: any[]): 1 | -1 | 0; | ||
export declare function compare(a: any, b: any, compareStrings?: any): 0 | 1 | -1; | ||
export declare function compareNSB(a: number | string | boolean, b: number | string | boolean): 0 | 1 | -1; | ||
export declare function compareArrays(a: any[], b: any[]): 0 | 1 | -1; | ||
export declare function compare(a: any, b: any, compareStrings?: (a: any, b: any) => 0 | 1 | -1): 0 | 1 | -1; | ||
export declare function sorter($sort: { | ||
[key: string]: -1 | 1; | ||
}): (a: any, b: any) => number; |
@@ -7,9 +7,6 @@ "use strict"; | ||
function compareNSB(a, b) { | ||
if (a < b) { | ||
return -1; | ||
if (a === b) { | ||
return 0; | ||
} | ||
if (a > b) { | ||
return 1; | ||
} | ||
return 0; | ||
return a < b ? -1 : 1; | ||
} | ||
@@ -32,35 +29,31 @@ exports.compareNSB = compareNSB; | ||
} | ||
// undefined | ||
if (a === undefined) { | ||
// null or undefined | ||
if (a == null) { | ||
return -1; | ||
} | ||
if (b === undefined) { | ||
if (b == null) { | ||
return 1; | ||
} | ||
// null | ||
if (a === null) { | ||
return -1; | ||
} | ||
if (b === null) { | ||
return 1; | ||
} | ||
// detect typeof once | ||
const typeofA = typeof a; | ||
const typeofB = typeof b; | ||
// Numbers | ||
if (typeof a === 'number') { | ||
return typeof b === 'number' ? compareNSB(a, b) : -1; | ||
if (typeofA === 'number') { | ||
return typeofB === 'number' ? compareNSB(a, b) : -1; | ||
} | ||
if (typeof b === 'number') { | ||
if (typeofB === 'number') { | ||
return 1; | ||
} | ||
// Strings | ||
if (typeof a === 'string') { | ||
return typeof b === 'string' ? compareStrings(a, b) : -1; | ||
if (typeofA === 'string') { | ||
return typeofB === 'string' ? compareStrings(a, b) : -1; | ||
} | ||
if (typeof b === 'string') { | ||
if (typeofB === 'string') { | ||
return 1; | ||
} | ||
// Booleans | ||
if (typeof a === 'boolean') { | ||
return typeof b === 'boolean' ? compareNSB(a, b) : -1; | ||
if (typeofA === 'boolean') { | ||
return typeofB === 'boolean' ? compareNSB(a, b) : -1; | ||
} | ||
if (typeof b === 'boolean') { | ||
if (typeofB === 'boolean') { | ||
return 1; | ||
@@ -94,13 +87,14 @@ } | ||
exports.compare = compare; | ||
// lodash-y get - probably want to use lodash get instead | ||
const get = (value, path) => path.reduce((value, key) => value[key], value); | ||
// An in-memory sorting function according to the | ||
// $sort special query parameter | ||
function sorter($sort) { | ||
const get = (value, path) => path.reduce((value, key) => value[key], value); | ||
const compares = Object.keys($sort).map((key) => { | ||
const direction = $sort[key]; | ||
const path = key.split('.'); | ||
if (path.length === 1) { | ||
if (!key.includes('.')) { | ||
return (a, b) => direction * compare(a[key], b[key]); | ||
} | ||
else { | ||
const path = key.split('.'); | ||
return (a, b) => direction * compare(get(a, path), get(b, path)); | ||
@@ -111,5 +105,5 @@ } | ||
for (const compare of compares) { | ||
const comparasion = compare(a, b); | ||
if (comparasion !== 0) { | ||
return comparasion; | ||
const comparison = compare(a, b); | ||
if (comparison !== 0) { | ||
return comparison; | ||
} | ||
@@ -116,0 +110,0 @@ } |
{ | ||
"name": "@feathersjs/adapter-commons", | ||
"version": "5.0.25", | ||
"version": "5.0.26", | ||
"description": "Shared database adapter utility functions", | ||
@@ -53,5 +53,5 @@ "homepage": "https://feathersjs.com", | ||
"dependencies": { | ||
"@feathersjs/commons": "^5.0.25", | ||
"@feathersjs/errors": "^5.0.25", | ||
"@feathersjs/feathers": "^5.0.25" | ||
"@feathersjs/commons": "^5.0.26", | ||
"@feathersjs/errors": "^5.0.26", | ||
"@feathersjs/feathers": "^5.0.26" | ||
}, | ||
@@ -68,3 +68,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "01bc3339efd27df1b3e8d4f6a82152faf6c5ced3" | ||
"gitHead": "3fe6843c1a478a2887ca91b6f79eda3b61d35ef3" | ||
} |
// Sorting algorithm taken from NeDB (https://github.com/louischatriot/nedb) | ||
// See https://github.com/louischatriot/nedb/blob/e3f0078499aa1005a59d0c2372e425ab789145c1/lib/model.js#L189 | ||
export function compareNSB(a: any, b: any) { | ||
if (a < b) { | ||
return -1 | ||
export function compareNSB(a: number | string | boolean, b: number | string | boolean): 0 | 1 | -1 { | ||
if (a === b) { | ||
return 0 | ||
} | ||
if (a > b) { | ||
return 1 | ||
} | ||
return 0 | ||
return a < b ? -1 : 1 | ||
} | ||
export function compareArrays(a: any[], b: any[]) { | ||
export function compareArrays(a: any[], b: any[]): 0 | 1 | -1 { | ||
for (let i = 0, l = Math.min(a.length, b.length); i < l; i++) { | ||
@@ -27,3 +25,7 @@ const comparison = compare(a[i], b[i]) | ||
export function compare(a: any, b: any, compareStrings: any = compareNSB): 0 | 1 | -1 { | ||
export function compare( | ||
a: any, | ||
b: any, | ||
compareStrings: (a: any, b: any) => 0 | 1 | -1 = compareNSB | ||
): 0 | 1 | -1 { | ||
if (a === b) { | ||
@@ -33,23 +35,19 @@ return 0 | ||
// undefined | ||
if (a === undefined) { | ||
// null or undefined | ||
if (a == null) { | ||
return -1 | ||
} | ||
if (b === undefined) { | ||
if (b == null) { | ||
return 1 | ||
} | ||
// null | ||
if (a === null) { | ||
return -1 | ||
} | ||
if (b === null) { | ||
return 1 | ||
} | ||
// detect typeof once | ||
const typeofA = typeof a | ||
const typeofB = typeof b | ||
// Numbers | ||
if (typeof a === 'number') { | ||
return typeof b === 'number' ? compareNSB(a, b) : -1 | ||
if (typeofA === 'number') { | ||
return typeofB === 'number' ? compareNSB(a, b) : -1 | ||
} | ||
if (typeof b === 'number') { | ||
if (typeofB === 'number') { | ||
return 1 | ||
@@ -59,6 +57,6 @@ } | ||
// Strings | ||
if (typeof a === 'string') { | ||
return typeof b === 'string' ? compareStrings(a, b) : -1 | ||
if (typeofA === 'string') { | ||
return typeofB === 'string' ? compareStrings(a, b) : -1 | ||
} | ||
if (typeof b === 'string') { | ||
if (typeofB === 'string') { | ||
return 1 | ||
@@ -68,6 +66,6 @@ } | ||
// Booleans | ||
if (typeof a === 'boolean') { | ||
return typeof b === 'boolean' ? compareNSB(a, b) : -1 | ||
if (typeofA === 'boolean') { | ||
return typeofB === 'boolean' ? compareNSB(a, b) : -1 | ||
} | ||
if (typeof b === 'boolean') { | ||
if (typeofB === 'boolean') { | ||
return 1 | ||
@@ -107,14 +105,15 @@ } | ||
// lodash-y get - probably want to use lodash get instead | ||
const get = (value: any, path: string[]) => path.reduce((value, key) => value[key], value) | ||
// An in-memory sorting function according to the | ||
// $sort special query parameter | ||
export function sorter($sort: { [key: string]: -1 | 1 }) { | ||
const get = (value: any, path: string[]) => path.reduce((value, key) => value[key], value) | ||
const compares = Object.keys($sort).map((key) => { | ||
const direction = $sort[key] | ||
const path = key.split('.') | ||
if (path.length === 1) { | ||
if (!key.includes('.')) { | ||
return (a: any, b: any) => direction * compare(a[key], b[key]) | ||
} else { | ||
const path = key.split('.') | ||
return (a: any, b: any) => direction * compare(get(a, path), get(b, path)) | ||
@@ -126,6 +125,6 @@ } | ||
for (const compare of compares) { | ||
const comparasion = compare(a, b) | ||
const comparison = compare(a, b) | ||
if (comparasion !== 0) { | ||
return comparasion | ||
if (comparison !== 0) { | ||
return comparison | ||
} | ||
@@ -132,0 +131,0 @@ } |
Sorry, the diff of this file is not supported yet
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
95428
1239
Updated@feathersjs/commons@^5.0.26
Updated@feathersjs/errors@^5.0.26
Updated@feathersjs/feathers@^5.0.26