@zcrkey/js-utils 0.0.7 → 0.0.9

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.
@@ -101,10 +101,10 @@ export default class CrColorUtil {
101
101
  */
102
102
  static numberToHex(num: number): string;
103
103
  /**
104
- * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
105
- * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
106
- * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
107
- */
104
+ * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
105
+ * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
106
+ * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
107
+ */
108
108
  static argbToNumber(argb: string): number;
109
109
  /**
110
110
  * 十进制数值 转回 ARGB颜色字符串(8位带透明通道,标准#AARRGGBB格式)
package/dist/cjs/color.js CHANGED
@@ -116,7 +116,11 @@ var CrColorUtil = class {
116
116
  */
117
117
  static hsvToHex(h, s, v) {
118
118
  const [r, g, b] = this.hsv2rgb(h, s, v);
119
- return this.rgbToHex(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255));
119
+ return this.rgbToHex(
120
+ Math.round(r * 255),
121
+ Math.round(g * 255),
122
+ Math.round(b * 255)
123
+ );
120
124
  }
121
125
  /**
122
126
  * hex 转 hsv 格式
@@ -200,10 +204,10 @@ var CrColorUtil = class {
200
204
  return `#${r}${g}${b}`;
201
205
  }
202
206
  /**
203
- * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
204
- * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
205
- * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
206
- */
207
+ * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
208
+ * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
209
+ * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
210
+ */
207
211
  static argbToNumber(argb) {
208
212
  const pureArgb = argb.replace("#", "");
209
213
  if (!/^[0-9a-fA-F]{8}$/.test(pureArgb)) {
@@ -1,3 +1,4 @@
1
+ export { default as CrColorUtil } from './color';
1
2
  export type * from './eventCenter';
2
3
  export { default as CrEventCenter } from './eventCenter';
3
4
  export type * from './obj';
@@ -6,4 +7,3 @@ export { default as CrStorage } from './storage';
6
7
  export type * from './tree';
7
8
  export { default as CrTreeUtil } from './tree';
8
9
  export { default as CrUtil } from './util';
9
- export { default as CrColorUtil } from './color';
package/dist/cjs/index.js CHANGED
@@ -37,12 +37,12 @@ __export(src_exports, {
37
37
  CrUtil: () => import_util.default
38
38
  });
39
39
  module.exports = __toCommonJS(src_exports);
40
+ var import_color = __toESM(require("./color"));
40
41
  var import_eventCenter = __toESM(require("./eventCenter"));
41
42
  var import_obj = __toESM(require("./obj"));
42
43
  var import_storage = __toESM(require("./storage"));
43
44
  var import_tree = __toESM(require("./tree"));
44
45
  var import_util = __toESM(require("./util"));
45
- var import_color = __toESM(require("./color"));
46
46
  // Annotate the CommonJS export names for ESM import in node:
47
47
  0 && (module.exports = {
48
48
  CrColorUtil,
@@ -21,13 +21,17 @@ export default class CrTreeUtil {
21
21
  * 树形数据转列表数据
22
22
  * @param tree
23
23
  * @param options
24
+ * @param options.idField 默认值 id
25
+ * @param options.parentIdField 默认值 parentId
24
26
  * @param options.childrenField 默认值 children
25
- * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
27
+ * @param options.rootParentValue 根节点的父值,默认值 null
26
28
  * @returns
27
29
  */
28
30
  static treeToList<T extends Record<string, any>>(tree: T[], options?: {
31
+ idField?: string;
32
+ parentIdField?: string;
29
33
  childrenField?: string;
30
- isCloneDeep?: boolean;
34
+ rootParentValue?: any;
31
35
  }): T[];
32
36
  /**
33
37
  * 获取扁平化父数据(包含自身)
package/dist/cjs/tree.js CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // src/tree.ts
@@ -23,6 +33,7 @@ __export(tree_exports, {
23
33
  });
24
34
  module.exports = __toCommonJS(tree_exports);
25
35
  var import_lodash = require("lodash");
36
+ var import_util = __toESM(require("./util"));
26
37
  var CrTreeUtil = class {
27
38
  /**
28
39
  * 列表数据转树形数据
@@ -83,24 +94,47 @@ var CrTreeUtil = class {
83
94
  * 树形数据转列表数据
84
95
  * @param tree
85
96
  * @param options
97
+ * @param options.idField 默认值 id
98
+ * @param options.parentIdField 默认值 parentId
86
99
  * @param options.childrenField 默认值 children
87
- * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
100
+ * @param options.rootParentValue 根节点的父值,默认值 null
88
101
  * @returns
89
102
  */
90
103
  static treeToList(tree, options) {
91
- const { childrenField = "children", isCloneDeep = true } = options || {};
104
+ const {
105
+ idField = "id",
106
+ parentIdField = "parentId",
107
+ childrenField = "children",
108
+ rootParentValue = null
109
+ } = options || {};
92
110
  const result = [];
93
- const queue = [...tree];
111
+ const originQueue = (0, import_lodash.cloneDeep)([...tree]);
112
+ const queue = originQueue.map((node) => ({
113
+ node,
114
+ parentId: rootParentValue
115
+ }));
94
116
  while (queue.length) {
95
- const node = queue.shift();
117
+ const { node, parentId } = queue.shift();
118
+ if (!node || !import_util.default.isObject(node)) {
119
+ continue;
120
+ }
96
121
  const children = node[childrenField];
122
+ const nodeId = node[idField];
97
123
  const { [childrenField]: _, ...rest } = node;
98
- result.push(rest);
99
- if (children && children.length) {
100
- queue.push(...children);
124
+ const flatNode = {
125
+ ...rest,
126
+ [parentIdField]: parentId
127
+ };
128
+ result.push(flatNode);
129
+ if (children && import_util.default.isArray(children) && children.length > 0) {
130
+ const childQueue = children.map((childNode) => ({
131
+ node: childNode,
132
+ parentId: nodeId
133
+ }));
134
+ queue.push(...childQueue);
101
135
  }
102
136
  }
103
- return isCloneDeep ? (0, import_lodash.cloneDeep)(result) : result;
137
+ return result;
104
138
  }
105
139
  /**
106
140
  * 获取扁平化父数据(包含自身)
@@ -10,7 +10,7 @@ export default class CrUtil {
10
10
  * @param value
11
11
  * @returns boolean
12
12
  */
13
- static isObject<T = any>(value: T): value is T extends object ? (T extends any[] ? never : T) : never;
13
+ static isObject(value: unknown): value is Record<string | number, unknown>;
14
14
  /**
15
15
  * 判断是否为空对象
16
16
  * @param value
@@ -201,9 +201,21 @@ export default class CrUtil {
201
201
  */
202
202
  static compareDataDiff(data1: any, data2: any): Record<string, "added" | "removed" | "modified">;
203
203
  /**
204
- * 格式化为千分位
204
+ * 格式化为千分位,主要用于展示格式化
205
205
  * @param value
206
206
  * @returns 12345.6789 => 12,345.6789
207
207
  */
208
208
  static fmtThousands(value: number | string | undefined): string;
209
+ /**
210
+ * 格式化为千分位格式
211
+ * @param value
212
+ * @returns
213
+ */
214
+ static formatThousands(value?: string | number): string;
215
+ /**
216
+ * 解析千分位格式
217
+ * @param value
218
+ * @returns
219
+ */
220
+ static parseThousands(value?: string | number): string;
209
221
  }
package/dist/cjs/util.js CHANGED
@@ -557,7 +557,7 @@ var CrUtil = class {
557
557
  return diffFieldSet;
558
558
  }
559
559
  /**
560
- * 格式化为千分位
560
+ * 格式化为千分位,主要用于展示格式化
561
561
  * @param value
562
562
  * @returns 12345.6789 => 12,345.6789
563
563
  */
@@ -569,4 +569,39 @@ var CrUtil = class {
569
569
  const formattedInt = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
570
570
  return decimalPart ? `${formattedInt}.${decimalPart}` : formattedInt;
571
571
  }
572
+ /**
573
+ * 格式化为千分位格式
574
+ * @param value
575
+ * @returns
576
+ */
577
+ static formatThousands(value) {
578
+ if (value == null)
579
+ return "";
580
+ const raw = String(value).trim();
581
+ if (!raw)
582
+ return "";
583
+ const hasTrailingDot = raw.endsWith(".");
584
+ let str = raw.replace(/[^\d.-]/g, "");
585
+ const sign = str.startsWith("-") ? "-" : "";
586
+ str = str.replace(/-/g, "");
587
+ const [intRaw, decRaw = ""] = str.split(".", 2);
588
+ const intPart = intRaw.replace(/,/g, "").replace(/^0+(?=\d)/, "");
589
+ const formattedInt = intPart ? intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",") : "";
590
+ let result = sign + formattedInt;
591
+ if (decRaw || hasTrailingDot)
592
+ result += ".";
593
+ if (decRaw)
594
+ result += decRaw;
595
+ return result;
596
+ }
597
+ /**
598
+ * 解析千分位格式
599
+ * @param value
600
+ * @returns
601
+ */
602
+ static parseThousands(value) {
603
+ if (value == null)
604
+ return "";
605
+ return String(value).trim().replace(/,/g, "").replace(/[^\d.-]/g, "").replace(/(?!^)-/g, "").replace(/(\..*)\./g, "$1");
606
+ }
572
607
  };
@@ -101,10 +101,10 @@ export default class CrColorUtil {
101
101
  */
102
102
  static numberToHex(num: number): string;
103
103
  /**
104
- * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
105
- * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
106
- * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
107
- */
104
+ * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
105
+ * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
106
+ * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
107
+ */
108
108
  static argbToNumber(argb: string): number;
109
109
  /**
110
110
  * 十进制数值 转回 ARGB颜色字符串(8位带透明通道,标准#AARRGGBB格式)