@cabloy/utils 1.0.31 → 1.0.32

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 CHANGED
@@ -1,6 +1,6 @@
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
3
  export declare const CeljsPrefix = "#!#";
4
- export declare function evaluateExpressions(expressions: any, context?: Record<string, unknown>, functions?: Record<string, CallableFunction>, dry?: boolean): unknown;
5
- export declare function evaluate(expression: CstNode | string, context?: Record<string, unknown>, functions?: Record<string, CallableFunction>): unknown;
4
+ export declare function evaluateExpressions<T = any>(expressions: any, context?: object, functions?: Record<string, CallableFunction>, dry?: boolean): T;
5
+ export declare function evaluate<T = any>(expression: CstNode | string, context?: object, functions?: Record<string, CallableFunction>): T;
6
6
  export declare function parse(expression: string): Celjs.ParseResult;
package/dist/cel.js CHANGED
@@ -1,5 +1,7 @@
1
- import * as celjs from 'cel-js';
1
+ import * as celjs from '@cabloy/cel-js';
2
2
  import { isNil } from "./check.js";
3
+ import { hashkey } from "./hash.js";
4
+ import { getProperty } from "./utils.js";
3
5
  export const CeljsPrefix = '#!#';
4
6
  export function evaluateExpressions(expressions, context, functions, dry) {
5
7
  if (isNil(expressions))
@@ -32,6 +34,8 @@ 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);
@@ -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
@@ -1,4 +1,5 @@
1
1
  export * from './cel.ts';
2
2
  export * from './check.ts';
3
+ export * from './hash.ts';
3
4
  export * from './utils.ts';
4
5
  export * from './vona.ts';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./cel.js";
2
2
  export * from "./check.js";
3
+ export * from "./hash.js";
3
4
  export * from "./utils.js";
4
5
  export * from "./vona.js";
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 name of names) {
63
- if (obj[name] === undefined || obj[name] === null) {
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
- obj[name] = {};
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.31",
4
+ "version": "1.0.32",
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/cel-js": "^0.7.2",
28
29
  "@cabloy/module-info": "^1.3.23",
29
30
  "@cabloy/word-utils": "^2.0.1",
30
- "cel-js": "^0.3.1",
31
- "chevrotain": "^11.0.3"
31
+ "chevrotain": "^11.0.3",
32
+ "object-hash": "^3.0.0"
32
33
  },
33
34
  "gitHead": "0eab9dc4a5622caffe89e7b1b3f02c08ccbc4c4b",
34
35
  "scripts": {