jz-toolkit 1.0.9 → 1.1.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.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const filterDuplicateValues_1 = require("../array/filterDuplicateValues");
4
4
  const toFixed_1 = require("../number/toFixed");
5
5
  const hasKey_1 = require("../object/hasKey");
6
+ const flattenTree_1 = require("../mapToArray/flattenTree");
6
7
  console.log((0, hasKey_1.getPropertyNames)({ a: 1, b: 2 }));
7
8
  const res = (0, filterDuplicateValues_1.filterArrayAttr)([
8
9
  {
@@ -17,3 +18,17 @@ const res = (0, filterDuplicateValues_1.filterArrayAttr)([
17
18
  console.log("res :>> ", res);
18
19
  const price = 19.956;
19
20
  console.log((0, toFixed_1.numberToFixed)(price, 2)); // "19.96" - 保留两位小数
21
+ const tree = [
22
+ {
23
+ id: 1,
24
+ name: "节点1",
25
+ children: [
26
+ { id: 11, name: "节点1-1" },
27
+ { id: 12, name: "节点1-2", children: [{ id: 121, name: "节点1-2-1" }] },
28
+ ],
29
+ },
30
+ { id: 2, name: "节点2" },
31
+ ];
32
+ // 扁平化
33
+ const flatArray = (0, flattenTree_1.flattenTree)(tree);
34
+ console.log(flatArray);
@@ -1,6 +1,7 @@
1
1
  import { filterArrayAttr } from "../array/filterDuplicateValues";
2
2
  import { numberToFixed } from "../number/toFixed";
3
3
  import { getPropertyNames } from "../object/hasKey";
4
+ import { flattenTree } from "../mapToArray/flattenTree";
4
5
  console.log(getPropertyNames({ a: 1, b: 2 }));
5
6
  const res = filterArrayAttr([
6
7
  {
@@ -15,3 +16,17 @@ const res = filterArrayAttr([
15
16
  console.log("res :>> ", res);
16
17
  const price = 19.956;
17
18
  console.log(numberToFixed(price, 2)); // "19.96" - 保留两位小数
19
+ const tree = [
20
+ {
21
+ id: 1,
22
+ name: "节点1",
23
+ children: [
24
+ { id: 11, name: "节点1-1" },
25
+ { id: 12, name: "节点1-2", children: [{ id: 121, name: "节点1-2-1" }] },
26
+ ],
27
+ },
28
+ { id: 2, name: "节点2" },
29
+ ];
30
+ // 扁平化
31
+ const flatArray = flattenTree(tree);
32
+ console.log(flatArray);
@@ -7,7 +7,9 @@ export * as object from "./object";
7
7
  export * as string from "./string";
8
8
  export * as utility from "./utility";
9
9
  export * as number from "./number";
10
- export { chunk, compact, } from "./array";
10
+ export * as mapToArray from "./mapToArray";
11
+ export { chunk, compact, filterDuplicateValues, filterArrayAttr, } from "./array";
12
+ export { flattenTree, convertToArray } from "./mapToArray";
11
13
  export { pick, omit, } from "./object";
12
14
  export { capitalize, camelCase, } from "./string";
13
15
  export { numberToFixed, } from "./number";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,cAAc,SAAS,CAAC;AAGxB,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAGnC,OAAO,EAEL,KAAK,EACL,OAAO,GACR,MAAM,SAAS,CAAC;AAEjB,OAAO,EAEL,IAAI,EACJ,IAAI,GACL,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,UAAU,EACV,SAAS,GACV,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,aAAa,GACd,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,EACN,WAAW,EACX,iBAAiB,EACjB,OAAO,EACP,QAAQ,EACR,UAAU,EACV,MAAM,EACN,QAAQ,EACR,KAAK,EACL,KAAK,EACL,OAAO,GACR,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,cAAc,SAAS,CAAC;AAGxB,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAG3C,OAAO,EAEL,KAAK,EACL,OAAO,EACP,qBAAqB,EACrB,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE3D,OAAO,EAEL,IAAI,EACJ,IAAI,GACL,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,UAAU,EACV,SAAS,GACV,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,aAAa,GACd,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,EACN,WAAW,EACX,iBAAiB,EACjB,OAAO,EACP,QAAQ,EACR,UAAU,EACV,MAAM,EACN,QAAQ,EACR,KAAK,EACL,KAAK,EACL,OAAO,GACR,MAAM,WAAW,CAAC"}
package/dist/esm/index.js CHANGED
@@ -9,10 +9,12 @@ export * as object from "./object";
9
9
  export * as string from "./string";
10
10
  export * as utility from "./utility";
11
11
  export * as number from "./number";
12
+ export * as mapToArray from "./mapToArray";
12
13
  // 直接导出常用函数
13
14
  export {
14
15
  // 数组
15
- chunk, compact, } from "./array";
16
+ chunk, compact, filterDuplicateValues, filterArrayAttr, } from "./array";
17
+ export { flattenTree, convertToArray } from "./mapToArray";
16
18
  export {
17
19
  // 对象
18
20
  pick, omit, } from "./object";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 将 Map 转换为数组
3
+ * @param map 输入 Map
4
+ * @returns 转换后的数组
5
+ *
6
+ * @example
7
+ * convertToArray(new Map<string, number>([["a", 1], ["b", 2], ["c", 3]])) // => [1, 2, 3]
8
+ */
9
+ export declare function convertToArray<T>(map: Map<string, T>): T[];
10
+ //# sourceMappingURL=convertToArray.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertToArray.d.ts","sourceRoot":"","sources":["../../../src/mapToArray/convertToArray.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAK1D"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 将 Map 转换为数组
3
+ * @param map 输入 Map
4
+ * @returns 转换后的数组
5
+ *
6
+ * @example
7
+ * convertToArray(new Map<string, number>([["a", 1], ["b", 2], ["c", 3]])) // => [1, 2, 3]
8
+ */
9
+ export function convertToArray(map) {
10
+ if (map.size === 0) {
11
+ return [];
12
+ }
13
+ return Array.from(map.values());
14
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 将树形结构展平为数组
3
+ * @param tree 树形结构数组
4
+ * @param childrenKey 子节点在节点对象中的键名,默认为"children"
5
+ * @returns 展平后的数组
6
+ */
7
+ export declare function flattenTree(tree: Record<string, unknown>[], childrenKey?: string): Record<string, unknown>[];
8
+ //# sourceMappingURL=flattenTree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flattenTree.d.ts","sourceRoot":"","sources":["../../../src/mapToArray/flattenTree.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EAAE,WAAW,GAAE,MAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAcxH"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 将树形结构展平为数组
3
+ * @param tree 树形结构数组
4
+ * @param childrenKey 子节点在节点对象中的键名,默认为"children"
5
+ * @returns 展平后的数组
6
+ */
7
+ export function flattenTree(tree, childrenKey = "children") {
8
+ if (!Array.isArray(tree)) {
9
+ throw new Error("tree must be an array");
10
+ }
11
+ const result = [];
12
+ tree.forEach((node) => {
13
+ result.push(node);
14
+ const children = node[childrenKey];
15
+ if (children && Array.isArray(children)) {
16
+ const flattenedChildren = flattenTree(children, childrenKey);
17
+ result.push(...flattenedChildren);
18
+ }
19
+ });
20
+ return result;
21
+ }
@@ -1,2 +1,3 @@
1
- export * from "./mapToArray";
1
+ export * from "./convertToArray";
2
+ export * from "./flattenTree";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mapToArray/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mapToArray/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC"}
@@ -1 +1,2 @@
1
- export * from "./mapToArray";
1
+ export * from "./convertToArray";
2
+ export * from "./flattenTree";
package/dist/index.d.ts CHANGED
@@ -7,7 +7,9 @@ export * as object from "./object";
7
7
  export * as string from "./string";
8
8
  export * as utility from "./utility";
9
9
  export * as number from "./number";
10
- export { chunk, compact, } from "./array";
10
+ export * as mapToArray from "./mapToArray";
11
+ export { chunk, compact, filterDuplicateValues, filterArrayAttr, } from "./array";
12
+ export { flattenTree, convertToArray } from "./mapToArray";
11
13
  export { pick, omit, } from "./object";
12
14
  export { capitalize, camelCase, } from "./string";
13
15
  export { numberToFixed, } from "./number";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,cAAc,SAAS,CAAC;AAGxB,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,MAAM,MAAM,UA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OAAO,EAEL,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,EACN,WAAW,EACX,iBAAiB,EACjB,OAAO,EACP,QAAQ,EACR,UAAU,EACV,MAAM,EACN,QAAQ,EACR,KAAK,EACL,KAAK,EACL,OAAO,GACR,MAAM,WAAW,CAAC"}
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
29
29
  return result;
30
30
  };
31
31
  Object.defineProperty(exports, "__esModule", { value: true });
32
- exports.isEmpty = exports.isSet = exports.isMap = exports.isRegExp = exports.isDate = exports.isFunction = exports.isObject = exports.isArray = exports.isNullOrUndefined = exports.isUndefined = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.numberToFixed = exports.camelCase = exports.capitalize = exports.omit = exports.pick = exports.compact = exports.chunk = exports.number = exports.utility = exports.string = exports.object = exports.array = void 0;
32
+ exports.isEmpty = exports.isSet = exports.isMap = exports.isRegExp = exports.isDate = exports.isFunction = exports.isObject = exports.isArray = exports.isNullOrUndefined = exports.isUndefined = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.numberToFixed = exports.camelCase = exports.capitalize = exports.omit = exports.pick = exports.convertToArray = exports.flattenTree = exports.filterArrayAttr = exports.filterDuplicateValues = exports.compact = exports.chunk = exports.mapToArray = exports.number = exports.utility = exports.string = exports.object = exports.array = void 0;
33
33
  // 类型导出
34
34
  __exportStar(require("./types"), exports);
35
35
  // 功能模块导出
@@ -38,11 +38,17 @@ exports.object = __importStar(require("./object"));
38
38
  exports.string = __importStar(require("./string"));
39
39
  exports.utility = __importStar(require("./utility"));
40
40
  exports.number = __importStar(require("./number"));
41
+ exports.mapToArray = __importStar(require("./mapToArray"));
41
42
  // 直接导出常用函数
42
43
  var array_1 = require("./array");
43
44
  // 数组
44
45
  Object.defineProperty(exports, "chunk", { enumerable: true, get: function () { return array_1.chunk; } });
45
46
  Object.defineProperty(exports, "compact", { enumerable: true, get: function () { return array_1.compact; } });
47
+ Object.defineProperty(exports, "filterDuplicateValues", { enumerable: true, get: function () { return array_1.filterDuplicateValues; } });
48
+ Object.defineProperty(exports, "filterArrayAttr", { enumerable: true, get: function () { return array_1.filterArrayAttr; } });
49
+ var mapToArray_1 = require("./mapToArray");
50
+ Object.defineProperty(exports, "flattenTree", { enumerable: true, get: function () { return mapToArray_1.flattenTree; } });
51
+ Object.defineProperty(exports, "convertToArray", { enumerable: true, get: function () { return mapToArray_1.convertToArray; } });
46
52
  var object_1 = require("./object");
47
53
  // 对象
48
54
  Object.defineProperty(exports, "pick", { enumerable: true, get: function () { return object_1.pick; } });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 将 Map 转换为数组
3
+ * @param map 输入 Map
4
+ * @returns 转换后的数组
5
+ *
6
+ * @example
7
+ * convertToArray(new Map<string, number>([["a", 1], ["b", 2], ["c", 3]])) // => [1, 2, 3]
8
+ */
9
+ export declare function convertToArray<T>(map: Map<string, T>): T[];
10
+ //# sourceMappingURL=convertToArray.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertToArray.d.ts","sourceRoot":"","sources":["../../src/mapToArray/convertToArray.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,CAK1D"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToArray = void 0;
4
+ /**
5
+ * 将 Map 转换为数组
6
+ * @param map 输入 Map
7
+ * @returns 转换后的数组
8
+ *
9
+ * @example
10
+ * convertToArray(new Map<string, number>([["a", 1], ["b", 2], ["c", 3]])) // => [1, 2, 3]
11
+ */
12
+ function convertToArray(map) {
13
+ if (map.size === 0) {
14
+ return [];
15
+ }
16
+ return Array.from(map.values());
17
+ }
18
+ exports.convertToArray = convertToArray;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 将树形结构展平为数组
3
+ * @param tree 树形结构数组
4
+ * @param childrenKey 子节点在节点对象中的键名,默认为"children"
5
+ * @returns 展平后的数组
6
+ */
7
+ export declare function flattenTree(tree: Record<string, unknown>[], childrenKey?: string): Record<string, unknown>[];
8
+ //# sourceMappingURL=flattenTree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flattenTree.d.ts","sourceRoot":"","sources":["../../src/mapToArray/flattenTree.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EAAE,WAAW,GAAE,MAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAcxH"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flattenTree = void 0;
4
+ /**
5
+ * 将树形结构展平为数组
6
+ * @param tree 树形结构数组
7
+ * @param childrenKey 子节点在节点对象中的键名,默认为"children"
8
+ * @returns 展平后的数组
9
+ */
10
+ function flattenTree(tree, childrenKey = "children") {
11
+ if (!Array.isArray(tree)) {
12
+ throw new Error("tree must be an array");
13
+ }
14
+ const result = [];
15
+ tree.forEach((node) => {
16
+ result.push(node);
17
+ const children = node[childrenKey];
18
+ if (children && Array.isArray(children)) {
19
+ const flattenedChildren = flattenTree(children, childrenKey);
20
+ result.push(...flattenedChildren);
21
+ }
22
+ });
23
+ return result;
24
+ }
25
+ exports.flattenTree = flattenTree;
@@ -1,2 +1,3 @@
1
- export * from "./mapToArray";
1
+ export * from "./convertToArray";
2
+ export * from "./flattenTree";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mapToArray/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mapToArray/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC"}
@@ -14,4 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./mapToArray"), exports);
17
+ __exportStar(require("./convertToArray"), exports);
18
+ __exportStar(require("./flattenTree"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jz-toolkit",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "一个轻量级的JavaScript/TypeScript 工具库",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",