@tmagic/utils 1.4.2 → 1.4.4

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.2",
2
+ "version": "1.4.4",
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.4.2",
28
+ "@tmagic/schema": "1.4.4",
29
29
  "dayjs": "^1.11.4",
30
30
  "lodash-es": "^4.17.21"
31
31
  },
package/src/index.ts CHANGED
@@ -165,18 +165,17 @@ export const guid = (digit = 8): string =>
165
165
  return v.toString(16);
166
166
  });
167
167
 
168
- export const getValueByKeyPath: any = (keys = '', data: Record<string | number, any> = {}) =>
168
+ export const getValueByKeyPath: any = (keys: string | string[] = '', data: Record<string | number, any> = {}) => {
169
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
- }
170
+ const keyArray = Array.isArray(keys) ? keys : keys.replaceAll(/\[(\d+)\]/g, '.$1').split('.');
171
+ return keyArray.reduce((accumulator, currentValue: any) => {
172
+ if (isObject(accumulator) || Array.isArray(accumulator)) {
173
+ return accumulator[currentValue];
174
+ }
177
175
 
178
- return void 0;
179
- }, data);
176
+ return void 0;
177
+ }, data);
178
+ };
180
179
 
181
180
  export const setValueByKeyPath: any = (keys: string, value: any, data: Record<string | number, any> = {}) =>
182
181
  objectSet(data, keys, value);
@@ -266,13 +265,20 @@ export const compiledNode = (
266
265
  }
267
266
 
268
267
  keys.forEach((key) => {
269
- const cacheKey = `${DSL_NODE_KEY_COPY_PREFIX}${key}`;
268
+ const keys = `${key}`.replaceAll(/\[(\d+)\]/g, '.$1').split('.');
269
+
270
+ const cacheKey = keys.map((key, index) => {
271
+ if (index < keys.length - 1) {
272
+ return key;
273
+ }
274
+ return `${DSL_NODE_KEY_COPY_PREFIX}${key}`;
275
+ });
270
276
 
271
277
  const value = getValueByKeyPath(key, node);
272
278
  let templateValue = getValueByKeyPath(cacheKey, node);
273
279
 
274
280
  if (typeof templateValue === 'undefined') {
275
- setValueByKeyPath(cacheKey, value, node);
281
+ setValueByKeyPath(cacheKey.join('.'), value, node);
276
282
  templateValue = value;
277
283
  }
278
284