@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.
@@ -0,0 +1,267 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
7
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
8
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
9
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
10
+ /**
11
+ * 文件类型
12
+ * - OTHER 其他
13
+ * - IMAGE 图片
14
+ * - VIDEO 视频
15
+ * - AUDIO 音频
16
+ * - DOCUMENT 文档
17
+ * - ARCHIVE 压缩包
18
+ */
19
+ export var FileTypeEnum = /*#__PURE__*/function (FileTypeEnum) {
20
+ FileTypeEnum["OTHER"] = "other";
21
+ FileTypeEnum["IMAGE"] = "image";
22
+ FileTypeEnum["VIDEO"] = "video";
23
+ FileTypeEnum["AUDIO"] = "audio";
24
+ FileTypeEnum["DOCUMENT"] = "document";
25
+ FileTypeEnum["ARCHIVE"] = "archive";
26
+ return FileTypeEnum;
27
+ }({});
28
+
29
+ /**
30
+ * 文档类型
31
+ * - OTHER
32
+ * - PDF
33
+ * - DOC
34
+ * - DOCX
35
+ * - XLS
36
+ * - XLSX
37
+ * - PPT
38
+ * - PPTX
39
+ * - TXT
40
+ */
41
+ export var DocumentTypeEnum = {
42
+ OTHER: "other",
43
+ PDF: "pdf",
44
+ DOC: "doc",
45
+ DOCX: "docx",
46
+ XLS: "xls",
47
+ XLSX: "xlsx",
48
+ PPT: "ppt",
49
+ PPTX: "pptx",
50
+ TXT: "txt"
51
+ };
52
+
53
+ /**
54
+ * 文件元信息
55
+ */
56
+
57
+ var FILE_META_LIST = [/* ---------- image ---------- */
58
+ {
59
+ ext: 'jpg',
60
+ type: FileTypeEnum.IMAGE
61
+ }, {
62
+ ext: 'jpeg',
63
+ type: FileTypeEnum.IMAGE
64
+ }, {
65
+ ext: 'png',
66
+ type: FileTypeEnum.IMAGE
67
+ }, {
68
+ ext: 'gif',
69
+ type: FileTypeEnum.IMAGE
70
+ }, {
71
+ ext: 'bmp',
72
+ type: FileTypeEnum.IMAGE
73
+ }, {
74
+ ext: 'webp',
75
+ type: FileTypeEnum.IMAGE
76
+ }, {
77
+ ext: 'svg',
78
+ type: FileTypeEnum.IMAGE
79
+ }, {
80
+ ext: 'ico',
81
+ type: FileTypeEnum.IMAGE
82
+ }, {
83
+ ext: 'heic',
84
+ type: FileTypeEnum.IMAGE
85
+ }, {
86
+ ext: 'avif',
87
+ type: FileTypeEnum.IMAGE
88
+ }, /* ---------- video ---------- */
89
+ {
90
+ ext: 'mp4',
91
+ type: FileTypeEnum.VIDEO
92
+ }, {
93
+ ext: 'mkv',
94
+ type: FileTypeEnum.VIDEO
95
+ }, {
96
+ ext: 'mov',
97
+ type: FileTypeEnum.VIDEO
98
+ }, {
99
+ ext: 'avi',
100
+ type: FileTypeEnum.VIDEO
101
+ }, {
102
+ ext: 'webm',
103
+ type: FileTypeEnum.VIDEO
104
+ }, {
105
+ ext: 'flv',
106
+ type: FileTypeEnum.VIDEO
107
+ }, /* ---------- audio ---------- */
108
+ {
109
+ ext: 'mp3',
110
+ type: FileTypeEnum.AUDIO
111
+ }, {
112
+ ext: 'wav',
113
+ type: FileTypeEnum.AUDIO
114
+ }, {
115
+ ext: 'aac',
116
+ type: FileTypeEnum.AUDIO
117
+ }, {
118
+ ext: 'flac',
119
+ type: FileTypeEnum.AUDIO
120
+ }, {
121
+ ext: 'ogg',
122
+ type: FileTypeEnum.AUDIO
123
+ }, {
124
+ ext: 'm4a',
125
+ type: FileTypeEnum.AUDIO
126
+ }, /* ---------- document ---------- */
127
+ {
128
+ ext: 'pdf',
129
+ type: FileTypeEnum.DOCUMENT,
130
+ documentType: DocumentTypeEnum.PDF
131
+ }, {
132
+ ext: 'doc',
133
+ type: FileTypeEnum.DOCUMENT,
134
+ documentType: DocumentTypeEnum.DOC
135
+ }, {
136
+ ext: 'docx',
137
+ type: FileTypeEnum.DOCUMENT,
138
+ documentType: DocumentTypeEnum.DOCX
139
+ }, {
140
+ ext: 'xls',
141
+ type: FileTypeEnum.DOCUMENT,
142
+ documentType: DocumentTypeEnum.XLS
143
+ }, {
144
+ ext: 'xlsx',
145
+ type: FileTypeEnum.DOCUMENT,
146
+ documentType: DocumentTypeEnum.XLSX
147
+ }, {
148
+ ext: 'ppt',
149
+ type: FileTypeEnum.DOCUMENT,
150
+ documentType: DocumentTypeEnum.PPT
151
+ }, {
152
+ ext: 'pptx',
153
+ type: FileTypeEnum.DOCUMENT,
154
+ documentType: DocumentTypeEnum.PPTX
155
+ }, {
156
+ ext: 'txt',
157
+ type: FileTypeEnum.DOCUMENT,
158
+ documentType: DocumentTypeEnum.TXT
159
+ }, {
160
+ ext: 'md',
161
+ type: FileTypeEnum.DOCUMENT
162
+ }, {
163
+ ext: 'csv',
164
+ type: FileTypeEnum.DOCUMENT
165
+ }, {
166
+ ext: 'json',
167
+ type: FileTypeEnum.DOCUMENT
168
+ }, /* ---------- archive ---------- */
169
+ {
170
+ ext: 'zip',
171
+ type: FileTypeEnum.ARCHIVE
172
+ }, {
173
+ ext: 'rar',
174
+ type: FileTypeEnum.ARCHIVE
175
+ }, {
176
+ ext: '7z',
177
+ type: FileTypeEnum.ARCHIVE
178
+ }, {
179
+ ext: 'tar',
180
+ type: FileTypeEnum.ARCHIVE
181
+ }, {
182
+ ext: 'gz',
183
+ type: FileTypeEnum.ARCHIVE
184
+ }];
185
+ var FILE_META_MAP = new Map();
186
+ FILE_META_LIST.forEach(function (item) {
187
+ FILE_META_MAP.set(item.ext, item);
188
+ });
189
+ var DEFAULT_META = {
190
+ ext: '',
191
+ type: FileTypeEnum.OTHER,
192
+ documentType: DocumentTypeEnum.OTHER
193
+ };
194
+ export var CrFileUtil = /*#__PURE__*/function () {
195
+ function CrFileUtil() {
196
+ _classCallCheck(this, CrFileUtil);
197
+ }
198
+ _createClass(CrFileUtil, null, [{
199
+ key: "getExt",
200
+ value:
201
+ /**
202
+ * 提取扩展名(不含 .)
203
+ */
204
+ function getExt(filename) {
205
+ var _filename$trim$split$;
206
+ if (!filename || typeof filename !== 'string') return '';
207
+ return ((_filename$trim$split$ = filename.trim().split('.').pop()) === null || _filename$trim$split$ === void 0 ? void 0 : _filename$trim$split$.toLowerCase()) || '';
208
+ }
209
+
210
+ /**
211
+ * 获取文件完整信息
212
+ */
213
+ }, {
214
+ key: "getMeta",
215
+ value: function getMeta(filename) {
216
+ var ext = this.getExt(filename);
217
+ if (!ext) return DEFAULT_META;
218
+ return FILE_META_MAP.get(ext) || _objectSpread(_objectSpread({}, DEFAULT_META), {}, {
219
+ ext: ext
220
+ });
221
+ }
222
+
223
+ /**
224
+ * 文件类型
225
+ */
226
+ }, {
227
+ key: "getType",
228
+ value: function getType(filename) {
229
+ return this.getMeta(filename).type;
230
+ }
231
+
232
+ /**
233
+ * 文档类型
234
+ */
235
+ }, {
236
+ key: "getDocumentType",
237
+ value: function getDocumentType(filename) {
238
+ return this.getMeta(filename).documentType || DocumentTypeEnum.OTHER;
239
+ }
240
+ }, {
241
+ key: "isImage",
242
+ value: function isImage(filename) {
243
+ return this.getType(filename) === FileTypeEnum.IMAGE;
244
+ }
245
+ }, {
246
+ key: "isVideo",
247
+ value: function isVideo(filename) {
248
+ return this.getType(filename) === FileTypeEnum.VIDEO;
249
+ }
250
+ }, {
251
+ key: "isAudio",
252
+ value: function isAudio(filename) {
253
+ return this.getType(filename) === FileTypeEnum.AUDIO;
254
+ }
255
+ }, {
256
+ key: "isDocument",
257
+ value: function isDocument(filename) {
258
+ return this.getType(filename) === FileTypeEnum.DOCUMENT;
259
+ }
260
+ }, {
261
+ key: "isArchive",
262
+ value: function isArchive(filename) {
263
+ return this.getType(filename) === FileTypeEnum.ARCHIVE;
264
+ }
265
+ }]);
266
+ return CrFileUtil;
267
+ }();
@@ -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/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
+ export { default as CrColorUtil } from "./color";
2
+ export * from "./file";
1
3
  export { default as CrEventCenter } from "./eventCenter";
