@collabland/common
Advanced tools
Comparing version 0.19.1 to 0.20.0
@@ -0,1 +1,2 @@ | ||
export { default as BN } from 'bn.js'; | ||
export { default as jsonata } from 'jsonata'; | ||
@@ -2,0 +3,0 @@ export { default as lodash } from 'lodash'; |
@@ -7,4 +7,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pMap = exports.lodash = exports.jsonata = void 0; | ||
exports.pMap = exports.lodash = exports.jsonata = exports.BN = void 0; | ||
const tslib_1 = require("tslib"); | ||
var bn_js_1 = require("bn.js"); | ||
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return tslib_1.__importDefault(bn_js_1).default; } }); | ||
var jsonata_1 = require("jsonata"); | ||
@@ -11,0 +13,0 @@ Object.defineProperty(exports, "jsonata", { enumerable: true, get: function () { return tslib_1.__importDefault(jsonata_1).default; } }); |
@@ -30,2 +30,3 @@ import { Debugger } from 'debug'; | ||
export declare function setDebugSettings(settings: Record<string, boolean>): void; | ||
export declare function inspectJson(value: unknown): string; | ||
export declare function inspectJson(value: unknown, depth?: number): string; | ||
export declare function stringify(value: unknown): string; |
@@ -7,3 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.inspectJson = exports.setDebugSettings = exports.getDebugSettings = exports.isDebugEnabled = exports.enableDebug = exports.debugFactory = exports.debugNamespaces = void 0; | ||
exports.stringify = exports.inspectJson = exports.setDebugSettings = exports.getDebugSettings = exports.isDebugEnabled = exports.enableDebug = exports.debugFactory = exports.debugNamespaces = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -78,6 +78,10 @@ const debug_1 = tslib_1.__importDefault(require("debug")); | ||
exports.setDebugSettings = setDebugSettings; | ||
function inspectJson(value) { | ||
return util_1.default.inspect(value, { depth: 5 }); | ||
function inspectJson(value, depth = 8) { | ||
return util_1.default.inspect(value, { depth }); | ||
} | ||
exports.inspectJson = inspectJson; | ||
function stringify(value) { | ||
return JSON.stringify(value, null, 2); | ||
} | ||
exports.stringify = stringify; | ||
//# sourceMappingURL=debug.js.map |
@@ -0,1 +1,2 @@ | ||
import BN from 'bn.js'; | ||
import { Expression, Focus } from 'jsonata'; | ||
@@ -7,2 +8,3 @@ export declare type JsonEvaluator = Expression['evaluate']; | ||
}; | ||
export declare type BNLike = number | string | BN | object | bigint | null | undefined; | ||
/** | ||
@@ -9,0 +11,0 @@ * Global jsonata functions |
@@ -9,21 +9,118 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const bn_js_1 = tslib_1.__importDefault(require("bn.js")); | ||
const jsonata_1 = tslib_1.__importDefault(require("jsonata")); | ||
const lodash_1 = tslib_1.__importDefault(require("lodash")); | ||
const debug_1 = require("./debug"); | ||
const http_client_fetch_1 = require("./http-client-fetch"); | ||
const debug = debug_1.debugFactory('collabland:common:jsonata'); | ||
/** | ||
* Check the list contains given items | ||
* @param list - An array of items | ||
* @param items - One or more items to be checked | ||
* @returns | ||
*/ | ||
function includesItemsInArray(list, items) { | ||
debug('$includes(%O, %O)', list, items); | ||
let result = false; | ||
if (Array.isArray(items)) { | ||
result = items.every(v => list.some(val => lodash_1.default.isEqual(val, v))); | ||
} | ||
else { | ||
result = list.some(v => lodash_1.default.isEqual(v, items)); | ||
} | ||
debug('$includes() returns %s', true); | ||
return result; | ||
} | ||
/** | ||
* Check the list contains given items | ||
* @param object - An object of items keyed by id or name | ||
* @param items - One or more items to be checked | ||
* @returns | ||
*/ | ||
function includesItemsInObject(obj, items) { | ||
return Object.entries(obj).some(([k, v]) => { | ||
const list = Array.isArray(v) ? v : [v]; | ||
return includesItemsInArray(list, items); | ||
}); | ||
} | ||
function includesItems(parent, items) { | ||
if (Array.isArray(parent)) { | ||
return includesItemsInArray(parent, items); | ||
} | ||
if (parent != null && typeof parent === 'object') { | ||
return includesItemsInObject(parent, items); | ||
} | ||
throw Error('The first argument must be an object or array'); | ||
} | ||
/** | ||
* `$includes` checks the first argument includes the child items. The following | ||
* structures are supported: | ||
* | ||
* - $includes(['a', 'b', 'c'], 'a') | ||
* - $includes(['a', 'b', 'c'], ['a', 'c']) | ||
* - $includes({x: ['a', 'b', 'c']}, ['a', 'c']) | ||
* - $includes({x: ['a', 'b'], y: ['a', 'c']}, ['a', 'c']) | ||
* - $includes({x: ['a', 'b', 'c']}, 'b') | ||
* - $includes({x: 'a', y: 'b'}, 'b') | ||
*/ | ||
const includes = { | ||
implementation: (list, values) => { | ||
debug('$includes(%O, %O)', list, values); | ||
let result = false; | ||
if (Array.isArray(values)) { | ||
result = values.every(v => list.some(val => lodash_1.default.isEqual(val, v))); | ||
} | ||
else { | ||
result = list.some(v => lodash_1.default.isEqual(v, values)); | ||
} | ||
debug('$includes() returns %s', true); | ||
return result; | ||
}, | ||
signature: '<a(asnblo):b>', | ||
implementation: includesItems, | ||
signature: '<(ao)(asnblo):b>', | ||
}; | ||
function compare(a, b) { | ||
if (a === b) | ||
return 0; | ||
if (a == null || b == null) | ||
return undefined; | ||
if (typeof a === 'bigint' || (typeof a === 'object' && !(a instanceof bn_js_1.default))) { | ||
a = a.toString(); | ||
} | ||
if (typeof b === 'bigint' || (typeof b === 'object' && !(b instanceof bn_js_1.default))) { | ||
b = b.toString(); | ||
} | ||
return new bn_js_1.default(a).cmp(new bn_js_1.default(b)); | ||
} | ||
const compareTo = { | ||
implementation: (a, b) => compare(a, b), | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const equal = (a, b) => compare(a, b) === 0; | ||
const eq = { | ||
implementation: equal, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const notEqual = (a, b) => { | ||
const result = compare(a, b); | ||
return result == null || result !== 0; | ||
}; | ||
const ne = { | ||
implementation: notEqual, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const greaterThan = (a, b) => compare(a, b) === 1; | ||
const gt = { | ||
implementation: greaterThan, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const greaterThanOrEqual = (a, b) => { | ||
const result = compare(a, b); | ||
return result != null && result !== -1; | ||
}; | ||
const gte = { | ||
implementation: greaterThanOrEqual, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const lessThan = (a, b) => compare(a, b) === -1; | ||
const lt = { | ||
implementation: lessThan, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const lessThanOrEqual = (a, b) => { | ||
const result = compare(a, b); | ||
return result != null && result !== 1; | ||
}; | ||
const lte = { | ||
implementation: lessThanOrEqual, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const between = { | ||
@@ -33,5 +130,9 @@ implementation: (value, min, max) => { | ||
value = value !== null && value !== void 0 ? value : 0; | ||
return (min == null || value >= min) && (max == null || value <= max); | ||
debug('$between(%s, %s, %s)', value, min, max); | ||
const result = (min == null || greaterThanOrEqual(value, min)) && | ||
(max == null || lessThanOrEqual(value, max)); | ||
debug('Result: %s', result); | ||
return result; | ||
}, | ||
signature: '<(nl)(nl)(nl):b>', | ||
signature: '<(snblo)(snblo)(snblo):b>', | ||
}; | ||
@@ -44,2 +145,9 @@ /** | ||
includes, | ||
compare: compareTo, | ||
eq, | ||
ne, | ||
gt, | ||
gte, | ||
lt, | ||
lte, | ||
}; | ||
@@ -56,2 +164,5 @@ /** | ||
for (const b in bindings) { | ||
if (BUILT_IN_FUNCTIONS.has(b)) { | ||
throw new http_client_fetch_1.HttpErrors.BadRequest(`The built-in function ${b} cannot be overridden.`); | ||
} | ||
exp.assign(b, bindings[b]); | ||
@@ -68,2 +179,77 @@ } | ||
exports.jsonQuery = jsonQuery; | ||
const BUILT_IN_FUNCTIONS = new Set([ | ||
'sum', | ||
'count', | ||
'max', | ||
'min', | ||
'average', | ||
'string', | ||
'substring', | ||
'substringBefore', | ||
'substringAfter', | ||
'lowercase', | ||
'uppercase', | ||
'length', | ||
'trim', | ||
'pad', | ||
'match', | ||
'contains', | ||
'replace', | ||
'split', | ||
'join', | ||
'formatNumber', | ||
'formatBase', | ||
'formatInteger', | ||
'parseInteger', | ||
'number', | ||
'floor', | ||
'ceil', | ||
'round', | ||
'abs', | ||
'sqrt', | ||
'power', | ||
'random', | ||
'boolean', | ||
'not', | ||
'map', | ||
'zip', | ||
'filter', | ||
'single', | ||
'reduce', | ||
'sift', | ||
'keys', | ||
'lookup', | ||
'append', | ||
'exists', | ||
'spread', | ||
'merge', | ||
'reverse', | ||
'each', | ||
'error', | ||
'assert', | ||
'type', | ||
'sort', | ||
'shuffle', | ||
'distinct', | ||
'base64encode', | ||
'base64decode', | ||
'encodeUrlComponent', | ||
'encodeUrl', | ||
'decodeUrlComponent', | ||
'decodeUrl', | ||
'eval', | ||
'toMillis', | ||
'fromMillis', | ||
'clone', | ||
// CollabLand extensions | ||
'between', | ||
'includes', | ||
'compare', | ||
'eq', | ||
'ne', | ||
'gt', | ||
'gte', | ||
'lt', | ||
'lte', | ||
]); | ||
//# sourceMappingURL=jsonata.js.map |
{ | ||
"name": "@collabland/common", | ||
"version": "0.19.1", | ||
"version": "0.20.0", | ||
"description": "CollabLand common utilities", | ||
@@ -32,2 +32,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@types/bn.js": "^5.1.0", | ||
"@types/debug": "^4.1.5", | ||
@@ -39,2 +40,3 @@ "@types/http-errors": "^1.8.0", | ||
"@types/pino": "^6.3.5", | ||
"bn.js": "^5.0.0", | ||
"cross-fetch": "^3.1.3", | ||
@@ -61,3 +63,3 @@ "debug": "^4.3.1", | ||
"author": "Abridged, Inc.", | ||
"gitHead": "e0944f19fecd734aa41d1522059cb7c48137a37d" | ||
"gitHead": "bd7b12dbd301b98fecd78442f97f0d065f0babf6" | ||
} |
@@ -6,2 +6,3 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
export {default as BN} from 'bn.js'; | ||
export {default as jsonata} from 'jsonata'; | ||
@@ -8,0 +9,0 @@ export {default as lodash} from 'lodash'; |
@@ -78,4 +78,8 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
export function inspectJson(value: unknown) { | ||
return util.inspect(value, {depth: 5}); | ||
export function inspectJson(value: unknown, depth = 8) { | ||
return util.inspect(value, {depth}); | ||
} | ||
export function stringify(value: unknown) { | ||
return JSON.stringify(value, null, 2); | ||
} |
@@ -6,5 +6,8 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
import BN from 'bn.js'; | ||
import jsonata, {Expression, Focus} from 'jsonata'; | ||
import lodash from 'lodash'; | ||
import {debugFactory} from './debug'; | ||
import {HttpErrors} from './http-client-fetch'; | ||
const debug = debugFactory('collabland:common:jsonata'); | ||
@@ -20,28 +23,141 @@ | ||
/** | ||
* Check the list contains given items | ||
* @param list - An array of items | ||
* @param items - One or more items to be checked | ||
* @returns | ||
*/ | ||
function includesItemsInArray(list: unknown[], items: unknown[] | unknown) { | ||
debug('$includes(%O, %O)', list, items); | ||
let result = false; | ||
if (Array.isArray(items)) { | ||
result = items.every(v => list.some(val => lodash.isEqual(val, v))); | ||
} else { | ||
result = list.some(v => lodash.isEqual(v, items)); | ||
} | ||
debug('$includes() returns %s', true); | ||
return result; | ||
} | ||
/** | ||
* Check the list contains given items | ||
* @param object - An object of items keyed by id or name | ||
* @param items - One or more items to be checked | ||
* @returns | ||
*/ | ||
function includesItemsInObject( | ||
obj: Record<string, unknown>, | ||
items: unknown[] | unknown, | ||
) { | ||
return Object.entries(obj).some(([k, v]) => { | ||
const list = Array.isArray(v) ? v : [v]; | ||
return includesItemsInArray(list, items); | ||
}); | ||
} | ||
function includesItems( | ||
parent: Record<string, unknown> | unknown[], | ||
items: unknown[] | unknown, | ||
) { | ||
if (Array.isArray(parent)) { | ||
return includesItemsInArray(parent, items); | ||
} | ||
if (parent != null && typeof parent === 'object') { | ||
return includesItemsInObject(parent, items); | ||
} | ||
throw Error('The first argument must be an object or array'); | ||
} | ||
/** | ||
* `$includes` checks the first argument includes the child items. The following | ||
* structures are supported: | ||
* | ||
* - $includes(['a', 'b', 'c'], 'a') | ||
* - $includes(['a', 'b', 'c'], ['a', 'c']) | ||
* - $includes({x: ['a', 'b', 'c']}, ['a', 'c']) | ||
* - $includes({x: ['a', 'b'], y: ['a', 'c']}, ['a', 'c']) | ||
* - $includes({x: ['a', 'b', 'c']}, 'b') | ||
* - $includes({x: 'a', y: 'b'}, 'b') | ||
*/ | ||
const includes: JsonQueryFunction = { | ||
implementation: (list: unknown[], values: unknown[] | unknown) => { | ||
debug('$includes(%O, %O)', list, values); | ||
let result = false; | ||
if (Array.isArray(values)) { | ||
result = values.every(v => list.some(val => lodash.isEqual(val, v))); | ||
} else { | ||
result = list.some(v => lodash.isEqual(v, values)); | ||
} | ||
debug('$includes() returns %s', true); | ||
return result; | ||
}, | ||
signature: '<a(asnblo):b>', | ||
implementation: includesItems, | ||
signature: '<(ao)(asnblo):b>', | ||
}; | ||
export type BNLike = number | string | BN | object | bigint | null | undefined; | ||
function compare(a: BNLike, b: BNLike) { | ||
if (a === b) return 0; | ||
if (a == null || b == null) return undefined; | ||
if (typeof a === 'bigint' || (typeof a === 'object' && !(a instanceof BN))) { | ||
a = a.toString(); | ||
} | ||
if (typeof b === 'bigint' || (typeof b === 'object' && !(b instanceof BN))) { | ||
b = b.toString(); | ||
} | ||
return new BN(a).cmp(new BN(b)); | ||
} | ||
const compareTo: JsonQueryFunction = { | ||
implementation: (a: BNLike, b: BNLike) => compare(a, b), | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const equal = (a: BNLike, b: BNLike) => compare(a, b) === 0; | ||
const eq: JsonQueryFunction = { | ||
implementation: equal, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const notEqual = (a: BNLike, b: BNLike) => { | ||
const result = compare(a, b); | ||
return result == null || result !== 0; | ||
}; | ||
const ne: JsonQueryFunction = { | ||
implementation: notEqual, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const greaterThan = (a: BNLike, b: BNLike) => compare(a, b) === 1; | ||
const gt: JsonQueryFunction = { | ||
implementation: greaterThan, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const greaterThanOrEqual = (a: BNLike, b: BNLike) => { | ||
const result = compare(a, b); | ||
return result != null && result !== -1; | ||
}; | ||
const gte: JsonQueryFunction = { | ||
implementation: greaterThanOrEqual, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const lessThan = (a: BNLike, b: BNLike) => compare(a, b) === -1; | ||
const lt: JsonQueryFunction = { | ||
implementation: lessThan, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const lessThanOrEqual = (a: BNLike, b: BNLike) => { | ||
const result = compare(a, b); | ||
return result != null && result !== 1; | ||
}; | ||
const lte: JsonQueryFunction = { | ||
implementation: lessThanOrEqual, | ||
signature: '<(snblo)(snblo):(bl)>', | ||
}; | ||
const between: JsonQueryFunction = { | ||
implementation: ( | ||
value: number | null, | ||
min: number | null, | ||
max: number | null, | ||
) => { | ||
implementation: (value: BNLike, min: BNLike, max: BNLike) => { | ||
// The value is default to 0 if it does not exist | ||
value = value ?? 0; | ||
return (min == null || value >= min) && (max == null || value <= max); | ||
debug('$between(%s, %s, %s)', value, min, max); | ||
const result = | ||
(min == null || greaterThanOrEqual(value, min)) && | ||
(max == null || lessThanOrEqual(value, max)); | ||
debug('Result: %s', result); | ||
return result; | ||
}, | ||
signature: '<(nl)(nl)(nl):b>', | ||
signature: '<(snblo)(snblo)(snblo):b>', | ||
}; | ||
@@ -55,2 +171,9 @@ | ||
includes, | ||
compare: compareTo, | ||
eq, | ||
ne, | ||
gt, | ||
gte, | ||
lt, | ||
lte, | ||
}; | ||
@@ -71,2 +194,7 @@ | ||
for (const b in bindings) { | ||
if (BUILT_IN_FUNCTIONS.has(b)) { | ||
throw new HttpErrors.BadRequest( | ||
`The built-in function ${b} cannot be overridden.`, | ||
); | ||
} | ||
exp.assign(b, bindings[b]); | ||
@@ -88,1 +216,77 @@ } | ||
} | ||
const BUILT_IN_FUNCTIONS = new Set([ | ||
'sum', | ||
'count', | ||
'max', | ||
'min', | ||
'average', | ||
'string', | ||
'substring', | ||
'substringBefore', | ||
'substringAfter', | ||
'lowercase', | ||
'uppercase', | ||
'length', | ||
'trim', | ||
'pad', | ||
'match', | ||
'contains', | ||
'replace', | ||
'split', | ||
'join', | ||
'formatNumber', | ||
'formatBase', | ||
'formatInteger', | ||
'parseInteger', | ||
'number', | ||
'floor', | ||
'ceil', | ||
'round', | ||
'abs', | ||
'sqrt', | ||
'power', | ||
'random', | ||
'boolean', | ||
'not', | ||
'map', | ||
'zip', | ||
'filter', | ||
'single', | ||
'reduce', | ||
'sift', | ||
'keys', | ||
'lookup', | ||
'append', | ||
'exists', | ||
'spread', | ||
'merge', | ||
'reverse', | ||
'each', | ||
'error', | ||
'assert', | ||
'type', | ||
'sort', | ||
'shuffle', | ||
'distinct', | ||
'base64encode', | ||
'base64decode', | ||
'encodeUrlComponent', | ||
'encodeUrl', | ||
'decodeUrlComponent', | ||
'decodeUrl', | ||
'eval', | ||
'toMillis', | ||
'fromMillis', | ||
'clone', | ||
// CollabLand extensions | ||
'between', | ||
'includes', | ||
'compare', | ||
'eq', | ||
'ne', | ||
'gt', | ||
'gte', | ||
'lt', | ||
'lte', | ||
]); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
93494
2278
18
+ Added@types/bn.js@^5.1.0
+ Addedbn.js@^5.0.0
+ Added@types/bn.js@5.1.6(transitive)
+ Addedbn.js@5.2.1(transitive)