@zcrkey/js-utils 0.0.3 → 0.0.5

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.
package/dist/storage.js CHANGED
@@ -11,10 +11,10 @@ var CrStorage = /*#__PURE__*/function () {
11
11
  _createClass(CrStorage, null, [{
12
12
  key: "setLocalItem",
13
13
  value:
14
- /**
15
- * 设置本地存储
16
- * @param key
17
- * @param data
14
+ /**
15
+ * 设置本地存储
16
+ * @param key
17
+ * @param data
18
18
  */
19
19
  function setLocalItem(key, data) {
20
20
  try {
@@ -24,10 +24,10 @@ var CrStorage = /*#__PURE__*/function () {
24
24
  }
25
25
  }
26
26
 
27
- /**
28
- * 获取本地存储
29
- * @param key
30
- * @returns
27
+ /**
28
+ * 获取本地存储
29
+ * @param key
30
+ * @returns
31
31
  */
32
32
  }, {
33
33
  key: "getLocalItem",
@@ -48,9 +48,9 @@ var CrStorage = /*#__PURE__*/function () {
48
48
  }
49
49
  }
50
50
 
51
- /**
52
- * 清除某个本地存储
53
- * @param key
51
+ /**
52
+ * 清除某个本地存储
53
+ * @param key
54
54
  */
55
55
  }, {
56
56
  key: "removeLocalItem",
@@ -58,8 +58,8 @@ var CrStorage = /*#__PURE__*/function () {
58
58
  localStorage.removeItem(key);
59
59
  }
60
60
 
61
- /**
62
- * 清除所有本地存储
61
+ /**
62
+ * 清除所有本地存储
63
63
  */
64
64
  }, {
65
65
  key: "clearLocal",
@@ -67,10 +67,10 @@ var CrStorage = /*#__PURE__*/function () {
67
67
  localStorage.clear();
68
68
  }
69
69
 
70
- /**
71
- * 设置会话存储
72
- * @param key
73
- * @param data
70
+ /**
71
+ * 设置会话存储
72
+ * @param key
73
+ * @param data
74
74
  */
75
75
  }, {
76
76
  key: "setSessionItem",
@@ -82,10 +82,10 @@ var CrStorage = /*#__PURE__*/function () {
82
82
  }
83
83
  }
84
84
 
85
- /**
86
- * 获取会话存储
87
- * @param key
88
- * @returns
85
+ /**
86
+ * 获取会话存储
87
+ * @param key
88
+ * @returns
89
89
  */
90
90
  }, {
91
91
  key: "getSessionItem",
@@ -106,9 +106,9 @@ var CrStorage = /*#__PURE__*/function () {
106
106
  }
107
107
  }
108
108
 
109
- /**
110
- * 清除某个会话存储
111
- * @param key
109
+ /**
110
+ * 清除某个会话存储
111
+ * @param key
112
112
  */
113
113
  }, {
114
114
  key: "removeSessionItem",
@@ -116,8 +116,8 @@ var CrStorage = /*#__PURE__*/function () {
116
116
  sessionStorage.removeItem(key);
117
117
  }
118
118
 
119
- /**
120
- * 清除所有会话存储
119
+ /**
120
+ * 清除所有会话存储
121
121
  */
122
122
  }, {
123
123
  key: "clearSession",
@@ -0,0 +1,48 @@
1
+ export default class CrTreeUtil {
2
+ /**
3
+ * 列表数据转树形数据
4
+ * @param list
5
+ * @param options
6
+ * @param options.idField 默认值 id
7
+ * @param options.parentIdField 默认值 parentId
8
+ * @param options.childrenField 默认值 children
9
+ * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
10
+ * @param options.getData 处理数据
11
+ * @returns
12
+ */
13
+ static listToTree<T extends Record<string, any>>(list: T[], options?: {
14
+ idField?: string;
15
+ parentIdField?: string;
16
+ childrenField?: string;
17
+ isCloneDeep?: boolean;
18
+ getData?: (item: T) => any;
19
+ }): any[];
20
+ /**
21
+ * 树形数据转列表数据
22
+ * @param tree
23
+ * @param options
24
+ * @param options.childrenField 默认值 children
25
+ * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
26
+ * @returns
27
+ */
28
+ static treeToList<T extends Record<string, any>>(tree: T[], options?: {
29
+ childrenField?: string;
30
+ isCloneDeep?: boolean;
31
+ }): T[];
32
+ /**
33
+ * 获取扁平化父数据(包含自身)
34
+ * @param listData
35
+ * @param value
36
+ * @param settings
37
+ * @param settings.valueField 默认值 value
38
+ * @param settings.idField 默认值 id
39
+ * @param settings.parentIdField 默认值 parentId
40
+ * @param settings.getData 过滤数据
41
+ */
42
+ static getFlatParentDatas<T = any, R extends Record<string, any> = any>(listData: R[], value: number | string, settings?: {
43
+ valueField?: string;
44
+ idField?: string;
45
+ parentIdField?: string;
46
+ getData?: (item: R) => T;
47
+ }): T[];
48
+ }
@@ -0,0 +1,185 @@
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
3
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
4
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
5
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
6
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
7
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
8
+ 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; }
9
+ 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; }
10
+ 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; }
11
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
12
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
14
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
+ 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); } }
16
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
17
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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
+ import { cloneDeep as _cloneDeep, find as _find, get as _get, keyBy as _keyBy } from 'lodash';
20
+ var CrTreeUtil = /*#__PURE__*/function () {
21
+ function CrTreeUtil() {
22
+ _classCallCheck(this, CrTreeUtil);
23
+ }
24
+ _createClass(CrTreeUtil, null, [{
25
+ key: "listToTree",
26
+ value:
27
+ /**
28
+ * 列表数据转树形数据
29
+ * @param list
30
+ * @param options
31
+ * @param options.idField 默认值 id
32
+ * @param options.parentIdField 默认值 parentId
33
+ * @param options.childrenField 默认值 children
34
+ * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
35
+ * @param options.getData 处理数据
36
+ * @returns
37
+ */
38
+ function listToTree(list, options) {
39
+ var _ref = options || {},
40
+ _ref$idField = _ref.idField,
41
+ idField = _ref$idField === void 0 ? 'id' : _ref$idField,
42
+ _ref$parentIdField = _ref.parentIdField,
43
+ parentIdField = _ref$parentIdField === void 0 ? 'parentId' : _ref$parentIdField,
44
+ _ref$childrenField = _ref.childrenField,
45
+ childrenField = _ref$childrenField === void 0 ? 'children' : _ref$childrenField,
46
+ _ref$isCloneDeep = _ref.isCloneDeep,
47
+ isCloneDeep = _ref$isCloneDeep === void 0 ? true : _ref$isCloneDeep,
48
+ _ref$getData = _ref.getData,
49
+ getData = _ref$getData === void 0 ? function (item) {
50
+ return item;
51
+ } : _ref$getData;
52
+ var listData = isCloneDeep ? _cloneDeep(list) : list;
53
+ var treeData = [];
54
+ var nodeMap = new Map();
55
+ var _iterator = _createForOfIteratorHelper(listData),
56
+ _step;
57
+ try {
58
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
59
+ var _item = _step.value;
60
+ var id = _item[idField];
61
+ var node = _objectSpread(_objectSpread({}, getData(_item)), {}, {
62
+ __origin_id: id,
63
+ __origin_pid: _item[parentIdField]
64
+ });
65
+ nodeMap.set(id, node);
66
+ }
67
+ } catch (err) {
68
+ _iterator.e(err);
69
+ } finally {
70
+ _iterator.f();
71
+ }
72
+ var _iterator2 = _createForOfIteratorHelper(nodeMap.values()),
73
+ _step2;
74
+ try {
75
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
76
+ var _node = _step2.value;
77
+ var parentId = _node.__origin_pid;
78
+ if (parentId === undefined || parentId === null || parentId === 0 || !nodeMap.has(parentId)) {
79
+ treeData.push(_node);
80
+ } else {
81
+ var parentNode = nodeMap.get(parentId);
82
+ if (!parentNode[childrenField]) {
83
+ parentNode[childrenField] = [];
84
+ }
85
+ parentNode[childrenField].push(_node);
86
+ }
87
+ }
88
+ } catch (err) {
89
+ _iterator2.e(err);
90
+ } finally {
91
+ _iterator2.f();
92
+ }
93
+ var clean = function clean(nodes) {
94
+ var _iterator3 = _createForOfIteratorHelper(nodes),
95
+ _step3;
96
+ try {
97
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
98
+ var node = _step3.value;
99
+ delete node.__origin_id;
100
+ delete node.__origin_pid;
101
+ if (node[childrenField]) {
102
+ clean(node[childrenField]);
103
+ }
104
+ }
105
+ } catch (err) {
106
+ _iterator3.e(err);
107
+ } finally {
108
+ _iterator3.f();
109
+ }
110
+ };
111
+ clean(treeData);
112
+ return treeData;
113
+ }
114
+
115
+ /**
116
+ * 树形数据转列表数据
117
+ * @param tree
118
+ * @param options
119
+ * @param options.childrenField 默认值 children
120
+ * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
121
+ * @returns
122
+ */
123
+ }, {
124
+ key: "treeToList",
125
+ value: function treeToList(tree, options) {
126
+ var _ref2 = options || {},
127
+ _ref2$childrenField = _ref2.childrenField,
128
+ childrenField = _ref2$childrenField === void 0 ? 'children' : _ref2$childrenField,
129
+ _ref2$isCloneDeep = _ref2.isCloneDeep,
130
+ isCloneDeep = _ref2$isCloneDeep === void 0 ? true : _ref2$isCloneDeep;
131
+ var result = [];
132
+ var queue = _toConsumableArray(tree);
133
+ while (queue.length) {
134
+ var node = queue.shift();
135
+ var children = node[childrenField];
136
+ var _ = node[childrenField],
137
+ rest = _objectWithoutProperties(node, [childrenField].map(_toPropertyKey));
138
+ result.push(rest);
139
+ if (children && children.length) {
140
+ queue.push.apply(queue, _toConsumableArray(children));
141
+ }
142
+ }
143
+ return isCloneDeep ? _cloneDeep(result) : result;
144
+ }
145
+
146
+ /**
147
+ * 获取扁平化父数据(包含自身)
148
+ * @param listData
149
+ * @param value
150
+ * @param settings
151
+ * @param settings.valueField 默认值 value
152
+ * @param settings.idField 默认值 id
153
+ * @param settings.parentIdField 默认值 parentId
154
+ * @param settings.getData 过滤数据
155
+ */
156
+ }, {
157
+ key: "getFlatParentDatas",
158
+ value: function getFlatParentDatas(listData, value, settings) {
159
+ if (!Array.isArray(listData) || listData.length === 0 || value == null) {
160
+ return [];
161
+ }
162
+ var options = Object.assign({
163
+ valueField: 'value',
164
+ idField: 'id',
165
+ parentIdField: 'parentId',
166
+ getData: function getData(item) {
167
+ return item;
168
+ }
169
+ }, settings);
170
+ var nodeMap = _keyBy(listData, options.valueField);
171
+ var result = [];
172
+ var visited = new Set();
173
+ var current = nodeMap[value];
174
+ while (current && !visited.has(_get(current, options.idField))) {
175
+ visited.add(_get(current, options.idField));
176
+ result.unshift(options.getData(current));
177
+ var parentId = _get(current, options.parentIdField);
178
+ current = _find(listData, [options.valueField, parentId]);
179
+ }
180
+ return result;
181
+ }
182
+ }]);
183
+ return CrTreeUtil;
184
+ }();
185
+ export { CrTreeUtil as default };
package/dist/util.d.ts CHANGED
@@ -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 Array<any> ? never : T;
13
+ static isObject<T = any>(value: T): value is T extends object ? (T extends any[] ? never : T) : never;
14
14
  /**
15
15
  * 判断是否为空对象
16
16
  * @param value
@@ -28,31 +28,37 @@ export default class CrUtil {
28
28
  * @param value
29
29
  * @returns boolean
30
30
  */
