@collabland/common
Advanced tools
Comparing version 0.24.0 to 0.25.0
import BN from 'bn.js'; | ||
import { Expression, Focus } from 'jsonata'; | ||
export declare type JsonEvaluator = Expression['evaluate']; | ||
export declare type JsonEvaluator = Expression['evaluate'] & { | ||
query: string; | ||
variables?: Record<string, unknown>; | ||
}; | ||
export declare type JsonQueryFunction = { | ||
@@ -5,0 +8,0 @@ implementation: (this: Focus, ...args: any[]) => any; |
@@ -76,26 +76,28 @@ "use strict"; | ||
}; | ||
function toBN(val) { | ||
val = val !== null && val !== void 0 ? val : '0'; | ||
if (typeof val === 'bigint' || | ||
(typeof val === 'object' && !(val instanceof bn_js_1.default))) { | ||
val = val.toString(); | ||
} | ||
return new bn_js_1.default(val); | ||
} | ||
function _add(a, b) { | ||
debug('$add(%s,%s)', a, b); | ||
a = a !== null && a !== void 0 ? a : '0'; | ||
b = b !== null && b !== void 0 ? b : '0'; | ||
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 !== null && a !== void 0 ? a : 0).add(new bn_js_1.default(b !== null && b !== void 0 ? b : 0)); | ||
return toBN(a).add(toBN(b)); | ||
} | ||
function _sub(a, b) { | ||
debug('$sub(%s,%s)', a, b); | ||
a = a !== null && a !== void 0 ? a : '0'; | ||
b = b !== null && b !== void 0 ? b : '0'; | ||
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 !== null && a !== void 0 ? a : 0).sub(new bn_js_1.default(b !== null && b !== void 0 ? b : 0)); | ||
return toBN(a).sub(toBN(b)); | ||
} | ||
function _addAll(items) { | ||
debug('$addAll(%O)', items); | ||
const vals = Array.isArray(items) ? items : [items]; | ||
let result = new bn_js_1.default(0); | ||
vals.forEach(v => { | ||
result = result.add(toBN(v)); | ||
}); | ||
debug('$addAll() returns %s', result); | ||
return result; | ||
} | ||
const add = { | ||
@@ -105,2 +107,6 @@ implementation: _add, | ||
}; | ||
const addAll = { | ||
implementation: _addAll, | ||
signature: '<(asnbol):o>', | ||
}; | ||
const sub = { | ||
@@ -206,2 +212,3 @@ implementation: _sub, | ||
sub, | ||
addAll, | ||
toString, // This is different from $string() which uses JSON.stringify | ||
@@ -231,3 +238,3 @@ }; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (input, vars) => { | ||
const evaluator = (input, vars) => { | ||
if (debug.enabled) { | ||
@@ -241,2 +248,5 @@ debug('JSONdata input: %O, bindings: %O, query: %s, variables: %O', input, bindings, query, vars); | ||
}; | ||
evaluator.query = query; | ||
evaluator.variables = bindings; | ||
return evaluator; | ||
} | ||
@@ -243,0 +253,0 @@ exports.jsonQuery = jsonQuery; |
{ | ||
"name": "@collabland/common", | ||
"version": "0.24.0", | ||
"version": "0.25.0", | ||
"description": "CollabLand common utilities", | ||
@@ -62,3 +62,3 @@ "main": "dist/index.js", | ||
"author": "Abridged, Inc.", | ||
"gitHead": "bc079a4c43e8b849abc563b3ff5841b25fdf7028" | ||
"gitHead": "e71f252bdb0c382b2d35727ce369c6edeaa370b7" | ||
} |
@@ -14,3 +14,6 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
export type JsonEvaluator = Expression['evaluate']; | ||
export type JsonEvaluator = Expression['evaluate'] & { | ||
query: string; | ||
variables?: Record<string, unknown>; | ||
}; | ||
@@ -96,13 +99,16 @@ export type JsonQueryFunction = { | ||
function toBN(val: BNLike) { | ||
val = val ?? '0'; | ||
if ( | ||
typeof val === 'bigint' || | ||
(typeof val === 'object' && !(val instanceof BN)) | ||
) { | ||
val = val.toString(); | ||
} | ||
return new BN(val); | ||
} | ||
function _add(a: BNLike, b: BNLike) { | ||
debug('$add(%s,%s)', a, b); | ||
a = a ?? '0'; | ||
b = b ?? '0'; | ||
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 ?? 0).add(new BN(b ?? 0)); | ||
return toBN(a).add(toBN(b)); | ||
} | ||
@@ -112,13 +118,16 @@ | ||
debug('$sub(%s,%s)', a, b); | ||
a = a ?? '0'; | ||
b = b ?? '0'; | ||
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 ?? 0).sub(new BN(b ?? 0)); | ||
return toBN(a).sub(toBN(b)); | ||
} | ||
function _addAll(items: BNLike[] | BNLike) { | ||
debug('$addAll(%O)', items); | ||
const vals = Array.isArray(items) ? items : [items]; | ||
let result = new BN(0); | ||
vals.forEach(v => { | ||
result = result.add(toBN(v)); | ||
}); | ||
debug('$addAll() returns %s', result); | ||
return result; | ||
} | ||
const add: JsonQueryFunction = { | ||
@@ -129,2 +138,7 @@ implementation: _add, | ||
const addAll: JsonQueryFunction = { | ||
implementation: _addAll, | ||
signature: '<(asnbol):o>', | ||
}; | ||
const sub: JsonQueryFunction = { | ||
@@ -240,2 +254,3 @@ implementation: _sub, | ||
sub, | ||
addAll, | ||
toString, // This is different from $string() which uses JSON.stringify | ||
@@ -277,3 +292,3 @@ }; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (input: any, vars?: Record<string, any>) => { | ||
const evaluator: JsonEvaluator = (input: any, vars?: Record<string, any>) => { | ||
if (debug.enabled) { | ||
@@ -293,2 +308,5 @@ debug( | ||
}; | ||
evaluator.query = query; | ||
evaluator.variables = bindings; | ||
return evaluator; | ||
} | ||
@@ -295,0 +313,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
105273
68
2582