@cabloy/utils 1.0.31 → 1.0.33
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/cel.d.ts +3 -4
- package/dist/cel.js +23 -6
- package/dist/hash.d.ts +1 -0
- package/dist/hash.js +13 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/match.d.ts +4 -0
- package/dist/match.js +16 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +3 -0
- package/dist/utils.js +21 -3
- package/package.json +5 -4
package/dist/cel.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type * as Celjs from 'cel-js' with { 'resolution-mode': 'import' };
|
|
1
|
+
import type * as Celjs from '@cabloy/cel-js' with { 'resolution-mode': 'import' };
|
|
2
2
|
import type { CstNode } from 'chevrotain';
|
|
3
|
-
export declare
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function evaluate(expression: CstNode | string, context?: Record<string, unknown>, functions?: Record<string, CallableFunction>): unknown;
|
|
3
|
+
export declare function evaluateExpressions<T = any>(expressions: any, context?: object, functions?: Record<string, CallableFunction>, dry?: boolean): T;
|
|
4
|
+
export declare function evaluate<T = any>(expression: CstNode | string, context?: object, functions?: Record<string, CallableFunction>): T;
|
|
6
5
|
export declare function parse(expression: string): Celjs.ParseResult;
|
package/dist/cel.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import * as celjs from 'cel-js';
|
|
1
|
+
import * as celjs from '@cabloy/cel-js';
|
|
2
2
|
import { isNil } from "./check.js";
|
|
3
|
-
|
|
3
|
+
import { hashkey } from "./hash.js";
|
|
4
|
+
import { StringPrefixCel, StringPrefixRaw } from "./types.js";
|
|
5
|
+
import { getProperty } from "./utils.js";
|
|
4
6
|
export function evaluateExpressions(expressions, context, functions, dry) {
|
|
5
7
|
if (isNil(expressions))
|
|
6
8
|
return _returnExpressionWithDry(expressions, dry);
|
|
@@ -24,22 +26,24 @@ function _evaluateExpressionInner(expression, context, functions, dry) {
|
|
|
24
26
|
return evaluateExpressions(expression, context, functions, dry);
|
|
25
27
|
if (typeof expression !== 'string')
|
|
26
28
|
return _returnExpressionWithDry(expression, dry);
|
|
27
|
-
if (!expression.startsWith(
|
|
29
|
+
if (!expression.startsWith(StringPrefixCel))
|
|
28
30
|
return _returnExpressionWithDry(expression, dry);
|
|
29
|
-
return dry ? true : evaluate(expression.substring(
|
|
31
|
+
return dry ? true : evaluate(expression.substring(StringPrefixCel.length), context, functions);
|
|
30
32
|
}
|
|
31
33
|
function _returnExpressionWithDry(expression, dry) {
|
|
32
34
|
return dry ? false : expression;
|
|
33
35
|
}
|
|
34
36
|
export function evaluate(expression, context, functions) {
|
|
37
|
+
// functions
|
|
38
|
+
functions = _prepareFunctions(functions);
|
|
35
39
|
// CstNode
|
|
36
40
|
if (typeof expression === 'object' && expression.children) {
|
|
37
41
|
return celjs.evaluate(expression, context, functions);
|
|
38
42
|
}
|
|
39
43
|
// string
|
|
40
44
|
if (typeof expression === 'string') {
|
|
41
|
-
if (expression.startsWith(
|
|
42
|
-
return expression.substring(
|
|
45
|
+
if (expression.startsWith(StringPrefixRaw)) {
|
|
46
|
+
return expression.substring(StringPrefixRaw.length);
|
|
43
47
|
}
|
|
44
48
|
return celjs.evaluate(expression, context, functions);
|
|
45
49
|
}
|
|
@@ -49,3 +53,16 @@ export function evaluate(expression, context, functions) {
|
|
|
49
53
|
export function parse(expression) {
|
|
50
54
|
return celjs.parse(expression);
|
|
51
55
|
}
|
|
56
|
+
function _prepareFunctions(functions) {
|
|
57
|
+
return Object.assign({
|
|
58
|
+
concat: (...args) => {
|
|
59
|
+
return args.reduce((accumulator, current) => `${accumulator}${current}`, '');
|
|
60
|
+
},
|
|
61
|
+
get: (obj, name, sep) => {
|
|
62
|
+
return getProperty(obj, name, sep);
|
|
63
|
+
},
|
|
64
|
+
hashkey: (key) => {
|
|
65
|
+
return hashkey(key);
|
|
66
|
+
},
|
|
67
|
+
}, functions);
|
|
68
|
+
}
|
package/dist/hash.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function hashkey(key: any): string;
|
package/dist/hash.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import objectHash from 'object-hash';
|
|
2
|
+
export function hashkey(key) {
|
|
3
|
+
if (key === undefined || key === null || key === false) {
|
|
4
|
+
return '';
|
|
5
|
+
}
|
|
6
|
+
if (Array.isArray(key) || typeof key === 'object') {
|
|
7
|
+
return objectHash(key, { respectType: false });
|
|
8
|
+
}
|
|
9
|
+
if (typeof key !== 'string') {
|
|
10
|
+
return String(key);
|
|
11
|
+
}
|
|
12
|
+
return key;
|
|
13
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/match.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type TypeMatchSelectorFunction = (this: any, ...args: any[]) => boolean;
|
|
2
|
+
export type TypeMatchSelectorRule<T> = T | RegExp | TypeMatchSelectorFunction;
|
|
3
|
+
export type TypeMatchSelectorRules<T> = (TypeMatchSelectorRule<T>)[] | TypeMatchSelectorRule<T>;
|
|
4
|
+
export declare function matchSelector<T>(match: TypeMatchSelectorRules<T>, selector: string | boolean, matchThis?: any, ...matchArgs: any[]): any;
|
package/dist/match.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { evaluateExpressions } from "./cel.js";
|
|
2
|
+
import { StringPrefixCel, StringPrefixRegexp } from "./types.js";
|
|
3
|
+
import { evaluateSimple } from "./utils.js";
|
|
4
|
+
export function matchSelector(match, selector, matchThis, ...matchArgs) {
|
|
5
|
+
if (!Array.isArray(match)) {
|
|
6
|
+
// prepare
|
|
7
|
+
if (typeof match === 'string' && match.startsWith(StringPrefixRegexp)) {
|
|
8
|
+
match = evaluateSimple(match.substring(StringPrefixRegexp.length));
|
|
9
|
+
}
|
|
10
|
+
return ((typeof match === 'string' && match.startsWith(StringPrefixCel) && !!evaluateExpressions(match, { selector, context: matchArgs[0], args: matchArgs })) ||
|
|
11
|
+
(typeof match === 'string' && !match.startsWith(StringPrefixCel) && typeof selector === 'string' && match === selector) ||
|
|
12
|
+
(match instanceof RegExp && typeof selector === 'string' && match.test(selector)) ||
|
|
13
|
+
(typeof match === 'function' && match.call(matchThis, selector, ...matchArgs)));
|
|
14
|
+
}
|
|
15
|
+
return match.some(item => matchSelector(item, selector));
|
|
16
|
+
}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
package/dist/utils.js
CHANGED
|
@@ -53,16 +53,25 @@ export function getProperty(obj, name, sep) {
|
|
|
53
53
|
export function getPropertyObject(obj, name, sep) {
|
|
54
54
|
return _getProperty(obj, name, sep, true);
|
|
55
55
|
}
|
|
56
|
+
const __keysIgnore = ['constructor', 'prototype', '__proto__'];
|
|
56
57
|
function _getProperty(_obj, name, sep, forceObject) {
|
|
57
58
|
if (!_obj)
|
|
58
59
|
return undefined;
|
|
59
60
|
let obj = _obj;
|
|
60
61
|
const names = name.split(sep || '.');
|
|
61
62
|
// loop
|
|
62
|
-
for (const
|
|
63
|
-
|
|
63
|
+
for (const _name of names) {
|
|
64
|
+
const [name, index] = _parsePropertyKey(_name);
|
|
65
|
+
if (__keysIgnore.includes(name))
|
|
66
|
+
throw new Error(`invalid prop: ${name}`);
|
|
67
|
+
if (obj[name] === undefined) { // not check obj[name] === null
|
|
64
68
|
if (forceObject) {
|
|
65
|
-
|
|
69
|
+
if (index === undefined) {
|
|
70
|
+
obj[name] = {};
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
obj[name] = [];
|
|
74
|
+
}
|
|
66
75
|
}
|
|
67
76
|
else {
|
|
68
77
|
obj = obj[name];
|
|
@@ -70,9 +79,18 @@ function _getProperty(_obj, name, sep, forceObject) {
|
|
|
70
79
|
}
|
|
71
80
|
}
|
|
72
81
|
obj = obj[name];
|
|
82
|
+
if (index !== undefined) {
|
|
83
|
+
obj = obj[index];
|
|
84
|
+
}
|
|
73
85
|
}
|
|
74
86
|
return obj;
|
|
75
87
|
}
|
|
88
|
+
function _parsePropertyKey(name) {
|
|
89
|
+
const matched = name.match((/([^[]+)\[(\d+)\]/));
|
|
90
|
+
if (!matched)
|
|
91
|
+
return [name, undefined];
|
|
92
|
+
return [matched[1], Number.parseInt(matched[2])];
|
|
93
|
+
}
|
|
76
94
|
export function createFunction(expression, scopeKeys) {
|
|
77
95
|
let fn;
|
|
78
96
|
try {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cabloy/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.33",
|
|
5
5
|
"description": "cabloy utils",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -25,10 +25,11 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@cabloy/
|
|
28
|
+
"@cabloy/cel-js": "^0.7.2",
|
|
29
|
+
"@cabloy/module-info": "^1.3.24",
|
|
29
30
|
"@cabloy/word-utils": "^2.0.1",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
31
|
+
"chevrotain": "^11.0.3",
|
|
32
|
+
"object-hash": "^3.0.0"
|
|
32
33
|
},
|
|
33
34
|
"gitHead": "0eab9dc4a5622caffe89e7b1b3f02c08ccbc4c4b",
|
|
34
35
|
"scripts": {
|