2
4
  export { default as CrObjUtil } from "./obj";
3
5
  export { default as CrStorage } from "./storage";
4
6
  export { default as CrTreeUtil } from "./tree";
5
- export { default as CrUtil } from "./util";
6
- export { default as CrColorUtil } from "./color";
7
+ export { default as CrUtil } from "./util";
package/dist/esm/obj.js CHANGED
@@ -12,12 +12,12 @@ var CrObjUtil = /*#__PURE__*/function () {
12
12
  _createClass(CrObjUtil, null, [{
13
13
  key: "getValueByPath",
14
14
  value:
15
- /**
16
- * 根据字段路径获取对象值
17
- * @param obj { 'a': [{ 'b': { 'c': 3 } }] }
18
- * @param path 'a[0].b.c' | ['a', '0', 'b', 'c']
19
- * @param defaultValue 找不到时返回的默认值
20
- * @returns
15
+ /**
16
+ * 根据字段路径获取对象值
17
+ * @param obj { 'a': [{ 'b': { 'c': 3 } }] }
18
+ * @param path 'a[0].b.c' | ['a', '0', 'b', 'c']
19
+ * @param defaultValue 找不到时返回的默认值
20
+ * @returns
21
21
  */
22
22
  function getValueByPath(obj, propertyPath, defaultValue) {
23
23
  return get(obj, propertyPath, defaultValue);
@@ -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/esm/tree.js CHANGED
@@ -17,6 +17,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
17
17
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
18
18
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
19
19
  import { cloneDeep as _cloneDeep, find as _find, get as _get, keyBy as _keyBy } from 'lodash';
20
+ import CrUtil from "./util";
20
21
  var CrTreeUtil = /*#__PURE__*/function () {
21
22
  function CrTreeUtil() {
22
23
  _classCallCheck(this, CrTreeUtil);
@@ -116,31 +117,59 @@ var CrTreeUtil = /*#__PURE__*/function () {
116
117
  * 树形数据转列表数据
117
118
  * @param tree
118
119
  * @param options
120
+ * @param options.idField 默认值 id
121
+ * @param options.parentIdField 默认值 parentId
119
122
  * @param options.childrenField 默认值 children
120
- * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
123
+ * @param options.rootParentValue 根节点的父值,默认值 null
121
124
  * @returns
122
125
  */
123
126
  }, {
124
127
  key: "treeToList",
125
128
  value: function treeToList(tree, options) {
126
129
  var _ref2 = options || {},
130
+ _ref2$idField = _ref2.idField,
131
+ idField = _ref2$idField === void 0 ? 'id' : _ref2$idField,
132
+ _ref2$parentIdField = _ref2.parentIdField,
133
+ parentIdField = _ref2$parentIdField === void 0 ? 'parentId' : _ref2$parentIdField,
127
134
  _ref2$childrenField = _ref2.childrenField,
128
135
  childrenField = _ref2$childrenField === void 0 ? 'children' : _ref2$childrenField,
129
- _ref2$isCloneDeep = _ref2.isCloneDeep,
130
- isCloneDeep = _ref2$isCloneDeep === void 0 ? true : _ref2$isCloneDeep;
136
+ _ref2$rootParentValue = _ref2.rootParentValue,
137
+ rootParentValue = _ref2$rootParentValue === void 0 ? null : _ref2$rootParentValue;
131
138
  var result = [];
132
- var queue = _toConsumableArray(tree);
133
- while (queue.length) {
134
- var node = queue.shift();
139
+ var originQueue = _cloneDeep(_toConsumableArray(tree));
140
+ var queue = originQueue.map(function (node) {
141
+ return {
142
+ node: node,
143
+ parentId: rootParentValue
144
+ };
145
+ });
146
+ var _loop = function _loop() {
147
+ var _ref3 = queue.shift(),
148
+ node = _ref3.node,
149
+ parentId = _ref3.parentId;
150
+ if (!node || !CrUtil.isObject(node)) {
151
+ return 1; // continue
152
+ }
135
153
  var children = node[childrenField];
154
+ var nodeId = node[idField];
136
155
  var _ = node[childrenField],
137
156
  rest = _objectWithoutProperties(node, [childrenField].map(_toPropertyKey));
138
- result.push(rest);
139
- if (children && children.length) {
140
- queue.push.apply(queue, _toConsumableArray(children));
157
+ var flatNode = _objectSpread(_objectSpread({}, rest), {}, _defineProperty({}, parentIdField, parentId));
158
+ result.push(flatNode);
159
+ if (children && CrUtil.isArray(children) && children.length > 0) {
160
+ var childQueue = children.map(function (childNode) {
161
+ return {
162
+ node: childNode,
163
+ parentId: nodeId
164
+ };
165
+ });
166
+ queue.push.apply(queue, _toConsumableArray(childQueue));
141
167
  }
168
+ };
169
+ while (queue.length) {
170
+ if (_loop()) continue;
142
171
  }
143
- return isCloneDeep ? _cloneDeep(result) : result;
172
+ return result;
144
173
  }
145
174
 
146
175
  /**
@@ -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
  }