@tmagic/utils 1.3.8 → 1.3.9-hotfix

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/src/index.ts CHANGED
@@ -18,6 +18,7 @@
18
18
 
19
19
  import dayjs from 'dayjs';
20
20
  import utc from 'dayjs/plugin/utc';
21
+ import { cloneDeep, get as objectGet, set as objectSet } from 'lodash-es';
21
22
 
22
23
  import type { DataSchema, DataSourceDeps, Id, MComponent, MNode } from '@tmagic/schema';
23
24
  import { NodeType } from '@tmagic/schema';
@@ -164,22 +165,10 @@ export const guid = (digit = 8): string =>
164
165
  return v.toString(16);
165
166
  });
166
167
 
167
- export const getValueByKeyPath: any = (keys: string, value: Record<string | number, any> = {}) => {
168
- const path = keys.split('.');
169
- const pathLength = path.length;
168
+ export const getValueByKeyPath: any = (keys: string, data: Record<string | number, any> = {}) => objectGet(data, keys);
170
169
 
171
- return path.reduce((accumulator, currentValue: any, currentIndex: number) => {
172
- if (Object.prototype.toString.call(accumulator) === '[object Object]' || Array.isArray(accumulator)) {
173
- return accumulator[currentValue];
174
- }
175
-
176
- if (pathLength - 1 === currentIndex) {
177
- return undefined;
178
- }
179
-
180
- return {};
181
- }, value);
182
- };
170
+ export const setValueByKeyPath: any = (keys: string, value: any, data: Record<string | number, any> = {}) =>
171
+ objectSet(data, keys, value);
183
172
 
184
173
  export const getNodes = (ids: Id[], data: MNode[] = []): MNode[] => {
185
174
  const nodes: MNode[] = [];
@@ -266,32 +255,25 @@ export const compiledNode = (
266
255
  const keyPrefix = '__magic__';
267
256
 
268
257
  keys.forEach((key) => {
269
- const keyPath = `${key}`.split('.');
270
- const keyPathLength = keyPath.length;
271
- keyPath.reduce((accumulator, currentValue: any, currentIndex) => {
272
- if (keyPathLength - 1 === currentIndex) {
273
- const cacheKey = `${keyPrefix}${currentValue}`;
274
-
275
- if (typeof accumulator[cacheKey] === 'undefined') {
276
- accumulator[cacheKey] = accumulator[currentValue];
277
- }
258
+ const cacheKey = `${keyPrefix}${key}`;
278
259
 
279
- try {
280
- accumulator[currentValue] = compile(accumulator[cacheKey]);
281
- } catch (e) {
282
- console.error(e);
283
- accumulator[currentValue] = '';
284
- }
260
+ const value = getValueByKeyPath(key, node);
261
+ let templateValue = getValueByKeyPath(cacheKey, node);
285
262
 
286
- return accumulator;
287
- }
263
+ if (typeof templateValue === 'undefined') {
264
+ setValueByKeyPath(cacheKey, value, node);
265
+ templateValue = value;
266
+ }
288
267
 
289
- if (isObject(accumulator) || Array.isArray(accumulator)) {
290
- return accumulator[currentValue];
291
- }
268
+ let newValue;
269
+ try {
270
+ newValue = compile(templateValue);
271
+ } catch (e) {
272
+ console.error(e);
273
+ newValue = '';
274
+ }
292
275
 
293
- return {};
294
- }, node);
276
+ setValueByKeyPath(key, newValue, node);
295
277
  });
296
278
 
297
279
  if (Array.isArray(node.items)) {
@@ -368,7 +350,8 @@ export const getDefaultValueFromFields = (fields: DataSchema[]) => {
368
350
  return;
369
351
  }
370
352
 
371
- data[field.name] = field.defaultValue;
353
+ data[field.name] = cloneDeep(field.defaultValue);
354
+
372
355
  return;
373
356
  }
374
357