@cabloy/utils 1.0.49 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/celjs/base.d.ts +2 -0
- package/dist/celjs/evaluate.d.ts +4 -0
- package/dist/celjs/index.d.ts +4 -0
- package/dist/celjs/utils.d.ts +5 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +93 -81
- package/dist/utils.d.ts +0 -2
- package/package.json +2 -3
- package/dist/cel.d.ts +0 -7
- /package/dist/{types.d.ts → celjs/types.d.ts} +0 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Environment, ParseResult } from '@marcbachmann/cel-js';
|
|
2
|
+
export declare function evaluateExpressions<T = any>(expressions: any, context?: object, celEnv?: Environment, dry?: boolean): T;
|
|
3
|
+
export declare function evaluate<T = any>(expression: string, context?: object, celEnv?: Environment): T;
|
|
4
|
+
export declare function parse(expression: string, celEnv?: Environment): ParseResult;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function regexp(str: string): string;
|
|
2
|
+
export declare function cel(str: string): string;
|
|
3
|
+
export declare function raw(str: string): string;
|
|
4
|
+
export declare function createFunction(expression: string, scopeKeys?: string[]): Function;
|
|
5
|
+
export declare function evaluateSimple(expression: string, scope?: object): any;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './celjs/index.ts';
|
|
2
2
|
export * from './check.ts';
|
|
3
3
|
export * from './hash.ts';
|
|
4
4
|
export * from './match.ts';
|
|
5
|
-
export * from './types.ts';
|
|
6
5
|
export * from './utils.ts';
|
|
7
6
|
export * from './vona.ts';
|
|
8
7
|
export * from './zod.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Environment } from '@marcbachmann/cel-js';
|
|
2
2
|
import objectHash from 'object-hash';
|
|
3
3
|
import { toLowerCaseFirstChar, stringToCapitalize } from '@cabloy/word-utils';
|
|
4
4
|
|
|
@@ -63,25 +63,6 @@ function safeBoolean(value) {
|
|
|
63
63
|
return Boolean(value);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
function hashkey(key) {
|
|
67
|
-
if (key === undefined || key === null) {
|
|
68
|
-
return '';
|
|
69
|
-
}
|
|
70
|
-
if (Array.isArray(key) || typeof key === 'object') {
|
|
71
|
-
return objectHash(key, {
|
|
72
|
-
respectType: false
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
if (typeof key !== 'string') {
|
|
76
|
-
return String(key);
|
|
77
|
-
}
|
|
78
|
-
return key;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const StringPrefixRegexp = 'regexp://';
|
|
82
|
-
const StringPrefixCel = 'cel://';
|
|
83
|
-
const StringPrefixRaw = 'raw://';
|
|
84
|
-
|
|
85
66
|
function deprecated(oldUsage, newUsage) {
|
|
86
67
|
const message = '`'.concat(oldUsage, '` is deprecated and will be removed in a later version. Use `').concat(newUsage, '` instead');
|
|
87
68
|
console.warn(message);
|
|
@@ -175,22 +156,6 @@ function _parsePropertyKey(name) {
|
|
|
175
156
|
if (!matched) return [name, undefined];
|
|
176
157
|
return [matched[1], Number.parseInt(matched[2])];
|
|
177
158
|
}
|
|
178
|
-
function createFunction(expression, scopeKeys) {
|
|
179
|
-
let fn;
|
|
180
|
-
try {
|
|
181
|
-
const js = `return (${expression})`;
|
|
182
|
-
fn = scopeKeys && scopeKeys.length > 0 ? new Function(scopeKeys.join(','), js) : new Function(js);
|
|
183
|
-
} catch (_err) {
|
|
184
|
-
fn = scopeKeys && scopeKeys.length > 0 ? new Function(scopeKeys.join(','), expression) : new Function(expression);
|
|
185
|
-
}
|
|
186
|
-
return fn;
|
|
187
|
-
}
|
|
188
|
-
function evaluateSimple(expression, scope) {
|
|
189
|
-
const scopeKeys = scope ? Object.keys(scope) : undefined;
|
|
190
|
-
const scopeValues = scope ? Object.values(scope) : undefined;
|
|
191
|
-
const fn = createFunction(expression, scopeKeys);
|
|
192
|
-
return scopeValues ? fn(...scopeValues) : fn();
|
|
193
|
-
}
|
|
194
159
|
function getRandomInt(size, start = 0) {
|
|
195
160
|
return Math.floor(Math.random() * size) + start;
|
|
196
161
|
}
|
|
@@ -207,7 +172,8 @@ function combineQueries(pagePath, queries) {
|
|
|
207
172
|
if (queries) {
|
|
208
173
|
for (const key in queries) {
|
|
209
174
|
const value = queries[key];
|
|
210
|
-
if (value
|
|
175
|
+
if (isNil(value)) continue;
|
|
176
|
+
if (typeof value === 'object') {
|
|
211
177
|
query2.push([key, value]);
|
|
212
178
|
} else {
|
|
213
179
|
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
@@ -255,72 +221,117 @@ function stringLazy(fn) {
|
|
|
255
221
|
};
|
|
256
222
|
}
|
|
257
223
|
|
|
224
|
+
const celEnvBase = new Environment({
|
|
225
|
+
unlistedVariablesAreDyn: true,
|
|
226
|
+
enableOptionalTypes: true,
|
|
227
|
+
homogeneousAggregateLiterals: false
|
|
228
|
+
});
|
|
229
|
+
const params = [];
|
|
230
|
+
for (let i = 0; i < 10; i++) {
|
|
231
|
+
params.push('dyn');
|
|
232
|
+
celEnvBase.registerFunction(`concat(${params.join(',')}):list`, _concat);
|
|
233
|
+
}
|
|
234
|
+
celEnvBase.registerFunction('join(list):string', list => {
|
|
235
|
+
return _join(list);
|
|
236
|
+
});
|
|
237
|
+
celEnvBase.registerFunction('join(list,string):string', (list, sep) => {
|
|
238
|
+
return _join(list, sep);
|
|
239
|
+
});
|
|
240
|
+
celEnvBase.registerFunction('get(map,string):dyn', (obj, name) => {
|
|
241
|
+
return getProperty(obj, name);
|
|
242
|
+
});
|
|
243
|
+
celEnvBase.registerFunction('get(map,string,string):dyn', (obj, name, sep) => {
|
|
244
|
+
return getProperty(obj, name, sep);
|
|
245
|
+
});
|
|
246
|
+
celEnvBase.registerOperator('string + int', (str, n) => str + String(n));
|
|
247
|
+
celEnvBase.registerOperator('int + string', (n, str) => String(n) + str);
|
|
248
|
+
function _concat(...args) {
|
|
249
|
+
return [].concat(...args);
|
|
250
|
+
}
|
|
251
|
+
function _join(list, sep) {
|
|
252
|
+
if (!list) return '';
|
|
253
|
+
return list.join(sep);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const StringPrefixRegexp = 'regexp://';
|
|
257
|
+
const StringPrefixCel = 'cel://';
|
|
258
|
+
const StringPrefixRaw = 'raw://';
|
|
259
|
+
|
|
260
|
+
function regexp(str) {
|
|
261
|
+
return `${StringPrefixRegexp}${str}`;
|
|
262
|
+
}
|
|
258
263
|
function cel(str) {
|
|
259
264
|
return `${StringPrefixCel}${str}`;
|
|
260
265
|
}
|
|
261
266
|
function raw(str) {
|
|
262
267
|
return `${StringPrefixRaw}${str}`;
|
|
263
268
|
}
|
|
264
|
-
function
|
|
269
|
+
function createFunction(expression, scopeKeys) {
|
|
270
|
+
let fn;
|
|
271
|
+
try {
|
|
272
|
+
const js = `return (${expression})`;
|
|
273
|
+
fn = scopeKeys && scopeKeys.length > 0 ? new Function(scopeKeys.join(','), js) : new Function(js);
|
|
274
|
+
} catch (_err) {
|
|
275
|
+
fn = scopeKeys && scopeKeys.length > 0 ? new Function(scopeKeys.join(','), expression) : new Function(expression);
|
|
276
|
+
}
|
|
277
|
+
return fn;
|
|
278
|
+
}
|
|
279
|
+
function evaluateSimple(expression, scope) {
|
|
280
|
+
const scopeKeys = scope ? Object.keys(scope) : undefined;
|
|
281
|
+
const scopeValues = scope ? Object.values(scope) : undefined;
|
|
282
|
+
const fn = createFunction(expression, scopeKeys);
|
|
283
|
+
return scopeValues ? fn(...scopeValues) : fn();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function evaluateExpressions(expressions, context, celEnv, dry) {
|
|
265
287
|
if (isNil(expressions)) return _returnExpressionWithDry(expressions, dry);
|
|
266
288
|
if (Array.isArray(expressions)) {
|
|
267
|
-
return expressions.map(item => _evaluateExpressionInner(item, context,
|
|
289
|
+
return expressions.map(item => _evaluateExpressionInner(item, context, celEnv, dry));
|
|
268
290
|
} else if (typeof expressions === 'object') {
|
|
269
291
|
const res = {};
|
|
270
292
|
for (const key in expressions) {
|
|
271
|
-
res[key] = _evaluateExpressionInner(expressions[key], context,
|
|
293
|
+
res[key] = _evaluateExpressionInner(expressions[key], context, celEnv, dry);
|
|
272
294
|
}
|
|
273
295
|
return res;
|
|
274
296
|
}
|
|
275
297
|
// others
|
|
276
|
-
return _evaluateExpressionInner(expressions, context,
|
|
298
|
+
return _evaluateExpressionInner(expressions, context, celEnv, dry);
|
|
277
299
|
}
|
|
278
|
-
function _evaluateExpressionInner(expression, context,
|
|
300
|
+
function _evaluateExpressionInner(expression, context, celEnv, dry) {
|
|
279
301
|
if (isNil(expression)) return _returnExpressionWithDry(expression, dry);
|
|
280
|
-
if (typeof expression === 'object') return evaluateExpressions(expression, context,
|
|
302
|
+
if (typeof expression === 'object') return evaluateExpressions(expression, context, celEnv, dry);
|
|
281
303
|
if (typeof expression !== 'string') return _returnExpressionWithDry(expression, dry);
|
|
282
304
|
if (!expression.startsWith(StringPrefixCel)) return _returnExpressionWithDry(expression, dry);
|
|
283
|
-
return dry ? true : evaluate(expression.substring(StringPrefixCel.length), context,
|
|
305
|
+
return dry ? true : evaluate(expression.substring(StringPrefixCel.length), context, celEnv);
|
|
284
306
|
}
|
|
285
307
|
function _returnExpressionWithDry(expression, dry) {
|
|
286
308
|
return dry ? false : expression;
|
|
287
309
|
}
|
|
288
|
-
function evaluate(expression, context,
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
return celjs.evaluate(expression, context, functions);
|
|
310
|
+
function evaluate(expression, context, celEnv) {
|
|
311
|
+
if (expression.startsWith(StringPrefixRaw)) {
|
|
312
|
+
return expression.substring(StringPrefixRaw.length);
|
|
313
|
+
} else if (expression.startsWith(StringPrefixRegexp)) {
|
|
314
|
+
return evaluateSimple(expression.substring(StringPrefixRegexp.length));
|
|
294
315
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
316
|
+
return (celEnv ?? celEnvBase).evaluate(expression, context);
|
|
317
|
+
}
|
|
318
|
+
function parse(expression, celEnv) {
|
|
319
|
+
return (celEnv ?? celEnvBase).parse(expression);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function hashkey(key) {
|
|
323
|
+
if (key === undefined || key === null) {
|
|
324
|
+
return '';
|
|
301
325
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return [].concat(...args);
|
|
312
|
-
},
|
|
313
|
-
join: (arr, sep) => {
|
|
314
|
-
if (!arr) return '';
|
|
315
|
-
return arr.join(sep);
|
|
316
|
-
},
|
|
317
|
-
get: (obj, name, sep) => {
|
|
318
|
-
return getProperty(obj, name, sep);
|
|
319
|
-
},
|
|
320
|
-
hashkey: key => {
|
|
321
|
-
return hashkey(key);
|
|
322
|
-
}
|
|
323
|
-
}, functions);
|
|
326
|
+
if (Array.isArray(key) || typeof key === 'object') {
|
|
327
|
+
return objectHash(key, {
|
|
328
|
+
respectType: false
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
if (typeof key !== 'string') {
|
|
332
|
+
return String(key);
|
|
333
|
+
}
|
|
334
|
+
return key;
|
|
324
335
|
}
|
|
325
336
|
|
|
326
337
|
function matchSelector(match, selector, matchThis, ...matchArgs) {
|
|
@@ -331,8 +342,9 @@ function matchSelector(match, selector, matchThis, ...matchArgs) {
|
|
|
331
342
|
}
|
|
332
343
|
return typeof match === 'string' && match.startsWith(StringPrefixCel) && !!evaluateExpressions(match, {
|
|
333
344
|
selector,
|
|
334
|
-
context: matchArgs[0]
|
|
335
|
-
|
|
345
|
+
context: matchArgs[0] && typeof matchArgs[0] === 'object' ? {
|
|
346
|
+
...matchArgs[0]
|
|
347
|
+
} : matchArgs[0]
|
|
336
348
|
}) || typeof match === 'string' && !match.startsWith(StringPrefixCel) && typeof selector === 'string' && match === selector || match instanceof RegExp && typeof selector === 'string' && match.test(selector) || typeof match === 'function' && match.call(matchThis, selector, ...matchArgs);
|
|
337
349
|
}
|
|
338
350
|
return match.some(item => matchSelector(item, selector));
|
|
@@ -413,4 +425,4 @@ function zodCustomError(path, message) {
|
|
|
413
425
|
return error;
|
|
414
426
|
}
|
|
415
427
|
|
|
416
|
-
export { StringPrefixCel, StringPrefixRaw, StringPrefixRegexp, addLeadingSlash, catchError, catchErrorSync, cel, checkMeta, combineApiPath, combineApiPathControllerAndAction, combineApiPathControllerAndActionRaw, combineParamsAndQuery, combineQueries, combineResourceName, combineResourceNameParts, createFunction, defaultPathSerializer, deprecated, ensureArray, evaluate, evaluateExpressions, evaluateSimple, getProperty, getPropertyObject, getRandomInt, hashkey, isClass, isClassStrict, isConstructor, isEmpty, isEmptyObject, isFunction, isNil, isNilOrEmptyString, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isUndefined, matchSelector, normalizePath, parse, raw, replaceTemplate, safeBoolean, setProperty, sleep, stringLazy, stripEndSlash, zodCustomError };
|
|
428
|
+
export { StringPrefixCel, StringPrefixRaw, StringPrefixRegexp, addLeadingSlash, catchError, catchErrorSync, cel, celEnvBase, checkMeta, combineApiPath, combineApiPathControllerAndAction, combineApiPathControllerAndActionRaw, combineParamsAndQuery, combineQueries, combineResourceName, combineResourceNameParts, createFunction, defaultPathSerializer, deprecated, ensureArray, evaluate, evaluateExpressions, evaluateSimple, getProperty, getPropertyObject, getRandomInt, hashkey, isClass, isClassStrict, isConstructor, isEmpty, isEmptyObject, isFunction, isNil, isNilOrEmptyString, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isUndefined, matchSelector, normalizePath, parse, raw, regexp, replaceTemplate, safeBoolean, setProperty, sleep, stringLazy, stripEndSlash, zodCustomError };
|
package/dist/utils.d.ts
CHANGED
|
@@ -6,8 +6,6 @@ export declare function replaceTemplate(content: string, scope: object): string;
|
|
|
6
6
|
export declare function setProperty<T>(obj: object, name: string, value: T): void;
|
|
7
7
|
export declare function getProperty<T>(obj: object | undefined, name: string, sep?: string): T | undefined;
|
|
8
8
|
export declare function getPropertyObject<T>(obj: object | undefined, name: string, sep?: string): T | undefined;
|
|
9
|
-
export declare function createFunction(expression: string, scopeKeys?: string[]): Function;
|
|
10
|
-
export declare function evaluateSimple(expression: string, scope?: object): any;
|
|
11
9
|
export declare function getRandomInt(size: number, start?: number): number;
|
|
12
10
|
export declare function combineParamsAndQuery(path: string, options?: {
|
|
13
11
|
params?: object;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cabloy/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.1",
|
|
5
5
|
"description": "cabloy utils",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -25,10 +25,9 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@cabloy/cel-js": "^0.7.4",
|
|
29
28
|
"@cabloy/module-info": "^1.3.32",
|
|
30
29
|
"@cabloy/word-utils": "^2.0.1",
|
|
31
|
-
"
|
|
30
|
+
"@marcbachmann/cel-js": "^6.4.0",
|
|
32
31
|
"object-hash": "^3.0.0"
|
|
33
32
|
},
|
|
34
33
|
"gitHead": "0eab9dc4a5622caffe89e7b1b3f02c08ccbc4c4b",
|
package/dist/cel.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type * as Celjs from '@cabloy/cel-js' with { 'resolution-mode': 'import' };
|
|
2
|
-
import type { CstNode } from 'chevrotain';
|
|
3
|
-
export declare function cel(str: string): string;
|
|
4
|
-
export declare function raw(str: string): string;
|
|
5
|
-
export declare function evaluateExpressions<T = any>(expressions: any, context?: object, functions?: Record<string, CallableFunction>, dry?: boolean): T;
|
|
6
|
-
export declare function evaluate<T = any>(expression: CstNode | string, context?: object, functions?: Record<string, CallableFunction>): T;
|
|
7
|
-
export declare function parse(expression: string): Celjs.ParseResult;
|
|
File without changes
|