@cabloy/utils 1.0.2 → 1.0.3
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/utils.d.ts +2 -0
- package/dist/utils.js +30 -1
- package/package.json +1 -1
package/dist/utils.d.ts
CHANGED
|
@@ -5,4 +5,6 @@ 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
|
+
export declare function createFunction(expression: string, scopeKeys?: string[]): Function;
|
|
8
10
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPropertyObject = exports.getProperty = exports.setProperty = exports.replaceTemplate = exports.sleep = exports.catchError = exports.deprecated = void 0;
|
|
3
|
+
exports.createFunction = exports.evaluate = exports.getPropertyObject = exports.getProperty = exports.setProperty = exports.replaceTemplate = exports.sleep = exports.catchError = exports.deprecated = void 0;
|
|
4
4
|
function deprecated(oldUsage, newUsage) {
|
|
5
5
|
const message = '`'
|
|
6
6
|
.concat(oldUsage, '` is deprecated and will be removed in a later version. Use `')
|
|
@@ -82,4 +82,33 @@ function _getProperty(obj, name, sep, forceObject) {
|
|
|
82
82
|
}
|
|
83
83
|
return obj;
|
|
84
84
|
}
|
|
85
|
+
function evaluate(expression, scope) {
|
|
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) {
|
|
99
|
+
let fn;
|
|
100
|
+
try {
|
|
101
|
+
const js = `return (${expression})`;
|
|
102
|
+
fn = scopeKeys && scopeKeys.length > 0 ? new Function(scopeKeys.join(','), js) : new Function(js);
|
|
103
|
+
}
|
|
104
|
+
catch (_err) {
|
|
105
|
+
fn = scopeKeys && scopeKeys.length > 0 ? new Function(scopeKeys.join(','), expression) : new Function(expression);
|
|
106
|
+
}
|
|
107
|
+
return fn;
|
|
108
|
+
}
|
|
109
|
+
exports.createFunction = createFunction;
|
|
110
|
+
function _evaluateSimple(expression) {
|
|
111
|
+
const fn = createFunction(expression);
|
|
112
|
+
return fn();
|
|
113
|
+
}
|
|
85
114
|
//# sourceMappingURL=utils.js.map
|