@cabloy/utils 1.0.3 → 1.0.5

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 ADDED
@@ -0,0 +1,4 @@
1
+ import type * as CelJS from 'cel-js' with { 'resolution-mode': 'import' };
2
+ export declare function evaluateExpressions(expressions: any, context?: Record<string, unknown>, functions?: Record<string, CallableFunction>): unknown;
3
+ export declare function evaluate(expression: string, context?: Record<string, unknown>, functions?: Record<string, CallableFunction>): unknown;
4
+ export declare function parse(expression: string): CelJS.ParseResult;
package/dist/cel.js ADDED
@@ -0,0 +1,29 @@
1
+ import * as celjs from 'cel-js';
2
+ import { isNil } from "./check.js";
3
+ export function evaluateExpressions(expressions, context, functions) {
4
+ if (isNil(expressions))
5
+ return expressions;
6
+ if (Array.isArray(expressions)) {
7
+ return expressions.map(item => evaluate(item, context, functions));
8
+ }
9
+ else if (typeof expressions === 'object') {
10
+ const res = {};
11
+ for (const key in expressions) {
12
+ res[key] = evaluate(expressions[key], context, functions);
13
+ }
14
+ return res;
15
+ }
16
+ // others
17
+ return evaluate(expressions, context, functions);
18
+ }
19
+ export function evaluate(expression, context, functions) {
20
+ if (typeof expression !== 'string')
21
+ return expression;
22
+ if (expression.startsWith('##')) {
23
+ return expression.substring('##'.length);
24
+ }
25
+ return celjs.evaluate(expression, context, functions);
26
+ }
27
+ export function parse(expression) {
28
+ return celjs.parse(expression);
29
+ }
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 const isPlainObject: (fn: any) => fn is object;
4
- export declare const addLeadingSlash: (path?: string) => string;
5
- export declare const normalizePath: (path?: string) => string;
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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
- /* eslint-disable @typescript-eslint/no-use-before-define */
5
- const isUndefined = (obj) => typeof obj === 'undefined';
6
- exports.isUndefined = isUndefined;
7
- const isObject = (fn) => !(0, exports.isNil)(fn) && typeof fn === 'object';
8
- exports.isObject = isObject;
9
- const isPlainObject = (fn) => {
10
- if (!(0, exports.isObject)(fn)) {
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
- exports.isPlainObject = isPlainObject;
23
- const addLeadingSlash = (path) => path && typeof path === 'string' ? (path.charAt(0) !== '/' ? '/' + path : path) : '';
24
- exports.addLeadingSlash = addLeadingSlash;
25
- const normalizePath = (path) => path
26
- ? path.startsWith('/')
27
- ? ('/' + path.replace(/\/+$/, '')).replace(/\/+/g, '/')
28
- : '/' + path.replace(/\/+$/, '')
29
- : '/';
30
- exports.normalizePath = normalizePath;
31
- const stripEndSlash = (path) => (path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path);
32
- exports.stripEndSlash = stripEndSlash;
33
- const isFunction = (val) => typeof val === 'function';
34
- exports.isFunction = isFunction;
35
- const isString = (val) => typeof val === 'string';
36
- exports.isString = isString;
37
- const isNumber = (val) => typeof val === 'number';
38
- exports.isNumber = isNumber;
39
- const isConstructor = (val) => val === 'constructor';
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
- exports.isPromise = isPromise;
51
- function isNilOrEmptyString(str) {
42
+ export function isNilOrEmptyString(str) {
52
43
  return str === undefined || str === null || str === '';
53
44
  }
54
- exports.isNilOrEmptyString = isNilOrEmptyString;
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 ((0, exports.isNil)(metaItem))
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 './check.js';
2
- export * from './utils.js';
3
- //# sourceMappingURL=index.d.ts.map
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
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
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
- "use strict";
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
- return console.warn(message);
5
+ console.warn(message);
9
6
  }
10
- exports.deprecated = deprecated;
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
- exports.catchError = catchError;
23
- async function sleep(ms) {
18
+ export async function sleep(ms) {
24
19
  return new Promise(resolve => setTimeout(resolve, ms));
25
20
  }
26
- exports.sleep = sleep;
27
- function replaceTemplate(content, scope) {
21
+ export function replaceTemplate(content, scope) {
28
22
  if (!content)
29
23
  return content;
30
- return content.toString().replace(/(\\)?{{ *([\w\.]+) *}}/g, (block, skip, key) => {
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
- exports.replaceTemplate = replaceTemplate;
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
- exports.setProperty = setProperty;
58
- function getProperty(obj, name, sep) {
50
+ export function getProperty(obj, name, sep) {
59
51
  return _getProperty(obj, name, sep, false);
60
52
  }
61
- exports.getProperty = getProperty;
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 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) {
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
- exports.createFunction = createFunction;
110
- function _evaluateSimple(expression) {
111
- const fn = createFunction(expression);
112
- return fn();
113
- }
114
- //# sourceMappingURL=utils.js.map
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
- "version": "1.0.3",
3
+ "type": "module",
4
+ "version": "1.0.5",
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
- "./dist/index.d.ts",
12
- "./src/index.ts"
17
+ "./src/index.ts",
18
+ "./dist/index.d.ts"
13
19
  ],
14
- "import": "./dist/index.js",
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/**/*.js",
22
- "dist/**/*.d.ts"
25
+ "dist"
23
26
  ],
24
- "keywords": [
25
- "egg",
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
  }