qidian-shared 1.0.57 → 1.0.59

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.
@@ -4,6 +4,9 @@ export declare function buildTree<T extends object>(data: T[], options?: {
4
4
  parentId?: keyof T;
5
5
  children?: keyof T;
6
6
  }): T[];
7
- export declare function remapKeys<T extends Record<string, any>, M extends Partial<Record<keyof T, string>>>(data: T, keyMap: M): {
8
- [K in keyof T as K extends keyof M ? (M[K] extends string ? M[K] : K) : K]: T[K];
7
+ type KeyMapValue<T, K extends keyof T> = string | {
8
+ newKey?: string;
9
+ handler?: (val: T[K]) => any;
9
10
  };
11
+ export declare function remapKeys<T extends Record<string, any>, M extends Partial<Record<keyof T, KeyMapValue<T, keyof T>>>>(data: T, keyMap: M, strict?: boolean): Record<string, any>;
12
+ export {};
@@ -1,4 +1,4 @@
1
- import { isEmpty } from "./common.mjs";
1
+ import { isEmpty, hasOwn } from "./common.mjs";
2
2
  function toArr(target) {
3
3
  if (Array.isArray(target)) return target;
4
4
  if (isEmpty(target)) return [];
@@ -37,11 +37,17 @@ function buildTree(data, options = {}) {
37
37
  }
38
38
  return tree;
39
39
  }
40
- function remapKeys(data, keyMap) {
40
+ function remapKeys(data, keyMap, strict = false) {
41
41
  const result = {};
42
42
  for (const key in data) {
43
- const newKey = keyMap[key] || key;
44
- result[newKey] = data[key];
43
+ if (hasOwn(keyMap, key)) {
44
+ const mapValue = keyMap[key];
45
+ const newKey = typeof mapValue === "string" ? mapValue : mapValue?.newKey;
46
+ const value = typeof mapValue === "object" && mapValue.handler ? mapValue.handler(data[key]) : data[key];
47
+ result[isEmpty(newKey) ? key : newKey] = value;
48
+ } else if (!strict) {
49
+ result[key] = data[key];
50
+ }
45
51
  }
46
52
  return result;
47
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"conversion.mjs","sources":["../../src/utils/conversion.ts"],"sourcesContent":["import { isEmpty } from './common'\n\n/**\n * 转数组\n * @param target\n * @return 转换后的数组\n */\nexport function toArr<T>(target: T | T[] | undefined): T[] {\n if (Array.isArray(target)) return target\n if (isEmpty(target)) return []\n return [target]\n}\n\n/**\n * 构造树型结构数据\n * @param data 扁平化的节点数组\n * @param options 配置项\n * @param options.id ID 字段名,默认 'id'\n * @param options.parentId 父节点 ID 字段名,默认 'parentId'\n * @param options.children 子节点字段名,默认 'children'\n * @returns 树型结构数组\n * @example\n * buildTree([\n * { id: 1, name: 'A' },\n * { id: 2, parentId: 1, name: 'B' },\n * { id: 3, parentId: 1, name: 'C' }\n * ]) // [{ id: 1, name: 'A', children: [{ id: 2, parentId: 1, name: 'B' }, { id: 3, parentId: 1, name: 'C' }] }]\n */\nexport function buildTree<T extends object>(\n data: T[],\n options: {\n id?: keyof T\n parentId?: keyof T\n children?: keyof T\n } = {}\n): T[] {\n const { id = 'id', parentId = 'parentId', children = 'children' } = options\n const childrenListMap: Record<string | number | symbol, T[]> = {}\n const nodeIds: Record<string | number | symbol, T> = {}\n const tree: T[] = []\n\n for (const d of data) {\n const pid = d[parentId as keyof typeof d] as string | number | symbol\n if (!childrenListMap[pid]) {\n childrenListMap[pid] = []\n }\n nodeIds[d[id as keyof typeof d] as string | number | symbol] = d\n childrenListMap[pid].push(d)\n }\n\n for (const d of data) {\n const pid = d[parentId as keyof typeof d] as string | number | symbol\n if (!nodeIds[pid]) {\n tree.push(d)\n }\n }\n\n for (const t of tree) {\n adaptToChildrenList(t)\n }\n\n function adaptToChildrenList(o: T): void {\n const childNodes = childrenListMap[o[id as keyof typeof o] as string | number | symbol]\n if (childNodes) {\n ;(o as Record<string, unknown>)[children as string] = childNodes\n for (const c of childNodes) {\n adaptToChildrenList(c)\n }\n }\n }\n return tree\n}\n\n/**\n * 重命名对象键\n * @param data 源对象\n * @param keyMap 键映射,格式为 { 旧键: 新键 }\n * @returns 键被重命名后的新对象\n * @example\n * remapKeys({ name: 'foo', age: 18 }, { name: 'userName' }) // { userName: 'foo', age: 18 }\n */\nexport function remapKeys<\n T extends Record<string, any>,\n M extends Partial<Record<keyof T, string>>\n>(\n data: T,\n keyMap: M\n): {\n [K in keyof T as K extends keyof M ? (M[K] extends string ? M[K] : K) : K]: T[K]\n} {\n const result = {} as any\n\n for (const key in data) {\n const newKey = keyMap[key] || key\n result[newKey] = data[key]\n }\n\n return result\n}\n"],"names":[],"mappings":";AAOO,SAAS,MAAS,QAAkC;AACzD,MAAI,MAAM,QAAQ,MAAM,EAAG,QAAO;AAClC,MAAI,QAAQ,MAAM,EAAG,QAAO,CAAA;AAC5B,SAAO,CAAC,MAAM;AAChB;AAiBO,SAAS,UACd,MACA,UAII,IACC;AACL,QAAM,EAAE,KAAK,MAAM,WAAW,YAAY,WAAW,eAAe;AACpE,QAAM,kBAAyD,CAAA;AAC/D,QAAM,UAA+C,CAAA;AACrD,QAAM,OAAY,CAAA;AAElB,aAAW,KAAK,MAAM;AACpB,UAAM,MAAM,EAAE,QAA0B;AACxC,QAAI,CAAC,gBAAgB,GAAG,GAAG;AACzB,sBAAgB,GAAG,IAAI,CAAA;AAAA,IACzB;AACA,YAAQ,EAAE,EAAoB,CAA6B,IAAI;AAC/D,oBAAgB,GAAG,EAAE,KAAK,CAAC;AAAA,EAC7B;AAEA,aAAW,KAAK,MAAM;AACpB,UAAM,MAAM,EAAE,QAA0B;AACxC,QAAI,CAAC,QAAQ,GAAG,GAAG;AACjB,WAAK,KAAK,CAAC;AAAA,IACb;AAAA,EACF;AAEA,aAAW,KAAK,MAAM;AACpB,wBAAoB,CAAC;AAAA,EACvB;AAEA,WAAS,oBAAoB,GAAY;AACvC,UAAM,aAAa,gBAAgB,EAAE,EAAoB,CAA6B;AACtF,QAAI,YAAY;AACZ,QAA8B,QAAkB,IAAI;AACtD,iBAAW,KAAK,YAAY;AAC1B,4BAAoB,CAAC;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAUO,SAAS,UAId,MACA,QAGA;AACA,QAAM,SAAS,CAAA;AAEf,aAAW,OAAO,MAAM;AACtB,UAAM,SAAS,OAAO,GAAG,KAAK;AAC9B,WAAO,MAAM,IAAI,KAAK,GAAG;AAAA,EAC3B;AAEA,SAAO;AACT;"}
1
+ {"version":3,"file":"conversion.mjs","sources":["../../src/utils/conversion.ts"],"sourcesContent":["import { hasOwn, isEmpty } from './common'\n\n/**\n * 转数组\n * @param target\n * @return 转换后的数组\n */\nexport function toArr<T>(target: T | T[] | undefined): T[] {\n if (Array.isArray(target)) return target\n if (isEmpty(target)) return []\n return [target]\n}\n\n/**\n * 构造树型结构数据\n * @param data 扁平化的节点数组\n * @param options 配置项\n * @param options.id ID 字段名,默认 'id'\n * @param options.parentId 父节点 ID 字段名,默认 'parentId'\n * @param options.children 子节点字段名,默认 'children'\n * @returns 树型结构数组\n * @example\n * buildTree([\n * { id: 1, name: 'A' },\n * { id: 2, parentId: 1, name: 'B' },\n * { id: 3, parentId: 1, name: 'C' }\n * ]) // [{ id: 1, name: 'A', children: [{ id: 2, parentId: 1, name: 'B' }, { id: 3, parentId: 1, name: 'C' }] }]\n */\nexport function buildTree<T extends object>(\n data: T[],\n options: {\n id?: keyof T\n parentId?: keyof T\n children?: keyof T\n } = {}\n): T[] {\n const { id = 'id', parentId = 'parentId', children = 'children' } = options\n const childrenListMap: Record<string | number | symbol, T[]> = {}\n const nodeIds: Record<string | number | symbol, T> = {}\n const tree: T[] = []\n\n for (const d of data) {\n const pid = d[parentId as keyof typeof d] as string | number | symbol\n if (!childrenListMap[pid]) {\n childrenListMap[pid] = []\n }\n nodeIds[d[id as keyof typeof d] as string | number | symbol] = d\n childrenListMap[pid].push(d)\n }\n\n for (const d of data) {\n const pid = d[parentId as keyof typeof d] as string | number | symbol\n if (!nodeIds[pid]) {\n tree.push(d)\n }\n }\n\n for (const t of tree) {\n adaptToChildrenList(t)\n }\n\n function adaptToChildrenList(o: T): void {\n const childNodes = childrenListMap[o[id as keyof typeof o] as string | number | symbol]\n if (childNodes) {\n ;(o as Record<string, unknown>)[children as string] = childNodes\n for (const c of childNodes) {\n adaptToChildrenList(c)\n }\n }\n }\n return tree\n}\n\ntype KeyMapValue<T, K extends keyof T> = string | { newKey?: string; handler?: (val: T[K]) => any }\n\n/**\n * 重命名对象键\n * @param data 源对象\n * @param keyMap 键映射,格式为 { 旧键: 新键 | { newKey, handler } }\n * @param strict 是否严格模式(只保留映射过的字段),默认 false\n * @returns 键被重命名后的新对象\n * @example\n * remapKeys({ name: 'foo', age: 18 }, { name: 'userName' }) // { userName: 'foo', age: 18 }\n * remapKeys({ name: 'foo', age: 18 }, { name: 'userName' }, true) // { userName: 'foo' }\n * remapKeys({ name: 'foo', age: 18 }, { name: { newKey: 'userName', handler: v => v.toUpperCase() } }) // { userName: 'FOO', age: 18 }\n */\nexport function remapKeys<\n T extends Record<string, any>,\n M extends Partial<Record<keyof T, KeyMapValue<T, keyof T>>>\n>(data: T, keyMap: M, strict = false): Record<string, any> {\n const result = {} as any\n\n for (const key in data) {\n if (hasOwn(keyMap, key)) {\n const mapValue = keyMap[key]\n const newKey = typeof mapValue === 'string' ? mapValue : mapValue?.newKey\n const value =\n typeof mapValue === 'object' && mapValue.handler ? mapValue.handler(data[key]) : data[key]\n result[isEmpty(newKey) ? key : newKey] = value\n } else if (!strict) {\n result[key] = data[key]\n }\n }\n\n return result\n}\n"],"names":[],"mappings":";AAOO,SAAS,MAAS,QAAkC;AACzD,MAAI,MAAM,QAAQ,MAAM,EAAG,QAAO;AAClC,MAAI,QAAQ,MAAM,EAAG,QAAO,CAAA;AAC5B,SAAO,CAAC,MAAM;AAChB;AAiBO,SAAS,UACd,MACA,UAII,IACC;AACL,QAAM,EAAE,KAAK,MAAM,WAAW,YAAY,WAAW,eAAe;AACpE,QAAM,kBAAyD,CAAA;AAC/D,QAAM,UAA+C,CAAA;AACrD,QAAM,OAAY,CAAA;AAElB,aAAW,KAAK,MAAM;AACpB,UAAM,MAAM,EAAE,QAA0B;AACxC,QAAI,CAAC,gBAAgB,GAAG,GAAG;AACzB,sBAAgB,GAAG,IAAI,CAAA;AAAA,IACzB;AACA,YAAQ,EAAE,EAAoB,CAA6B,IAAI;AAC/D,oBAAgB,GAAG,EAAE,KAAK,CAAC;AAAA,EAC7B;AAEA,aAAW,KAAK,MAAM;AACpB,UAAM,MAAM,EAAE,QAA0B;AACxC,QAAI,CAAC,QAAQ,GAAG,GAAG;AACjB,WAAK,KAAK,CAAC;AAAA,IACb;AAAA,EACF;AAEA,aAAW,KAAK,MAAM;AACpB,wBAAoB,CAAC;AAAA,EACvB;AAEA,WAAS,oBAAoB,GAAY;AACvC,UAAM,aAAa,gBAAgB,EAAE,EAAoB,CAA6B;AACtF,QAAI,YAAY;AACZ,QAA8B,QAAkB,IAAI;AACtD,iBAAW,KAAK,YAAY;AAC1B,4BAAoB,CAAC;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAeO,SAAS,UAGd,MAAS,QAAW,SAAS,OAA4B;AACzD,QAAM,SAAS,CAAA;AAEf,aAAW,OAAO,MAAM;AACtB,QAAI,OAAO,QAAQ,GAAG,GAAG;AACvB,YAAM,WAAW,OAAO,GAAG;AAC3B,YAAM,SAAS,OAAO,aAAa,WAAW,WAAW,UAAU;AACnE,YAAM,QACJ,OAAO,aAAa,YAAY,SAAS,UAAU,SAAS,QAAQ,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG;AAC3F,aAAO,QAAQ,MAAM,IAAI,MAAM,MAAM,IAAI;AAAA,IAC3C,WAAW,CAAC,QAAQ;AAClB,aAAO,GAAG,IAAI,KAAK,GAAG;AAAA,IACxB;AAAA,EACF;AAEA,SAAO;AACT;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qidian-shared",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "description": "QiDian 共享工具函数和钩子",
5
5
  "type": "module",
6
6
  "author": "qidian",