@collabland/common
Advanced tools
Comparing version 0.26.0 to 0.27.0
@@ -20,1 +20,3 @@ /// <reference types="node" /> | ||
export declare function isBS58Encoded(data: string): boolean; | ||
export declare function baseEncode(value: Uint8Array | string): string; | ||
export declare function baseDecode(value: string): Buffer; |
@@ -7,3 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isBS58Encoded = exports.bs58Decode = exports.bs58Encode = void 0; | ||
exports.baseDecode = exports.baseEncode = exports.isBS58Encoded = exports.bs58Decode = exports.bs58Encode = void 0; | ||
const bs58_1 = require("bs58"); | ||
@@ -40,2 +40,13 @@ /** | ||
exports.isBS58Encoded = isBS58Encoded; | ||
function baseEncode(value) { | ||
if (typeof value === 'string') { | ||
value = Buffer.from(value, 'utf8'); | ||
} | ||
return (0, bs58_1.encode)(Buffer.from(value)); | ||
} | ||
exports.baseEncode = baseEncode; | ||
function baseDecode(value) { | ||
return Buffer.from((0, bs58_1.decode)(value)); | ||
} | ||
exports.baseDecode = baseDecode; | ||
//# sourceMappingURL=bs58.js.map |
@@ -25,4 +25,10 @@ /** | ||
*/ | ||
export declare function getEnvVar(name: string, defaultValue?: string): string | undefined; | ||
export declare function getEnvVar(name: string, defaultValue: string): string; | ||
/** | ||
* Get the string value of an environment variable | ||
* @param name - Name of the variable | ||
* @returns | ||
*/ | ||
export declare function getEnvVar(name: string): string | undefined; | ||
/** | ||
* Get the number value of an environment variable | ||
@@ -56,3 +62,3 @@ * @param name - Name of the variable | ||
*/ | ||
export declare function setEnvVar(name: string, value: unknown, override?: boolean): unknown; | ||
export declare function setEnvVar(name: string, value: unknown, override?: boolean): string | null | undefined; | ||
/** | ||
@@ -59,0 +65,0 @@ * Delete an environment variable |
@@ -46,8 +46,2 @@ "use strict"; | ||
exports.getApiServerUrl = getApiServerUrl; | ||
/** | ||
* Get the string value of an environment variable | ||
* @param name - Name of the variable | ||
* @param defaultValue - Default value | ||
* @returns | ||
*/ | ||
function getEnvVar(name, defaultValue) { | ||
@@ -54,0 +48,0 @@ var _a; |
@@ -23,1 +23,19 @@ import BN from 'bn.js'; | ||
export declare function jsonQuery(query: string, bindings?: Record<string, unknown>): JsonEvaluator; | ||
/** | ||
* Parse a string as an array of ranges | ||
* @param ids - A string that lists items and/or item ranges, such as `1`, | ||
* `1,2,5`, or `1-2,4,6-8` | ||
* | ||
* @example | ||
* - '1' -> ['1'] | ||
* - '1,2' -> ['1','2'] | ||
* - '2-3' -> [['2','3']] | ||
* - '2-3,5' -> [['2','3'], '5'] | ||
* - '*,5' -> ['*', '5'] | ||
* - '5??' -> ['5??'] | ||
* - '-5' -> [['','5']] | ||
* - '5-' -> [['5','']] | ||
* @returns | ||
*/ | ||
export declare function parseRanges(ids: string): ([string, string] | string)[]; | ||
export declare function isInRanges(value: unknown, ranges: string): boolean; |
@@ -7,3 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.jsonQuery = exports.globalJsonQueryFunctions = void 0; | ||
exports.isInRanges = exports.parseRanges = exports.jsonQuery = exports.globalJsonQueryFunctions = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -196,2 +196,8 @@ const bn_js_1 = (0, tslib_1.__importDefault)(require("bn.js")); | ||
}; | ||
const inRanges = { | ||
implementation: (value, ranges) => { | ||
return isInRanges(value, ranges); | ||
}, | ||
signature: '<(snlo)s:b>', | ||
}; | ||
/** | ||
@@ -213,3 +219,4 @@ * Global jsonata functions | ||
addAll, | ||
toString, // This is different from $string() which uses JSON.stringify | ||
toString, | ||
inRanges, | ||
}; | ||
@@ -327,2 +334,86 @@ /** | ||
]); | ||
/** | ||
* Parse a string as an array of ranges | ||
* @param ids - A string that lists items and/or item ranges, such as `1`, | ||
* `1,2,5`, or `1-2,4,6-8` | ||
* | ||
* @example | ||
* - '1' -> ['1'] | ||
* - '1,2' -> ['1','2'] | ||
* - '2-3' -> [['2','3']] | ||
* - '2-3,5' -> [['2','3'], '5'] | ||
* - '*,5' -> ['*', '5'] | ||
* - '5??' -> ['5??'] | ||
* - '-5' -> [['','5']] | ||
* - '5-' -> [['5','']] | ||
* @returns | ||
*/ | ||
function parseRanges(ids) { | ||
const parts = ids.split(/[,\s]+/).filter(Boolean); | ||
return parts.map(id => { | ||
if (id.includes('-')) { | ||
// 2-100 inclusive | ||
const bounds = id.replace(/\s+/g, '').split(/-/); | ||
return [bounds[0], bounds[1]]; | ||
} | ||
else { | ||
return id; | ||
} | ||
}); | ||
} | ||
exports.parseRanges = parseRanges; | ||
function isInRanges(value, ranges) { | ||
debug('$inRanges(%s, %s)', value, ranges); | ||
if (value == null) | ||
return false; | ||
const idStr = String(value); | ||
if (String(value) === ranges) { | ||
debug('Result: %s', true); | ||
return true; | ||
} | ||
const ids = parseRanges(ranges); | ||
// Check if the token id falls into the ranges | ||
const result = ids.some(range => { | ||
if (typeof range === 'string') { | ||
if (idStr === range) | ||
return true; | ||
if (range.includes('?') || range.includes('*')) { | ||
// Wildcard | ||
const regex = wildcardToRegExp(range); | ||
return idStr.match(regex); | ||
} | ||
return false; | ||
} | ||
// range[0] <= id && id <= range[1] | ||
try { | ||
const val = new bn_js_1.default(idStr); | ||
return ((range[0] === '' || val.gte(new bn_js_1.default(range[0]))) && | ||
(range[1] === '' || val.lte(new bn_js_1.default(range[1])))); | ||
} | ||
catch (err) { | ||
debug('%s is not a number', idStr); | ||
return ((range[0] === '' || idStr.localeCompare(range[0]) !== -1) && | ||
(range[1] === '' || idStr.localeCompare(range[1]) !== 1)); | ||
} | ||
}); | ||
debug('Result: %s', result); | ||
return result; | ||
} | ||
exports.isInRanges = isInRanges; | ||
/** | ||
* Convert a wildcard pattern to RegExp | ||
* @param pattern - A wildcard string with `*` and `?` as special characters. | ||
* - `*` matches zero or more characters except `.` and `:` | ||
* - `?` matches exactly one character except `.` and `:` | ||
*/ | ||
function wildcardToRegExp(pattern) { | ||
// Escape reserved chars for RegExp: | ||
// `- \ ^ $ + . ( ) | { } [ ] :` | ||
let regexp = pattern.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|\:]/g, '\\$&'); | ||
// Replace wildcard chars `*` and `?` | ||
// `*` matches zero or more characters except `.` and `:` | ||
// `?` matches one character except `.` and `:` | ||
regexp = regexp.replace(/\*/g, '[^.:]*').replace(/\?/g, '[^.:]'); | ||
return new RegExp(`^${regexp}$`); | ||
} | ||
//# sourceMappingURL=jsonata.js.map |
@@ -18,3 +18,3 @@ import pMap from 'p-map'; | ||
* Map a large collection asynchronously with pagination (start, end) | ||
* @param size - Size of the collection | ||
* @param range - Size of the collection | ||
* @param mapper - Mapping function that handles a range (start, end) | ||
@@ -24,3 +24,3 @@ * @param options - Options for mapping, including `concurrency` and `pageSize` | ||
*/ | ||
export declare function pMapByRange<N = unknown>(size: number, mapper: pMap.Mapper<{ | ||
export declare function pMapByRange<N = unknown>(range: number | [number, number], mapper: pMap.Mapper<{ | ||
start: number; | ||
@@ -27,0 +27,0 @@ end: number; |
@@ -36,5 +36,21 @@ "use strict"; | ||
} | ||
function paginateRanges(size, pageSize = 100) { | ||
function paginateRanges(range, pageSize = 100) { | ||
let size; | ||
if (Array.isArray(range)) { | ||
size = range[1] - range[0] + 1; | ||
} | ||
else { | ||
size = range; | ||
} | ||
return Array.from({ length: Math.ceil(size / pageSize) }, (v, page) => { | ||
return getRange(page, pageSize, size); | ||
const r = getRange(page, pageSize, size); | ||
if (typeof range === 'number') { | ||
return r; | ||
} | ||
else { | ||
return { | ||
start: r.start + range[0], | ||
end: r.end + range[0], | ||
}; | ||
} | ||
}); | ||
@@ -62,3 +78,3 @@ } | ||
* Map a large collection asynchronously with pagination (start, end) | ||
* @param size - Size of the collection | ||
* @param range - Size of the collection | ||
* @param mapper - Mapping function that handles a range (start, end) | ||
@@ -68,4 +84,4 @@ * @param options - Options for mapping, including `concurrency` and `pageSize` | ||
*/ | ||
async function pMapByRange(size, mapper, options) { | ||
const rangesByPage = paginateRanges(size, options === null || options === void 0 ? void 0 : options.pageSize); | ||
async function pMapByRange(range, mapper, options) { | ||
const rangesByPage = paginateRanges(range, options === null || options === void 0 ? void 0 : options.pageSize); | ||
const pages = await (0, p_map_1.default)(rangesByPage, mapper, options); | ||
@@ -72,0 +88,0 @@ return pages; |
@@ -8,7 +8,7 @@ /// <reference types="node" /> | ||
*/ | ||
export declare function resolvePromiseWithTimeout<T>(promise: Promise<T> | (() => Promise<T>), timeoutMs: number, failureMessage?: string): Promise<T>; | ||
export declare function resolvePromiseWithTimeout<T>(promise: Promise<T> | (() => Promise<T>), timeoutMs: number, failureMessage?: string): Promise<Awaited<T>>; | ||
/** | ||
* Sleep function | ||
*/ | ||
export declare const sleep: typeof setTimeout.__promisify__; | ||
export declare const sleep: typeof import("timers/promises").setTimeout; | ||
/** | ||
@@ -15,0 +15,0 @@ * Exponential backoff strategy for retries |
{ | ||
"name": "@collabland/common", | ||
"version": "0.26.0", | ||
"version": "0.27.0", | ||
"description": "CollabLand common utilities", | ||
@@ -39,4 +39,5 @@ "main": "dist/index.js", | ||
"@types/nanoid": "^2.1.0", | ||
"@types/pino": "^6.3.5", | ||
"@types/pino": "^7.0.5", | ||
"bn.js": "^5.0.0", | ||
"borsh": "^0.6.0", | ||
"bs58": "^4.0.1", | ||
@@ -51,15 +52,15 @@ "cross-fetch": "^3.1.3", | ||
"p-map": "^4.0.0", | ||
"pino": "^7.0.0", | ||
"pino": "^7.3.0", | ||
"tslib": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^7.0.2", | ||
"@loopback/eslint-config": "^11.0.2", | ||
"@loopback/testlab": "^3.4.4", | ||
"@types/node": "^12.12.6", | ||
"typescript": "~4.4.4" | ||
"@loopback/build": "^8.0.0", | ||
"@loopback/eslint-config": "^12.0.0", | ||
"@loopback/testlab": "^4.0.0", | ||
"@types/node": "^16.11.9", | ||
"typescript": "~4.5.2" | ||
}, | ||
"copyright.owner": "Abridged, Inc.", | ||
"author": "Abridged, Inc.", | ||
"gitHead": "2fb2a2a33b8afa9d6208ddac65c4ddc6dd66dca8" | ||
"gitHead": "8669002d6e5ea1b1011e5f985b32f00e939c1d15" | ||
} |
@@ -39,1 +39,12 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
} | ||
export function baseEncode(value: Uint8Array | string): string { | ||
if (typeof value === 'string') { | ||
value = Buffer.from(value, 'utf8'); | ||
} | ||
return encode(Buffer.from(value)); | ||
} | ||
export function baseDecode(value: string): Buffer { | ||
return Buffer.from(decode(value)); | ||
} |
@@ -49,2 +49,11 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
*/ | ||
export function getEnvVar(name: string, defaultValue: string): string; | ||
/** | ||
* Get the string value of an environment variable | ||
* @param name - Name of the variable | ||
* @returns | ||
*/ | ||
export function getEnvVar(name: string): string | undefined; | ||
export function getEnvVar( | ||
@@ -51,0 +60,0 @@ name: string, |
@@ -9,2 +9,3 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
import lodash from 'lodash'; | ||
import {AnyError} from '..'; | ||
import {debugFactory, stringify} from './debug'; | ||
@@ -236,2 +237,9 @@ import {HttpErrors} from './http-client-fetch'; | ||
const inRanges: JsonQueryFunction = { | ||
implementation: (value: unknown, ranges: string) => { | ||
return isInRanges(value, ranges); | ||
}, | ||
signature: '<(snlo)s:b>', | ||
}; | ||
/** | ||
@@ -254,2 +262,3 @@ * Global jsonata functions | ||
toString, // This is different from $string() which uses JSON.stringify | ||
inRanges, | ||
}; | ||
@@ -385,1 +394,86 @@ | ||
]); | ||
/** | ||
* Parse a string as an array of ranges | ||
* @param ids - A string that lists items and/or item ranges, such as `1`, | ||
* `1,2,5`, or `1-2,4,6-8` | ||
* | ||
* @example | ||
* - '1' -> ['1'] | ||
* - '1,2' -> ['1','2'] | ||
* - '2-3' -> [['2','3']] | ||
* - '2-3,5' -> [['2','3'], '5'] | ||
* - '*,5' -> ['*', '5'] | ||
* - '5??' -> ['5??'] | ||
* - '-5' -> [['','5']] | ||
* - '5-' -> [['5','']] | ||
* @returns | ||
*/ | ||
export function parseRanges(ids: string): ([string, string] | string)[] { | ||
const parts = ids.split(/[,\s]+/).filter(Boolean); | ||
return parts.map(id => { | ||
if (id.includes('-')) { | ||
// 2-100 inclusive | ||
const bounds = id.replace(/\s+/g, '').split(/-/); | ||
return [bounds[0], bounds[1]]; | ||
} else { | ||
return id; | ||
} | ||
}); | ||
} | ||
export function isInRanges(value: unknown, ranges: string) { | ||
debug('$inRanges(%s, %s)', value, ranges); | ||
if (value == null) return false; | ||
const idStr = String(value); | ||
if (String(value) === ranges) { | ||
debug('Result: %s', true); | ||
return true; | ||
} | ||
const ids = parseRanges(ranges); | ||
// Check if the token id falls into the ranges | ||
const result = ids.some(range => { | ||
if (typeof range === 'string') { | ||
if (idStr === range) return true; | ||
if (range.includes('?') || range.includes('*')) { | ||
// Wildcard | ||
const regex = wildcardToRegExp(range); | ||
return idStr.match(regex); | ||
} | ||
return false; | ||
} | ||
// range[0] <= id && id <= range[1] | ||
try { | ||
const val = new BN(idStr); | ||
return ( | ||
(range[0] === '' || val.gte(new BN(range[0]))) && | ||
(range[1] === '' || val.lte(new BN(range[1]))) | ||
); | ||
} catch (err: AnyError) { | ||
debug('%s is not a number', idStr); | ||
return ( | ||
(range[0] === '' || idStr.localeCompare(range[0]) !== -1) && | ||
(range[1] === '' || idStr.localeCompare(range[1]) !== 1) | ||
); | ||
} | ||
}); | ||
debug('Result: %s', result); | ||
return result; | ||
} | ||
/** | ||
* Convert a wildcard pattern to RegExp | ||
* @param pattern - A wildcard string with `*` and `?` as special characters. | ||
* - `*` matches zero or more characters except `.` and `:` | ||
* - `?` matches exactly one character except `.` and `:` | ||
*/ | ||
function wildcardToRegExp(pattern: string): RegExp { | ||
// Escape reserved chars for RegExp: | ||
// `- \ ^ $ + . ( ) | { } [ ] :` | ||
let regexp = pattern.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|\:]/g, '\\$&'); | ||
// Replace wildcard chars `*` and `?` | ||
// `*` matches zero or more characters except `.` and `:` | ||
// `?` matches one character except `.` and `:` | ||
regexp = regexp.replace(/\*/g, '[^.:]*').replace(/\?/g, '[^.:]'); | ||
return new RegExp(`^${regexp}$`); | ||
} |
@@ -37,5 +37,19 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
function paginateRanges(size: number, pageSize = 100) { | ||
function paginateRanges(range: number | [number, number], pageSize = 100) { | ||
let size: number; | ||
if (Array.isArray(range)) { | ||
size = range[1] - range[0] + 1; | ||
} else { | ||
size = range; | ||
} | ||
return Array.from({length: Math.ceil(size / pageSize)}, (v, page) => { | ||
return getRange(page, pageSize, size); | ||
const r = getRange(page, pageSize, size); | ||
if (typeof range === 'number') { | ||
return r; | ||
} else { | ||
return { | ||
start: r.start + range[0], | ||
end: r.end + range[0], | ||
}; | ||
} | ||
}); | ||
@@ -76,3 +90,3 @@ } | ||
* Map a large collection asynchronously with pagination (start, end) | ||
* @param size - Size of the collection | ||
* @param range - Size of the collection | ||
* @param mapper - Mapping function that handles a range (start, end) | ||
@@ -83,7 +97,7 @@ * @param options - Options for mapping, including `concurrency` and `pageSize` | ||
export async function pMapByRange<N = unknown>( | ||
size: number, | ||
range: number | [number, number], | ||
mapper: pMap.Mapper<{start: number; end: number}, N>, | ||
options?: PMapByPageOptions, | ||
): Promise<N[]> { | ||
const rangesByPage = paginateRanges(size, options?.pageSize); | ||
const rangesByPage = paginateRanges(range, options?.pageSize); | ||
const pages = await pMap(rangesByPage, mapper, options); | ||
@@ -90,0 +104,0 @@ return pages; |
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
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
127077
3067
21
+ Addedborsh@^0.6.0
+ Added@types/pino@7.0.5(transitive)
+ Addedborsh@0.6.0(transitive)
+ Addedtext-encoding-utf-8@1.0.2(transitive)
- Removed@types/pino@6.3.12(transitive)
- Removed@types/pino-pretty@5.0.0(transitive)
- Removed@types/pino-std-serializers@4.0.0(transitive)
- Removedcolorette@2.0.20(transitive)
- Removeddateformat@4.6.3(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedfast-copy@3.0.2(transitive)
- Removedfast-safe-stringify@2.1.1(transitive)
- Removedhelp-me@5.0.0(transitive)
- Removedjoycon@3.1.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedon-exit-leak-free@2.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpino-abstract-transport@2.0.0(transitive)
- Removedpino-pretty@13.0.0(transitive)
- Removedpino-std-serializers@7.0.0(transitive)
- Removedpump@3.0.2(transitive)
- Removedsecure-json-parse@2.7.0(transitive)
- Removedsonic-boom@4.2.0(transitive)
- Removedsplit2@4.2.0(transitive)
- Removedstrip-json-comments@3.1.1(transitive)
- Removedwrappy@1.0.2(transitive)
Updated@types/pino@^7.0.5
Updatedpino@^7.3.0