31
- static isDate(value: any): boolean;
31
+ static isDate(value: unknown): boolean;
32
32
  /**
33
33
  * 判断是否为字符串
34
34
  * @param value
35
35
  * @returns boolean
36
36
  */
37
- static isString(value: any): value is string;
37
+ static isString(value: unknown): value is string;
38
38
  /**
39
39
  * 判断是否为数字
40
40
  * @param value
41
41
  * @returns boolean
42
42
  */
43
- static isNumber(value: any): value is number;
43
+ static isNumber(value: unknown): value is number;
44
44
  /**
45
45
  * 判断是否为文件 File
46
46
  * @param value
47
47
  * @returns boolean
48
48
  */
49
- static isFile(value: any): value is File;
49
+ static isFile(value: unknown): value is File;
50
50
  /**
51
51
  * 判断是否为 Boolean
52
52
  * @param value
53
53
  * @returns boolean
54
54
  */
55
- static isBoolean(value: any): value is boolean;
55
+ static isBoolean(value: unknown): value is boolean;
56
+ /**
57
+ * 判断是否为 Function
58
+ * @param value
59
+ * @returns boolean
60
+ */
61
+ static isFunction(value: unknown): value is (...args: any[]) => any;
56
62
  /**
57
63
  * 去掉字符串前后所有空格
58
64
  * @param str
@@ -65,9 +71,16 @@ export default class CrUtil {
65
71
  * @returns number
66
72
  */
