@tmagic/utils 1.3.7 → 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/dist/tmagic-utils.js +20 -36
- package/dist/tmagic-utils.js.map +1 -1
- package/dist/tmagic-utils.umd.cjs +2733 -35
- package/dist/tmagic-utils.umd.cjs.map +1 -1
- package/package.json +11 -9
- package/src/index.ts +21 -38
- package/LICENSE +0 -5432
- package/types/dom.d.ts +0 -11
- package/types/index.d.ts +0 -48
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,
|
|
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
|
-
|
|
172
|
-
|
|
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
|
|
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
|
-
|
|
280
|
-
|
|
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
|
-
|
|
287
|
-
|
|
263
|
+
if (typeof templateValue === 'undefined') {
|
|
264
|
+
setValueByKeyPath(cacheKey, value, node);
|
|
265
|
+
templateValue = value;
|
|
266
|
+
}
|
|
288
267
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
268
|
+
let newValue;
|
|
269
|
+
try {
|
|
270
|
+
newValue = compile(templateValue);
|
|
271
|
+
} catch (e) {
|
|
272
|
+
console.error(e);
|
|
273
|
+
newValue = '';
|
|
274
|
+
}
|
|
292
275
|
|
|
293
|
-
|
|
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
|
|