@tmagic/utils 1.8.0-manmanyu.9 → 1.8.0

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.8.0-manmanyu.9",
2
+ "version": "1.8.0",
3
3
  "name": "@tmagic/utils",
4
4
  "type": "module",
5
5
  "sideEffects": false,
@@ -28,14 +28,14 @@
28
28
  "url": "https://github.com/Tencent/tmagic-editor.git"
29
29
  },
30
30
  "dependencies": {
31
- "lodash-es": "^4.17.21"
31
+ "lodash-es": "^4.18.1"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/lodash-es": "^4.17.4"
34
+ "@types/lodash-es": "^4.17.12"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "typescript": "^6.0.3",
38
- "@tmagic/schema": "1.8.0-manmanyu.9"
38
+ "@tmagic/schema": "1.8.0"
39
39
  },
40
40
  "peerDependenciesMeta": {
41
41
  "typescript": {
package/src/index.ts CHANGED
@@ -17,7 +17,7 @@
17
17
  * limitations under the License.
18
18
  */
19
19
 
20
- import { cloneDeep, set as objectSet } from 'lodash-es';
20
+ import { cloneDeep, get as objectGet, set as objectSet } from 'lodash-es';
21
21
 
22
22
  import type {
23
23
  DataSchema,
@@ -142,7 +142,7 @@ export const getNodeInfo = (id: Id, root: { id: Id; items?: MNode[] } | null, sk
142
142
  info.parent = path[path.length - 2] as MContainer;
143
143
 
144
144
  for (const item of path) {
145
- if (isPage(item) || isPageFragment(item)) {
145
+ if (isPageOrFragment(item)) {
146
146
  info.page = item as MPage | MPageFragment;
147
147
  break;
148
148
  }
@@ -279,6 +279,10 @@ export const isPageFragment = (node?: Pick<MComponent, 'type'> | null): boolean
279
279
  return Boolean(node.type?.toLowerCase() === NodeType.PAGE_FRAGMENT);
280
280
  };
281
281
 
282
+ /** 是否为页面或页面片 */
283
+ export const isPageOrFragment = (node?: Pick<MComponent, 'type'> | null): boolean =>
284
+ isPage(node) || isPageFragment(node);
285
+
282
286
  export const isNumber = (value: any) =>
283
287
  (typeof value === 'number' && !isNaN(value)) || /^(-?\d+)(\.\d+)?$/.test(`${value}`);
284
288
 
@@ -308,6 +312,13 @@ export const getKeysArray = (keys: string | number) =>
308
312
  // 将 array[0] 转成 array.0
309
313
  `${keys}`.replace(/\[(\d+)\]/g, '.$1').split('.');
310
314
 
315
+ /**
316
+ * 判断某一层 key 是否为数组下标(纯数字)
317
+ * @param key 单层 key
318
+ * @returns 是否为数组下标
319
+ */
320
+ export const isArrayIndex = (key: string | number): boolean => /^\d+$/.test(`${key}`);
321
+
311
322
  export const getValueByKeyPath = (
312
323
  keys: number | string | string[] = '',
313
324
  data: Record<string | number, any> = {},
@@ -327,8 +338,23 @@ export const getValueByKeyPath = (
327
338
  }, data);
328
339
  };
329
340
 
330
- export const setValueByKeyPath = (keys: string | number, value: any, data: Record<string | number, any> = {}): any =>
331
- objectSet(data, keys, value);
341
+ export const setValueByKeyPath = (keys: string | number, value: any, data: Record<string | number, any> = {}): any => {
342
+ if (typeof value === 'undefined') {
343
+ // 仅数组下标场景需要特殊处理:lodash 的 set 会把 undefined 写入数组得到 [undefined],
344
+ // 此处改为只创建父级空数组,避免出现 [undefined]。对象属性场景 undefined 会被 JSON 自然丢弃,保持原行为。
345
+ // 注意:这里将「数字型末级 key」统一按数组下标处理,无法区分数字型对象 key,属于预期内的取舍。
346
+ const keyArray = getKeysArray(keys);
347
+ const lastKey = keyArray[keyArray.length - 1];
348
+ if (keyArray.length > 1 && isArrayIndex(lastKey)) {
349
+ const parentPath = keyArray.slice(0, -1).join('.');
350
+ if (typeof objectGet(data, parentPath) === 'undefined') {
351
+ objectSet(data, parentPath, []);
352
+ }
353
+ return data;
354
+ }
355
+ }
356
+ return objectSet(data, keys, value);
357
+ };
332
358
 
333
359
  export const getNodes = (ids: Id[], data: MNode[] = []): MNode[] => {
334
360
  const nodes: MNode[] = [];