67
73
  static getArrayDimension(arr: any[]): number;
74
+ /**
75
+ * 深拷贝
76
+ * @param value
77
+ * @returns
78
+ */
79
+ static cloneDeep<T>(value: T): T;
68
80
  /**
69
81
  * 深拷贝
70
82
  * @param target
83
+ * @deprecated 即将移除,使用 {@link CrUtil.cloneDeep} 替代
71
84
  * @returns
72
85
  */
73
86
  static deepCopy<T = any>(target: T): T;
@@ -75,6 +88,10 @@ export default class CrUtil {
75
88
  * 列表数据转树型数据
76
89
  * @param listData
77
90
  * @param settings
91
+ * @param settings.idField 默认值 id
92
+ * @param settings.pidField 默认值 parentId
93
+ * @param settings.childrenField 默认值 children
94
+ * @deprecated 即将移除,使用 {@link CrObjUtil.listToTree} 替代
78
95
  * @returns
79
96
  */
80
97
  static listToTreeData(listData: any[], settings?: {
@@ -88,6 +105,10 @@ export default class CrUtil {
88
105
  * 树型数据转列表数据
89
106
  * @param treeData
90
107
  * @param settings
108
+ * @param settings.idField 默认值 id
109
+ * @param settings.pidField 默认值 parentId
110
+ * @param settings.childrenField 默认值 children
111
+ * @deprecated 即将移除,使用 {@link CrObjUtil.treeToList} 替代
91
112
  * @returns
92
113
  */
93
114
  static treeDataToListData(treeData: any[], pid?: string | number, settings?: {
@@ -101,6 +122,10 @@ export default class CrUtil {
101
122
  * @param listData
102
123
  * @param value
103
124
  * @param settings
125
+ * @param settings.valueField 默认值 value
126
+ * @param settings.idField 默认值 id
127
+ * @param settings.pidField 默认值 parentId
128
+ * @deprecated 即将移除,使用 {@link CrObjUtil.getFlatParentDatas} 替代
104
129
  * @returns
105
130
  */
106
131
  static getParentNodes<T, R extends Record<string, any>>(listData: R[], value: number | string, settings?: {
@@ -126,6 +151,19 @@ export default class CrUtil {
126
151
  static paramsParse(str: string, settings?: {
127
152
  ignoreQueryPrefix: boolean;
128
153
  }): {};
154
+ /**
155
+ * 获取 URL 参数
156
+ * @param {*} url
157
+ * @returns
158
+ * @example
159
+ * "https://example.com?foo=bar&baz=qux" => {"query":{"foo":"bar","baz":"qux"},"hash":{},"all":{"foo":"bar","baz":"qux"}}
160
+ * "https://example.com/page?foo=1#/?a=1&b=2" => {"query":{"foo":"1"},"hash":{"/":"","a":"1","b":"2"},"all":{"foo":"1","/":"","a":"1","b":"2"}},
161
+ */
162
+ static getQueryParams(url: string): {
163
+ query: Record<string, any>;
164
+ hash: Record<string, any>;
165
+ all: Record<string, any>;
166
+ };
129
167
  /**
130
168
  * 版本号比较
131
169
  * @param v1
@@ -162,4 +200,10 @@ export default class CrUtil {
162
200
  * @returns
163
201
  */
164
202
  static compareDataDiff(data1: any, data2: any): Record<string, "added" | "removed" | "modified">;
203
+ /**
204
+ * 格式化为千分位
205
+ * @param value
206
+ * @returns 12345.6789 => 12,345.6789
207
+ */
208
+ static fmtThousands(value: number | string | undefined): string;
165
209
  }