@zcrkey/js-utils 0.0.6 → 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.
@@ -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,15 +16,24 @@ 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
- // src/treeUtil.ts
20
- var treeUtil_exports = {};
21
- __export(treeUtil_exports, {
29
+ // src/tree.ts
30
+ var tree_exports = {};
31
+ __export(tree_exports, {
22
32
  default: () => CrTreeUtil
23
33
  });
24
- module.exports = __toCommonJS(treeUtil_exports);
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
@@ -91,7 +91,7 @@ export default class CrUtil {
91
91
  * @param settings.idField 默认值 id
92
92
  * @param settings.pidField 默认值 parentId
93
93
  * @param settings.childrenField 默认值 children
94
- * @deprecated 即将移除,使用 {@link CrObjUtil.listToTree} 替代
94
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.listToTree} 替代
95
95
  * @returns
96
96
  */
97
97
  static listToTreeData(listData: any[], settings?: {
@@ -108,7 +108,7 @@ export default class CrUtil {
108
108
  * @param settings.idField 默认值 id
109
109
  * @param settings.pidField 默认值 parentId
110
110
  * @param settings.childrenField 默认值 children
111
- * @deprecated 即将移除,使用 {@link CrObjUtil.treeToList} 替代
111
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.treeToList} 替代
112
112
  * @returns
113
113
  */
114
114
  static treeDataToListData(treeData: any[], pid?: string | number, settings?: {
@@ -125,7 +125,7 @@ export default class CrUtil {
125
125
  * @param settings.valueField 默认值 value
126
126
  * @param settings.idField 默认值 id
127
127
  * @param settings.pidField 默认值 parentId
128
- * @deprecated 即将移除,使用 {@link CrObjUtil.getFlatParentDatas} 替代
128
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.getFlatParentDatas} 替代
129
129
  * @returns
130
130
  */
131
131
  static getParentNodes<T, R extends Record<string, any>>(listData: R[], value: number | string, settings?: {
@@ -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
@@ -187,7 +187,7 @@ var CrUtil = class {
187
187
  * @param settings.idField 默认值 id
188
188
  * @param settings.pidField 默认值 parentId
189
189
  * @param settings.childrenField 默认值 children
190
- * @deprecated 即将移除,使用 {@link CrObjUtil.listToTree} 替代
190
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.listToTree} 替代
191
191
  * @returns
192
192
  */
193
193
  static listToTreeData(listData, settings) {
@@ -250,7 +250,7 @@ var CrUtil = class {
250
250
  * @param settings.idField 默认值 id
251
251
  * @param settings.pidField 默认值 parentId
252
252
  * @param settings.childrenField 默认值 children
253
- * @deprecated 即将移除,使用 {@link CrObjUtil.treeToList} 替代
253
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.treeToList} 替代
254
254
  * @returns
255
255
  */
256
256
  static treeDataToListData(treeData, pid = 0, settings) {
@@ -292,7 +292,7 @@ var CrUtil = class {
292
292
  * @param settings.valueField 默认值 value
293
293
  * @param settings.idField 默认值 id
294
294
  * @param settings.pidField 默认值 parentId
295
- * @deprecated 即将移除,使用 {@link CrObjUtil.getFlatParentDatas} 替代
295
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.getFlatParentDatas} 替代
296
296
  * @returns
297
297
  */
298
298
  static getParentNodes(listData, value, settings) {
@@ -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
  };
@@ -0,0 +1,195 @@
1
+ export default class CrColorUtil {
2
+ /**
3
+ * hex 转 rgb 数组 [r,g,b] r/g/b范围 0-255
4
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
5
+ */
6
+ static hexToRgb(hex: string): number[];
7
+ /**
8
+ * rgb 转 hex 标准格式
9
+ * @param r 0-255
10
+ * @param g 0-255
11
+ * @param b 0-255
12
+ */
13
+ static rgbToHex(r: number, g: number, b: number): string;
14
+ /**
15
+ * 将 rgb 格式转为 hsv 格式
16
+ * input: r,g,b in [0,1], out: h in [0,360) and s,v in [0,1]
17
+ * @param r 0-1
18
+ * @param g 0-1
19
+ * @param b 0-1
20
+ */
21
+ static rgb2hsv(r: number, g: number, b: number): number[];
22
+ /**
23
+ * 将 hsv 格式转为 rgb 格式
24
+ * input: h in [0,360] and s,v in [0,1] - output: r,g,b in [0,1]
25
+ * @param h 0-360
26
+ * @param s 0-1
27
+ * @param v 0-1
28
+ */
29
+ static hsv2rgb(h: number, s: number, v: number): number[];
30
+ /**
31
+ * hex 转 rgba 格式字符串 可直接用于css
32
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
33
+ * @param alpha 透明度 0-1 默认1(完全不透明)
34
+ * @returns 例: rgba(255,255,255,0.5)
35
+ */
36
+ static hexToRgba(hex: string, alpha?: number): string;
37
+ /**
38
+ * 给hex颜色添加透明度,返回 8位带透明通道的hex格式 #RRGGBBAA (CSS常用)
39
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
40
+ * @param alpha 透明度 0-1 默认1(完全不透明)
41
+ * @returns 例: #ffffff80 #ff000033
42
+ */
43
+ static hexAddAlpha(hex: string, alpha?: number): string;
44
+ /**
45
+ * rgba 转 hex 格式
46
+ * @param r 0-255
47
+ * @param g 0-255
48
+ * @param b 0-255
49
+ */
50
+ static rgbaToHex(r: number, g: number, b: number): string;
51
+ /**
52
+ * hsv 转 hex 十六进制颜色
53
+ * @param h 色相 0-360
54
+ * @param s 饱和度 0-1
55
+ * @param v 明度 0-1
56
+ */
57
+ static hsvToHex(h: number, s: number, v: number): string;
58
+ /**
59
+ * hex 转 hsv 格式
60
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
61
+ * @returns [h:0-360, s:0-1, v:0-1]
62
+ */
63
+ static hexToHsv(hex: string): number[];
64
+ /**
65
+ * 颜色加深处理 - 基于hex颜色
66
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
67
+ * @param level 加深程度 0-1 0不变 1最深(接近黑色)
68
+ */
69
+ static darkenColor(hex: string, level: number): string;
70
+ /**
71
+ * 颜色减淡/提亮处理 - 基于hex颜色
72
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
73
+ * @param level 减淡程度 0-1 0不变 1最亮(接近白色)
74
+ */
75
+ static lightenColor(hex: string, level: number): string;
76
+ /**
77
+ * 解析8位带透明的hex #RRGGBBAA 为 普通hex + 透明度
78
+ * @param hex8 8位带透明的十六进制颜色 例:#ffffff80
79
+ * @returns { hex: '#ffffff', alpha: 0.5 }
80
+ */
81
+ static parseHex8(hex8: string): {
82
+ hex: string;
83
+ alpha: number;
84
+ };
85
+ /**
86
+ * rgb 数组 转 rgb/rgba 字符串
87
+ * @param rgb [r,g,b] 0-255
88
+ * @param alpha 透明度0-1 传则返回rgba,不传返回rgb
89
+ */
90
+ static rgbToStr(rgb: number[], alpha?: number): string;
91
+ /**
92
+ * Hex颜色字符串 转 十进制数值(6位纯颜色值,无透明通道,专用于颜色运算)
93
+ * @param hex 支持 #fff / #ffffff / fff / ffffff 格式
94
+ * @returns 十进制数值 例:#ff0000 → 16711680,非法入参返回 0
95
+ */
96
+ static hexToNumber(hex: string): number;
97
+ /**
98
+ * 十进制数值 转回 Hex颜色字符串(无透明通道 标准6位)
99
+ * @param num 十进制数值 例:16711680 → #ff0000
100
+ * @returns 标准6位十六进制颜色 #RRGGBB 格式
101
+ */
102
+ static numberToHex(num: number): string;
103
+ /**
104
+ * ARGB颜色字符串 转 十进制数值(8位带透明通道,专用于带透明的颜色运算)
105
+ * @param argb 支持 #AARRGGBB / AARRGGBB 格式 例:#80FFFFFF、80FFFFFF
106
+ * @returns 十进制数值 例:#80FFFFFF → 2147483647,非法入参/格式错误返回 0
107
+ */
108
+ static argbToNumber(argb: string): number;
109
+ /**
110
+ * 十进制数值 转回 ARGB颜色字符串(8位带透明通道,标准#AARRGGBB格式)
111
+ * @param num 十进制数值 例:2147483647 → #80ffffff
112
+ * @returns 标准ARGB颜色字符串 #AARRGGBB 小写格式
113
+ */
114
+ static numberToArgb(num: number): string;
115
+ /**
116
+ * 生成随机Hex颜色
117
+ * @param withSharp 是否带#号 默认为true
118
+ * @returns 随机hex颜色
119
+ */
120
+ static randomHexColor(withSharp?: boolean): string;
121
+ /**
122
+ * 反色处理 - 取颜色的补色(比如黑色→白色,白色→黑色)
123
+ * @param hex 目标颜色
124
+ * @returns 反色hex
125
+ */
126
+ static reverseColor(hex: string): string;
127
+ /**
128
+ * 校验是否为合法的hex颜色
129
+ * @param hex 待校验的颜色字符串
130
+ * @returns true=合法 false=不合法
131
+ */
132
+ static isHexColor(hex: string): boolean;
133
+ /**
134
+ * 标准6位Hex颜色 转 ARGB 8位颜色格式 ( 核心:ARGB格式是 #AARRGGBB )
135
+ * @param hex 6位标准hex 支持 #ffffff / ffffff / #fff / fff
136
+ * @param alpha 透明度 0~1 默认 1 (完全不透明)
137
+ * @returns 8位ARGB格式颜色 例:#80FFFFFF 半透明白色
138
+ */
139
+ static hexToArgb(hex: string, alpha?: number): string;
140
+ /**
141
+ * ARGB 8位颜色格式 转回 标准6位Hex颜色 + 独立的透明度值 ( 反向解析 )
142
+ * @param argb 8位ARGB格式颜色 必须是 #AARRGGBB 格式 例:#80FFFFFF
143
+ * @returns { hex: '#ffffff', alpha: 0.5 } 标准hex + 0~1的透明度
144
+ */
145
+ static argbToHex(argb: string): {
146
+ hex: string;
147
+ alpha: number;
148
+ };
149
+ /**
150
+ * ARGB格式(#AARRGGBB) 转 RRGGBBAA格式(#RRGGBBAA)
151
+ * @param argb #AARRGGBB
152
+ * @returns #RRGGBBAA
153
+ */
154
+ static argbToRgbaHex(argb: string): string;
155
+ /**
156
+ * RRGGBBAA格式(#RRGGBBAA) 转 ARGB格式(#AARRGGBB)
157
+ * @param rgbaHex #RRGGBBAA
158
+ * @returns #AARRGGBB
159
+ */
160
+ static rgbaHexToArgb(rgbaHex: string): string;
161
+ /**
162
+ * 调整颜色饱和度
163
+ * @param hex 目标颜色
164
+ * @param level 调整程度 0-2 1=不变 <1=降低饱和度 >1=提高饱和度
165
+ * @returns 调整后的hex颜色
166
+ */
167
+ static adjustSaturation(hex: string, level: number): string;
168
+ /**
169
+ * 调整颜色明度
170
+ * @param hex 目标颜色
171
+ * @param level 调整程度 0-2 1=不变 <1=降低明度 >1=提高明度
172
+ * @returns 调整后的hex颜色
173
+ */
174
+ static adjustBrightness(hex: string, level: number): string;
175
+ /**
176
+ * 普通6位Hex+透明度 → 直接返回ARGB对应的十进制数值
177
+ * @param hex 支持 #fff / #ffffff 格式
178
+ * @param alpha 透明度 0-1
179
+ * @returns ARGB十进制数值
180
+ */
181
+ static hexAlphaToArgbNumber(hex: string, alpha?: number): number;
182
+ /**
183
+ * ARGB十进制数值 → 解析出 普通Hex + 0-1透明度
184
+ * @param num ARGB十进制数值
185
+ * @returns { hex: '#ffffff', alpha: 0.5 }
186
+ */
187
+ static argbNumberToHexAlpha(num: number): {
188
+ hex: string;
189
+ alpha: number;
190
+ };
191
+ /**
192
+ * 补零成两位字符
193
+ */
194
+ private static twoDigits;
195
+ }