@cabloy/utils 1.0.1 → 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/check.d.ts CHANGED
@@ -13,4 +13,5 @@ export declare const isEmpty: (array: any) => boolean;
13
13
  export declare const isSymbol: (val: any) => val is symbol;
14
14
  export declare function isPromise(obj: any): obj is Promise<any>;
15
15
  export declare function isNilOrEmptyString(str?: string | undefined | null): str is null | undefined | '';
16
+ export declare function checkMeta(meta?: {}, data?: {}): boolean;
16
17
  //# sourceMappingURL=check.d.ts.map
package/dist/check.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNilOrEmptyString = exports.isPromise = exports.isSymbol = exports.isEmpty = exports.isNil = exports.isConstructor = exports.isNumber = exports.isString = exports.isFunction = exports.stripEndSlash = exports.normalizePath = exports.addLeadingSlash = exports.isPlainObject = exports.isObject = exports.isUndefined = void 0;
3
+ exports.checkMeta = exports.isNilOrEmptyString = exports.isPromise = exports.isSymbol = exports.isEmpty = exports.isNil = exports.isConstructor = exports.isNumber = exports.isString = exports.isFunction = exports.stripEndSlash = exports.normalizePath = exports.addLeadingSlash = exports.isPlainObject = exports.isObject = exports.isUndefined = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-use-before-define */
5
5
  const isUndefined = (obj) => typeof obj === 'undefined';
6
6
  exports.isUndefined = isUndefined;
@@ -52,4 +52,22 @@ function isNilOrEmptyString(str) {
52
52
  return str === undefined || str === null || str === '';
53
53
  }
54
54
  exports.isNilOrEmptyString = isNilOrEmptyString;
55
+ function checkMeta(meta, data) {
56
+ // check none
57
+ if (!meta)
58
+ return true;
59
+ // loop
60
+ for (const key in meta) {
61
+ const metaItem = meta[key];
62
+ if ((0, exports.isNil)(metaItem))
63
+ continue;
64
+ if (!Array.isArray(metaItem) && metaItem !== data?.[key])
65
+ return false;
66
+ if (Array.isArray(metaItem) && !metaItem.includes(data?.[key]))
67
+ return false;
68
+ }
69
+ // default
70
+ return true;
71
+ }
72
+ exports.checkMeta = checkMeta;
55
73
  //# sourceMappingURL=check.js.map
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cabloy/utils",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "cabloy utils",
5
5
  "publishConfig": {
6
6
  "access": "public"