@cabloy/utils 1.0.3 → 1.0.4
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 -0
- package/dist/cel.js +12 -0
- package/dist/check.d.ts +5 -5
- package/dist/check.js +32 -44
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -19
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +25 -39
- package/package.json +13 -17
package/dist/cel.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type * as CelJS from 'cel-js' with { 'resolution-mode': 'import' };
|
|
2
|
+
export declare function evaluate(expression: string, context?: Record<string, unknown>, functions?: Record<string, CallableFunction>): unknown;
|
|
3
|
+
export declare function parse(expression: string): CelJS.ParseResult;
|
package/dist/cel.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as celjs from 'cel-js';
|
|
2
|
+
export function evaluate(expression, context, functions) {
|
|
3
|
+
if (typeof expression !== 'string')
|
|
4
|
+
return expression;
|
|
5
|
+
if (expression.startsWith('##')) {
|
|
6
|
+
return expression.substring('##'.length);
|
|
7
|
+
}
|
|
8
|
+
return celjs.evaluate(expression, context, functions);
|
|
9
|
+
}
|
|
10
|
+
export function parse(expression) {
|
|
11
|
+
return celjs.parse(expression);
|
|
12
|
+
}
|
package/dist/check.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
export declare const isUndefined: (obj: any) => obj is undefined;
|
|
2
|
+
export declare const isNil: (val: any) => val is null | undefined;
|
|
2
3
|
export declare const isObject: (fn: any) => fn is object;
|
|
3
|
-
export declare
|
|
4
|
-
export declare
|
|
5
|
-
export declare
|
|
4
|
+
export declare function isEmptyObject(obj: any): boolean;
|
|
5
|
+
export declare function isPlainObject(fn: any): fn is object;
|
|
6
|
+
export declare function addLeadingSlash(path?: string): string;
|
|
7
|
+
export declare function normalizePath(path?: string): string;
|
|
6
8
|
export declare const stripEndSlash: (path: string) => string;
|
|
7
9
|
export declare const isFunction: (val: any) => val is Function;
|
|
8
10
|
export declare const isString: (val: any) => val is string;
|
|
9
11
|
export declare const isNumber: (val: any) => val is number;
|
|
10
12
|
export declare const isConstructor: (val: any) => boolean;
|
|
11
|
-
export declare const isNil: (val: any) => val is null | undefined;
|
|
12
13
|
export declare const isEmpty: (array: any) => boolean;
|
|
13
14
|
export declare const isSymbol: (val: any) => val is symbol;
|
|
14
15
|
export declare function isPromise(obj: any): obj is Promise<any>;
|
|
15
16
|
export declare function isNilOrEmptyString(str?: string | undefined | null): str is null | undefined | '';
|
|
16
17
|
export declare function checkMeta(meta?: {}, data?: {}): boolean;
|
|
17
|
-
//# sourceMappingURL=check.d.ts.map
|
package/dist/check.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (!
|
|
1
|
+
export const isUndefined = (obj) => typeof obj === 'undefined';
|
|
2
|
+
export const isNil = (val) => isUndefined(val) || val === null;
|
|
3
|
+
export const isObject = (fn) => !isNil(fn) && typeof fn === 'object';
|
|
4
|
+
export function isEmptyObject(obj) {
|
|
5
|
+
if (!obj)
|
|
6
|
+
return true;
|
|
7
|
+
return Object.keys(obj).length === 0;
|
|
8
|
+
}
|
|
9
|
+
export function isPlainObject(fn) {
|
|
10
|
+
if (!isObject(fn)) {
|
|
11
11
|
return false;
|
|
12
12
|
}
|
|
13
13
|
const proto = Object.getPrototypeOf(fn);
|
|
@@ -18,48 +18,38 @@ const isPlainObject = (fn) => {
|
|
|
18
18
|
return (typeof ctor === 'function' &&
|
|
19
19
|
ctor instanceof ctor &&
|
|
20
20
|
Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
?
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const isFunction = (val) => typeof val === 'function';
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
exports.isConstructor = isConstructor;
|
|
41
|
-
const isNil = (val) => (0, exports.isUndefined)(val) || val === null;
|
|
42
|
-
exports.isNil = isNil;
|
|
43
|
-
const isEmpty = (array) => !(array && array.length > 0);
|
|
44
|
-
exports.isEmpty = isEmpty;
|
|
45
|
-
const isSymbol = (val) => typeof val === 'symbol';
|
|
46
|
-
exports.isSymbol = isSymbol;
|
|
47
|
-
function isPromise(obj) {
|
|
21
|
+
}
|
|
22
|
+
export function addLeadingSlash(path) {
|
|
23
|
+
return path && typeof path === 'string' ? (path.charAt(0) !== '/' ? `/${path}` : path) : '';
|
|
24
|
+
}
|
|
25
|
+
export function normalizePath(path) {
|
|
26
|
+
return path
|
|
27
|
+
? path.startsWith('/')
|
|
28
|
+
? (`/${path.replace(/\/+$/, '')}`).replace(/\/+/g, '/')
|
|
29
|
+
: `/${path.replace(/\/+$/, '')}`
|
|
30
|
+
: '/';
|
|
31
|
+
}
|
|
32
|
+
export const stripEndSlash = (path) => (path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path);
|
|
33
|
+
export const isFunction = (val) => typeof val === 'function';
|
|
34
|
+
export const isString = (val) => typeof val === 'string';
|
|
35
|
+
export const isNumber = (val) => typeof val === 'number';
|
|
36
|
+
export const isConstructor = (val) => val === 'constructor';
|
|
37
|
+
export const isEmpty = (array) => !(array && array.length > 0);
|
|
38
|
+
export const isSymbol = (val) => typeof val === 'symbol';
|
|
39
|
+
export function isPromise(obj) {
|
|
48
40
|
return obj instanceof Promise || (obj && typeof obj.then === 'function');
|
|
49
41
|
}
|
|
50
|
-
|
|
51
|
-
function isNilOrEmptyString(str) {
|
|
42
|
+
export function isNilOrEmptyString(str) {
|
|
52
43
|
return str === undefined || str === null || str === '';
|
|
53
44
|
}
|
|
54
|
-
|
|
55
|
-
function checkMeta(meta, data) {
|
|
45
|
+
export function checkMeta(meta, data) {
|
|
56
46
|
// check none
|
|
57
47
|
if (!meta)
|
|
58
48
|
return true;
|
|
59
49
|
// loop
|
|
60
50
|
for (const key in meta) {
|
|
61
51
|
const metaItem = meta[key];
|
|
62
|
-
if (
|
|
52
|
+
if (isNil(metaItem))
|
|
63
53
|
continue;
|
|
64
54
|
if (!Array.isArray(metaItem) && metaItem !== data?.[key])
|
|
65
55
|
return false;
|
|
@@ -69,5 +59,3 @@ function checkMeta(meta, data) {
|
|
|
69
59
|
// default
|
|
70
60
|
return true;
|
|
71
61
|
}
|
|
72
|
-
exports.checkMeta = checkMeta;
|
|
73
|
-
//# sourceMappingURL=check.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
|
|
1
|
+
export * from './cel.ts';
|
|
2
|
+
export * from './check.ts';
|
|
3
|
+
export * from './utils.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./check.js"), exports);
|
|
18
|
-
__exportStar(require("./utils.js"), exports);
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export * from "./cel.js";
|
|
2
|
+
export * from "./check.js";
|
|
3
|
+
export * from "./utils.js";
|
package/dist/utils.d.ts
CHANGED
|
@@ -5,6 +5,4 @@ export declare function replaceTemplate(content: string, scope: object): string;
|
|
|
5
5
|
export declare function setProperty<T>(obj: object, name: string, value: T): void;
|
|
6
6
|
export declare function getProperty<T>(obj: object, name: string, sep?: string): T | undefined;
|
|
7
7
|
export declare function getPropertyObject<T>(obj: object, name: string, sep?: string): T | undefined;
|
|
8
|
-
export declare function evaluate(expression: string, scope?: object): any;
|
|
9
8
|
export declare function createFunction(expression: string, scopeKeys?: string[]): Function;
|
|
10
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createFunction = exports.evaluate = exports.getPropertyObject = exports.getProperty = exports.setProperty = exports.replaceTemplate = exports.sleep = exports.catchError = exports.deprecated = void 0;
|
|
4
|
-
function deprecated(oldUsage, newUsage) {
|
|
1
|
+
export function deprecated(oldUsage, newUsage) {
|
|
5
2
|
const message = '`'
|
|
6
3
|
.concat(oldUsage, '` is deprecated and will be removed in a later version. Use `')
|
|
7
4
|
.concat(newUsage, '` instead');
|
|
8
|
-
|
|
5
|
+
console.warn(message);
|
|
9
6
|
}
|
|
10
|
-
|
|
11
|
-
async function catchError(fnMethod) {
|
|
7
|
+
export async function catchError(fnMethod) {
|
|
12
8
|
let error;
|
|
13
9
|
let data;
|
|
14
10
|
try {
|
|
@@ -19,15 +15,13 @@ async function catchError(fnMethod) {
|
|
|
19
15
|
}
|
|
20
16
|
return error ? [undefined, error] : [data, undefined];
|
|
21
17
|
}
|
|
22
|
-
|
|
23
|
-
async function sleep(ms) {
|
|
18
|
+
export async function sleep(ms) {
|
|
24
19
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
25
20
|
}
|
|
26
|
-
|
|
27
|
-
function replaceTemplate(content, scope) {
|
|
21
|
+
export function replaceTemplate(content, scope) {
|
|
28
22
|
if (!content)
|
|
29
23
|
return content;
|
|
30
|
-
return content.toString().replace(/(\\)
|
|
24
|
+
return content.toString().replace(/(\\)?\{\{ *([\w.]+) *\}\}/g, (block, skip, key) => {
|
|
31
25
|
if (skip) {
|
|
32
26
|
return block.substring(skip.length);
|
|
33
27
|
}
|
|
@@ -35,8 +29,7 @@ function replaceTemplate(content, scope) {
|
|
|
35
29
|
return value !== undefined ? value : '';
|
|
36
30
|
});
|
|
37
31
|
}
|
|
38
|
-
|
|
39
|
-
function setProperty(obj, name, value) {
|
|
32
|
+
export function setProperty(obj, name, value) {
|
|
40
33
|
const names = name.split('.');
|
|
41
34
|
if (names.length === 1) {
|
|
42
35
|
obj[name] = value;
|
|
@@ -54,15 +47,12 @@ function setProperty(obj, name, value) {
|
|
|
54
47
|
obj[names[names.length - 1]] = value;
|
|
55
48
|
}
|
|
56
49
|
}
|
|
57
|
-
|
|
58
|
-
function getProperty(obj, name, sep) {
|
|
50
|
+
export function getProperty(obj, name, sep) {
|
|
59
51
|
return _getProperty(obj, name, sep, false);
|
|
60
52
|
}
|
|
61
|
-
|
|
62
|
-
function getPropertyObject(obj, name, sep) {
|
|
53
|
+
export function getPropertyObject(obj, name, sep) {
|
|
63
54
|
return _getProperty(obj, name, sep, true);
|
|
64
55
|
}
|
|
65
|
-
exports.getPropertyObject = getPropertyObject;
|
|
66
56
|
function _getProperty(obj, name, sep, forceObject) {
|
|
67
57
|
if (!obj)
|
|
68
58
|
return undefined;
|
|
@@ -82,20 +72,7 @@ function _getProperty(obj, name, sep, forceObject) {
|
|
|
82
72
|
}
|
|
83
73
|
return obj;
|
|
84
74
|
}
|
|
85
|
-
function
|
|
86
|
-
if (!scope)
|
|
87
|
-
return _evaluateSimple(expression);
|
|
88
|
-
const scopeKeys = Object.keys(scope);
|
|
89
|
-
const scopeParams = [];
|
|
90
|
-
for (let i = 0; i < scopeKeys.length; i++) {
|
|
91
|
-
const key = scopeKeys[i];
|
|
92
|
-
scopeParams.push(scope[key]);
|
|
93
|
-
}
|
|
94
|
-
const fn = createFunction(expression, scopeKeys);
|
|
95
|
-
return fn(...scopeParams);
|
|
96
|
-
}
|
|
97
|
-
exports.evaluate = evaluate;
|
|
98
|
-
function createFunction(expression, scopeKeys) {
|
|
75
|
+
export function createFunction(expression, scopeKeys) {
|
|
99
76
|
let fn;
|
|
100
77
|
try {
|
|
101
78
|
const js = `return (${expression})`;
|
|
@@ -106,9 +83,18 @@ function createFunction(expression, scopeKeys) {
|
|
|
106
83
|
}
|
|
107
84
|
return fn;
|
|
108
85
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
86
|
+
// export function evaluate(expression: string, scope?: object) {
|
|
87
|
+
// if (!scope) return _evaluateSimple(expression);
|
|
88
|
+
// const scopeKeys = Object.keys(scope);
|
|
89
|
+
// const scopeParams: any[] = [];
|
|
90
|
+
// for (let i = 0; i < scopeKeys.length; i++) {
|
|
91
|
+
// const key = scopeKeys[i];
|
|
92
|
+
// scopeParams.push(scope[key]);
|
|
93
|
+
// }
|
|
94
|
+
// const fn = createFunction(expression, scopeKeys);
|
|
95
|
+
// return fn(...scopeParams);
|
|
96
|
+
// }
|
|
97
|
+
// function _evaluateSimple(expression: string) {
|
|
98
|
+
// const fn = createFunction(expression);
|
|
99
|
+
// return fn();
|
|
100
|
+
// }
|
package/package.json
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cabloy/utils",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.4",
|
|
4
5
|
"description": "cabloy utils",
|
|
5
6
|
"publishConfig": {
|
|
6
7
|
"access": "public"
|
|
7
8
|
},
|
|
9
|
+
"author": "zhennann",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"framework",
|
|
12
|
+
"cabloy"
|
|
13
|
+
],
|
|
8
14
|
"exports": {
|
|
9
15
|
".": {
|
|
10
16
|
"types": [
|
|
11
|
-
"./
|
|
12
|
-
"./
|
|
17
|
+
"./src/index.ts",
|
|
18
|
+
"./dist/index.d.ts"
|
|
13
19
|
],
|
|
14
|
-
"
|
|
15
|
-
"require": "./dist/index.js",
|
|
16
|
-
"default": "./src/index.ts"
|
|
20
|
+
"default": "./dist/index.js"
|
|
17
21
|
},
|
|
18
22
|
"./package.json": "./package.json"
|
|
19
23
|
},
|
|
20
24
|
"files": [
|
|
21
|
-
"dist
|
|
22
|
-
"dist/**/*.d.ts"
|
|
25
|
+
"dist"
|
|
23
26
|
],
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"egg-born",
|
|
27
|
-
"framework",
|
|
28
|
-
"cabloy"
|
|
29
|
-
],
|
|
30
|
-
"author": "zhennann",
|
|
31
|
-
"scripts": {
|
|
32
|
-
"lint": "eslint ."
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"cel-js": "^0.3.1"
|
|
33
29
|
}
|
|
34
30
|
}
|