@tmagic/utils 1.3.16 → 1.4.0-beta.2
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/tmagic-utils.js +13 -8
- package/dist/tmagic-utils.js.map +1 -1
- package/dist/tmagic-utils.umd.cjs +12 -56
- package/dist/tmagic-utils.umd.cjs.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +16 -9
- package/types/index.d.ts +2 -1
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.
|
|
2
|
+
"version": "1.4.0-beta.2",
|
|
3
3
|
"name": "@tmagic/utils",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/tmagic-utils.umd.cjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"url": "https://github.com/Tencent/tmagic-editor.git"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@tmagic/schema": "1.
|
|
28
|
+
"@tmagic/schema": "1.4.0-beta.2",
|
|
29
29
|
"dayjs": "^1.11.4",
|
|
30
30
|
"lodash-es": "^4.17.21"
|
|
31
31
|
},
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"@types/lodash-es": "^4.17.4",
|
|
37
37
|
"@types/node": "^18.19.0",
|
|
38
38
|
"rimraf": "^3.0.2",
|
|
39
|
-
"typescript": "^5.
|
|
40
|
-
"vite": "^5.
|
|
39
|
+
"typescript": "^5.4.2",
|
|
40
|
+
"vite": "^5.1.6"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "rimraf ./dist && npm run build:type && vite build --mode=es && vite build --mode=umd",
|
package/src/index.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
import dayjs from 'dayjs';
|
|
20
20
|
import utc from 'dayjs/plugin/utc';
|
|
21
|
-
import { cloneDeep,
|
|
21
|
+
import { cloneDeep, set as objectSet } from 'lodash-es';
|
|
22
22
|
|
|
23
23
|
import type { DataSchema, DataSourceDeps, Id, MComponent, MNode } from '@tmagic/schema';
|
|
24
24
|
import { NodeType } from '@tmagic/schema';
|
|
@@ -165,7 +165,18 @@ export const guid = (digit = 8): string =>
|
|
|
165
165
|
return v.toString(16);
|
|
166
166
|
});
|
|
167
167
|
|
|
168
|
-
export const getValueByKeyPath: any = (keys
|
|
168
|
+
export const getValueByKeyPath: any = (keys = '', data: Record<string | number, any> = {}) =>
|
|
169
|
+
// 将 array[0] 转成 array.0
|
|
170
|
+
keys
|
|
171
|
+
.replaceAll(/\[(\d+)\]/g, '.$1')
|
|
172
|
+
.split('.')
|
|
173
|
+
.reduce((accumulator, currentValue: any) => {
|
|
174
|
+
if (isObject(accumulator) || Array.isArray(accumulator)) {
|
|
175
|
+
return accumulator[currentValue];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return void 0;
|
|
179
|
+
}, data);
|
|
169
180
|
|
|
170
181
|
export const setValueByKeyPath: any = (keys: string, value: any, data: Record<string | number, any> = {}) =>
|
|
171
182
|
objectSet(data, keys, value);
|
|
@@ -238,6 +249,8 @@ export const replaceChildNode = (newNode: MNode, data?: MNode[], parentId?: Id)
|
|
|
238
249
|
parent.items.splice(index, 1, newNode);
|
|
239
250
|
};
|
|
240
251
|
|
|
252
|
+
export const DSL_NODE_KEY_COPY_PREFIX = '__magic__';
|
|
253
|
+
|
|
241
254
|
export const compiledNode = (
|
|
242
255
|
compile: (value: any) => any,
|
|
243
256
|
node: MNode,
|
|
@@ -252,10 +265,8 @@ export const compiledNode = (
|
|
|
252
265
|
keys = dep?.[node.id].keys || [];
|
|
253
266
|
}
|
|
254
267
|
|
|
255
|
-
const keyPrefix = '__magic__';
|
|
256
|
-
|
|
257
268
|
keys.forEach((key) => {
|
|
258
|
-
const cacheKey = `${
|
|
269
|
+
const cacheKey = `${DSL_NODE_KEY_COPY_PREFIX}${key}`;
|
|
259
270
|
|
|
260
271
|
const value = getValueByKeyPath(key, node);
|
|
261
272
|
let templateValue = getValueByKeyPath(cacheKey, node);
|
|
@@ -276,10 +287,6 @@ export const compiledNode = (
|
|
|
276
287
|
setValueByKeyPath(key, newValue, node);
|
|
277
288
|
});
|
|
278
289
|
|
|
279
|
-
if (Array.isArray(node.items)) {
|
|
280
|
-
node.items.forEach((item) => compiledNode(compile, item, dataSourceDeps));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
290
|
return node;
|
|
284
291
|
};
|
|
285
292
|
|
package/types/index.d.ts
CHANGED
|
@@ -39,11 +39,12 @@ export declare const getDepNodeIds: (dataSourceDeps?: DataSourceDeps) => string[
|
|
|
39
39
|
* @param parentId 父节点 id
|
|
40
40
|
*/
|
|
41
41
|
export declare const replaceChildNode: (newNode: MNode, data?: MNode[], parentId?: Id) => void;
|
|
42
|
+
export declare const DSL_NODE_KEY_COPY_PREFIX = "__magic__";
|
|
42
43
|
export declare const compiledNode: (compile: (value: any) => any, node: MNode, dataSourceDeps?: DataSourceDeps, sourceId?: Id) => MNode;
|
|
43
44
|
export declare const compiledCond: (op: string, fieldValue: any, inputValue: any, range?: number[]) => boolean;
|
|
44
45
|
export declare const getDefaultValueFromFields: (fields: DataSchema[]) => Record<string, any>;
|
|
45
46
|
export declare const DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX = "ds-field::";
|
|
46
|
-
export declare const getKeys: <T extends object>(obj: T) =>
|
|
47
|
+
export declare const getKeys: <T extends object>(obj: T) => Array<keyof T>;
|
|
47
48
|
export declare const calculatePercentage: (value: number, percentageStr: string) => number;
|
|
48
49
|
export declare const isPercentage: (value: number | string) => boolean;
|
|
49
50
|
export declare const convertToNumber: (value: number | string, parentValue?: number) => number;
|