@zcrkey/js-utils 0.0.7 → 0.0.10

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)) {
@@ -0,0 +1,71 @@
1
+ /**
2
+ * 文件类型
3
+ * - OTHER 其他
4
+ * - IMAGE 图片
5
+ * - VIDEO 视频
6
+ * - AUDIO 音频
7
+ * - DOCUMENT 文档
8
+ * - ARCHIVE 压缩包
9
+ */
10
+ export declare enum FileTypeEnum {
11
+ OTHER = "other",
12
+ IMAGE = "image",
13
+ VIDEO = "video",
14
+ AUDIO = "audio",
15
+ DOCUMENT = "document",
16
+ ARCHIVE = "archive"
17
+ }
18
+ /**
19
+ * 文档类型
20
+ * - OTHER
21
+ * - PDF
22
+ * - DOC
23
+ * - DOCX
24
+ * - XLS
25
+ * - XLSX
26
+ * - PPT
27
+ * - PPTX
28
+ * - TXT
29
+ */
30
+ export declare const enum DocumentTypeEnum {
31
+ OTHER = "other",
32
+ PDF = "pdf",
33
+ DOC = "doc",
34
+ DOCX = "docx",
35
+ XLS = "xls",
36
+ XLSX = "xlsx",
37
+ PPT = "ppt",
38
+ PPTX = "pptx",
39
+ TXT = "txt"
40
+ }
41
+ /**
42
+ * 文件元信息
43
+ */
44
+ export interface CrFileMeta {
45
+ ext: string;
46
+ type: FileTypeEnum;
47
+ documentType?: DocumentTypeEnum;
48
+ }
49
+ export declare class CrFileUtil {
50
+ /**
51
+ * 提取扩展名(不含 .)
52
+ */
53
+ static getExt(filename?: string): string;
54
+ /**
55
+ * 获取文件完整信息
56
+ */
57
+ static getMeta(filename: string): CrFileMeta;
58
+ /**
59
+ * 文件类型
60
+ */
61
+ static getType(filename: string): FileTypeEnum;
62
+ /**
63
+ * 文档类型
64
+ */
65
+ static getDocumentType(filename: string): DocumentTypeEnum;
66
+ static isImage(filename: string): boolean;
67
+ static isVideo(filename: string): boolean;
68
+ static isAudio(filename: string): boolean;
69
+ static isDocument(filename: string): boolean;
70
+ static isArchive(filename: string): boolean;
71
+ }
@@ -0,0 +1,154 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/file.ts
20
+ var file_exports = {};
21
+ __export(file_exports, {
22
+ CrFileUtil: () => CrFileUtil,
23
+ DocumentTypeEnum: () => DocumentTypeEnum,
24
+ FileTypeEnum: () => FileTypeEnum
25
+ });
26
+ module.exports = __toCommonJS(file_exports);
27
+ var FileTypeEnum = /* @__PURE__ */ ((FileTypeEnum2) => {
28
+ FileTypeEnum2["OTHER"] = "other";
29
+ FileTypeEnum2["IMAGE"] = "image";
30
+ FileTypeEnum2["VIDEO"] = "video";
31
+ FileTypeEnum2["AUDIO"] = "audio";
32
+ FileTypeEnum2["DOCUMENT"] = "document";
33
+ FileTypeEnum2["ARCHIVE"] = "archive";
34
+ return FileTypeEnum2;
35
+ })(FileTypeEnum || {});
36
+ var DocumentTypeEnum = /* @__PURE__ */ ((DocumentTypeEnum2) => {
37
+ DocumentTypeEnum2["OTHER"] = "other";
38
+ DocumentTypeEnum2["PDF"] = "pdf";
39
+ DocumentTypeEnum2["DOC"] = "doc";
40
+ DocumentTypeEnum2["DOCX"] = "docx";
41
+ DocumentTypeEnum2["XLS"] = "xls";
42
+ DocumentTypeEnum2["XLSX"] = "xlsx";
43
+ DocumentTypeEnum2["PPT"] = "ppt";
44
+ DocumentTypeEnum2["PPTX"] = "pptx";
45
+ DocumentTypeEnum2["TXT"] = "txt";
46
+ return DocumentTypeEnum2;
47
+ })(DocumentTypeEnum || {});
48
+ var FILE_META_LIST = [
49
+ /* ---------- image ---------- */
50
+ { ext: "jpg", type: "image" /* IMAGE */ },
51
+ { ext: "jpeg", type: "image" /* IMAGE */ },
52
+ { ext: "png", type: "image" /* IMAGE */ },
53
+ { ext: "gif", type: "image" /* IMAGE */ },
54
+ { ext: "bmp", type: "image" /* IMAGE */ },
55
+ { ext: "webp", type: "image" /* IMAGE */ },
56
+ { ext: "svg", type: "image" /* IMAGE */ },
57
+ { ext: "ico", type: "image" /* IMAGE */ },
58
+ { ext: "heic", type: "image" /* IMAGE */ },
59
+ { ext: "avif", type: "image" /* IMAGE */ },
60
+ /* ---------- video ---------- */
61
+ { ext: "mp4", type: "video" /* VIDEO */ },
62
+ { ext: "mkv", type: "video" /* VIDEO */ },
63
+ { ext: "mov", type: "video" /* VIDEO */ },
64
+ { ext: "avi", type: "video" /* VIDEO */ },
65
+ { ext: "webm", type: "video" /* VIDEO */ },
66
+ { ext: "flv", type: "video" /* VIDEO */ },
67
+ /* ---------- audio ---------- */
68
+ { ext: "mp3", type: "audio" /* AUDIO */ },
69
+ { ext: "wav", type: "audio" /* AUDIO */ },
70
+ { ext: "aac", type: "audio" /* AUDIO */ },
71
+ { ext: "flac", type: "audio" /* AUDIO */ },
72
+ { ext: "ogg", type: "audio" /* AUDIO */ },
73
+ { ext: "m4a", type: "audio" /* AUDIO */ },
74
+ /* ---------- document ---------- */
75
+ { ext: "pdf", type: "document" /* DOCUMENT */, documentType: "pdf" /* PDF */ },
76
+ { ext: "doc", type: "document" /* DOCUMENT */, documentType: "doc" /* DOC */ },
77
+ { ext: "docx", type: "document" /* DOCUMENT */, documentType: "docx" /* DOCX */ },
78
+ { ext: "xls", type: "document" /* DOCUMENT */, documentType: "xls" /* XLS */ },
79
+ { ext: "xlsx", type: "document" /* DOCUMENT */, documentType: "xlsx" /* XLSX */ },
80
+ { ext: "ppt", type: "document" /* DOCUMENT */, documentType: "ppt" /* PPT */ },
81
+ { ext: "pptx", type: "document" /* DOCUMENT */, documentType: "pptx" /* PPTX */ },
82
+ { ext: "txt", type: "document" /* DOCUMENT */, documentType: "txt" /* TXT */ },
83
+ { ext: "md", type: "document" /* DOCUMENT */ },
84
+ { ext: "csv", type: "document" /* DOCUMENT */ },
85
+ { ext: "json", type: "document" /* DOCUMENT */ },
86
+ /* ---------- archive ---------- */
87
+ { ext: "zip", type: "archive" /* ARCHIVE */ },
88
+ { ext: "rar", type: "archive" /* ARCHIVE */ },
89
+ { ext: "7z", type: "archive" /* ARCHIVE */ },
90
+ { ext: "tar", type: "archive" /* ARCHIVE */ },
91
+ { ext: "gz", type: "archive" /* ARCHIVE */ }
92
+ ];
93
+ var FILE_META_MAP = /* @__PURE__ */ new Map();
94
+ FILE_META_LIST.forEach((item) => {
95
+ FILE_META_MAP.set(item.ext, item);
96
+ });
97
+ var DEFAULT_META = {
98
+ ext: "",
99
+ type: "other" /* OTHER */,
100
+ documentType: "other" /* OTHER */
101
+ };
102
+ var CrFileUtil = class {
103
+ /**
104
+ * 提取扩展名(不含 .)
105
+ */
106
+ static getExt(filename) {
107
+ var _a;
108
+ if (!filename || typeof filename !== "string")
109
+ return "";
110
+ return ((_a = filename.trim().split(".").pop()) == null ? void 0 : _a.toLowerCase()) || "";
111
+ }
112
+ /**
113
+ * 获取文件完整信息
114
+ */
115
+ static getMeta(filename) {
116
+ const ext = this.getExt(filename);
117
+ if (!ext)
118
+ return DEFAULT_META;
119
+ return FILE_META_MAP.get(ext) || { ...DEFAULT_META, ext };
120
+ }
121
+ /**
122
+ * 文件类型
123
+ */
124
+ static getType(filename) {
125
+ return this.getMeta(filename).type;
126
+ }
127
+ /**
128
+ * 文档类型
129
+ */
130
+ static getDocumentType(filename) {
131
+ return this.getMeta(filename).documentType || "other" /* OTHER */;
132
+ }
133
+ static isImage(filename) {
134
+ return this.getType(filename) === "image" /* IMAGE */;
135
+ }
136
+ static isVideo(filename) {
137
+ return this.getType(filename) === "video" /* VIDEO */;
138
+ }
139
+ static isAudio(filename) {
140
+ return this.getType(filename) === "audio" /* AUDIO */;
141
+ }
142
+ static isDocument(filename) {
143
+ return this.getType(filename) === "document" /* DOCUMENT */;
144
+ }
145
+ static isArchive(filename) {
146
+ return this.getType(filename) === "archive" /* ARCHIVE */;
147
+ }
148
+ };
149
+ // Annotate the CommonJS export names for ESM import in node:
150
+ 0 && (module.exports = {
151
+ CrFileUtil,
152
+ DocumentTypeEnum,
153
+ FileTypeEnum
154
+ });
@@ -1,3 +1,6 @@
1
+ export { default as CrColorUtil } from './color';
2
+ export * from './file';
3
+ export type * from './file';
1
4
  export type * from './eventCenter';
2
5
  export { default as CrEventCenter } from './eventCenter';
3
6
  export type * from './obj';
@@ -6,4 +9,3 @@ export { default as CrStorage } from './storage';
6
9
  export type * from './tree';
7
10
  export { default as CrTreeUtil } from './tree';
8
11
  export { default as CrUtil } from './util';
9
- export { default as CrColorUtil } from './color';
package/dist/cjs/index.js CHANGED
@@ -16,6 +16,7 @@ var __copyProps = (to, from, except, desc) => {
16
16
  }
17
17
  return to;
18
18
  };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
21
  // If the importer is in node compatibility mode or this is not an ESM
21
22
  // file that has been converted to a CommonJS file using a Babel-
@@ -37,12 +38,13 @@ __export(src_exports, {
37
38
  CrUtil: () => import_util.default
38
39
  });
39
40
  module.exports = __toCommonJS(src_exports);
41
+ var import_color = __toESM(require("./color"));
42
+ __reExport(src_exports, require("./file"), module.exports);
40
43
  var import_eventCenter = __toESM(require("./eventCenter"));
41
44
  var import_obj = __toESM(require("./obj"));
42
45
  var import_storage = __toESM(require("./storage"));
43
46
  var import_tree = __toESM(require("./tree"));
44
47
  var import_util = __toESM(require("./util"));
45
- var import_color = __toESM(require("./color"));
46
48
  // Annotate the CommonJS export names for ESM import in node:
47
49
  0 && (module.exports = {
48
50
  CrColorUtil,
@@ -50,5 +52,6 @@ var import_color = __toESM(require("./color"));
50
52
  CrObjUtil,
51
53
  CrStorage,
52
54
  CrTreeUtil,
53
- CrUtil
55
+ CrUtil,
56
+ ...require("./file")
54
57
  });
@@ -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格式)