sculp-js 1.0.1 → 1.2.0

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.
Files changed (50) hide show
  1. package/README.md +105 -9
  2. package/lib/cjs/array.js +1 -92
  3. package/lib/cjs/async.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cookie.js +1 -1
  6. package/lib/cjs/date.js +1 -1
  7. package/lib/cjs/dom.js +1 -1
  8. package/lib/cjs/download.js +1 -1
  9. package/lib/cjs/easing.js +1 -1
  10. package/lib/cjs/file.js +1 -1
  11. package/lib/cjs/func.js +1 -1
  12. package/lib/cjs/index.js +6 -3
  13. package/lib/cjs/number.js +1 -1
  14. package/lib/cjs/object.js +1 -1
  15. package/lib/cjs/path.js +1 -1
  16. package/lib/cjs/qs.js +1 -1
  17. package/lib/cjs/random.js +1 -1
  18. package/lib/cjs/string.js +1 -1
  19. package/lib/cjs/tooltip.js +1 -1
  20. package/lib/cjs/tree.js +212 -0
  21. package/lib/cjs/type.js +1 -1
  22. package/lib/cjs/unique.js +1 -1
  23. package/lib/cjs/url.js +1 -1
  24. package/lib/cjs/watermark.js +1 -1
  25. package/lib/es/array.js +2 -91
  26. package/lib/es/async.js +1 -1
  27. package/lib/es/clipboard.js +1 -1
  28. package/lib/es/cookie.js +1 -1
  29. package/lib/es/date.js +1 -1
  30. package/lib/es/dom.js +1 -1
  31. package/lib/es/download.js +1 -1
  32. package/lib/es/easing.js +1 -1
  33. package/lib/es/file.js +1 -1
  34. package/lib/es/func.js +1 -1
  35. package/lib/es/index.js +3 -2
  36. package/lib/es/number.js +1 -1
  37. package/lib/es/object.js +1 -1
  38. package/lib/es/path.js +1 -1
  39. package/lib/es/qs.js +1 -1
  40. package/lib/es/random.js +1 -1
  41. package/lib/es/string.js +1 -1
  42. package/lib/es/tooltip.js +1 -1
  43. package/lib/es/tree.js +207 -0
  44. package/lib/es/type.js +1 -1
  45. package/lib/es/unique.js +1 -1
  46. package/lib/es/url.js +1 -1
  47. package/lib/es/watermark.js +1 -1
  48. package/lib/index.d.ts +77 -25
  49. package/lib/umd/index.js +203 -90
  50. package/package.json +10 -6
package/README.md CHANGED
@@ -1,24 +1,120 @@
1
1
  [![Node.js CI](https://github.com/chandq/sculp-js/actions/workflows/node.js.yml/badge.svg)](https://github.com/chandq/sculp-js/actions/workflows/node.js.yml)
2
2
  [![sculp-js](https://img.shields.io/github/package-json/v/chandq/sculp-js?style=flat-square)](https://github.com/chandq/sculp-js)
3
- [![node](https://img.shields.io/badge/node-v12.0.0-blue)](https://nodejs.org/download/release/v12.0.0/)
4
3
  [![node](https://img.shields.io/badge/language-typescript-orange.svg)](https://nodejs.org/download/release/v12.0.0/)
5
4
  [![license:MIT](https://img.shields.io/npm/l/vue.svg?sanitize=true)](https://github.com/chandq/sculp-js/blob/main/LICENSE.md)
6
5
  [![Downloads:?](https://img.shields.io/npm/dm/sculp-js.svg?sanitize=true)](https://npmcharts.com/compare/sculp-js?minimal=true)
6
+ [![codecov](https://codecov.io/gh/chandq/sculp-js/graph/badge.svg?token=VZ6TERPGI9)](https://codecov.io/gh/chandq/sculp-js)
7
7
 
8
8
  # sculp-js
9
9
 
10
- > TS + Rollup, 原生实现,不依赖任何第三方库,输出 esm、cjs、umd三种模块方式的产物
10
+ [API文档](https://chandq.github.io/sculp-js/)
11
11
 
12
- js 工具函数库, 包含类型判断模块:type, 数据处理模块:`array`、`object`、`string`、`number`,功能性模块:下载`download`、复制`clipboard`、`cookie`、日期`date`、qs、水印`watermark`, 文件处理模块:`file`,自定义悬浮提示模块: `tooltip`, dom处理模块:`dom`;
12
+ > TS + Rollup, native implementation, without relying on any third-party libraries, outputs products of three module modes: ESM, CJS, and UMD
13
+
14
+ ## Features
15
+
16
+ - Type
17
+
18
+ - isString
19
+ - isBoolean
20
+ - isNumber
21
+ - isUndefined
22
+ - isNull
23
+ - isPrimitive
24
+ - isObject
25
+ - typeIs
13
26
 
14
27
  - Array
15
28
 
16
29
  - arrayLike 判断类数组
17
- - arrayEach 可中断的数组遍历, 支持倒序
18
30
  - arrayEachAsync 异步遍历数组,可中断,支持倒序
19
- - arrayInsertBefore 改变数组元素位置
20
- - arrayRemove 数组删除指定元素
21
- - deepTraversal 深度优先遍历函数, 支持continue、break,可定制id、children
22
- - getTreeIds 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
31
+ - forEachDeep 深度优先遍历函数, 支持continue、break,可定制id、children
32
+ - searchTreeById 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
33
+ - buildTree 根据 id 与 parentId 从对象数组中构建对应的树
34
+
35
+ - Object
36
+
37
+ - isPlainObject
38
+ - objectHas
39
+ - objectAssign 合并
40
+ - objectEach
41
+ - objectEachAsync
42
+ - objectGet
43
+
44
+ - Number
45
+
46
+ - formatNumber
47
+
48
+ - String
49
+
50
+ - stringCamelCase
51
+ - stringKebabCase
52
+ - getStrWidthPx
53
+
54
+ - Unique
55
+
56
+ - uniqueString
57
+ - uniqueNumber
58
+
59
+ - Date
60
+
61
+ - formatDate
62
+ - dateToStart
63
+ - dateToEnd
64
+ - calculateDate
65
+ - calculateDateTime
66
+
67
+ - Download
68
+
69
+ - downloadURL
70
+ - downloadHref
71
+ - downloadBlob
72
+ - downloadData
73
+
74
+ - File
75
+
76
+ - chooseLocalFile
77
+
78
+ - Dom
79
+
80
+ - hasClass
81
+ - addClass
82
+ - removeClass
83
+ - setStyle
84
+ - getStyle
85
+ - getComputedCssVal
86
+
87
+ - Watermark
88
+
89
+ - genCanvasWM
90
+
91
+ - Clipboard
92
+ - copyText
93
+
94
+ ## Install
95
+
96
+ ```js
97
+ npm i sculp-js
98
+ ```
99
+
100
+ ## Usage
101
+
102
+ ```js
103
+ import { forEachDeep } from 'sculp-js';
104
+
105
+ const tree = [
106
+ { id: 1, name: 'row1' },
107
+ {
108
+ id: 2,
109
+ name: 'row2',
110
+ children: [{ id: 21, name: 'row2-1' }]
111
+ },
112
+ { id: 3, name: 'row3' }
113
+ ];
23
114
 
24
- ## [API文档详情](https://chandq.github.io/sculp-js/)
115
+ const arr = [];
116
+ forEachDeep(tree, ({ id, name }) => {
117
+ arr.push(name);
118
+ });
119
+ // arr will be: ['row1', 'row2', 'row2-1', 'row3'];
120
+ ```
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -109,100 +109,9 @@ function arrayRemove(array, expect) {
109
109
  indexes.forEach((val, idx) => array.splice(val - idx, 1));
110
110
  return array;
111
111
  }
112
- /**
113
- * 自定义深度优先遍历函数(支持continue和break操作)
114
- * @param {ArrayLike<V>} tree 树形数据
115
- * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
116
- * @param {string} children 定制子元素的key
117
- * @param {boolean} isReverse 是否反向遍历
118
- * @returns {*}
119
- */
120
- function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
121
- let level = 0, isBreak = false;
122
- const walk = (arr, parent) => {
123
- if (isReverse) {
124
- for (let i = arr.length - 1; i >= 0; i--) {
125
- if (isBreak) {
126
- break;
127
- }
128
- const re = iterator(arr[i], i, tree, parent, level);
129
- if (re === false) {
130
- isBreak = true;
131
- break;
132
- }
133
- else if (re === true) {
134
- continue;
135
- }
136
- // @ts-ignore
137
- if (Array.isArray(arr[i][children])) {
138
- ++level;
139
- // @ts-ignore
140
- walk(arr[i][children], arr[i]);
141
- }
142
- }
143
- }
144
- else {
145
- for (let i = 0; i < arr.length; i++) {
146
- if (isBreak) {
147
- break;
148
- }
149
- const re = iterator(arr[i], i, tree, parent, level);
150
- if (re === false) {
151
- isBreak = true;
152
- break;
153
- }
154
- else if (re === true) {
155
- continue;
156
- }
157
- // @ts-ignore
158
- if (Array.isArray(arr[i][children])) {
159
- ++level;
160
- // @ts-ignore
161
- walk(arr[i][children], arr[i]);
162
- }
163
- }
164
- }
165
- };
166
- walk(tree, null);
167
- }
168
- /**
169
- * 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
170
- *
171
- * @param {ArrayLike<T>} tree - 树形数据
172
- * @param {IdLike} nodeId - 元素ID
173
- * @param {ITreeConf} config - 迭代配置项
174
- * @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
175
- */
176
- function searchTreeById(tree, nodeId, config) {
177
- const { children = 'children', id = 'id' } = config || {};
178
- const toFlatArray = (tree, parentId, parent) => {
179
- return tree.reduce((t, _) => {
180
- const child = _[children];
181
- return [
182
- ...t,
183
- parentId ? { ..._, parentId, parent } : _,
184
- ...(child && child.length ? toFlatArray(child, _[id], _) : [])
185
- ];
186
- }, []);
187
- };
188
- const getIds = (flatArray) => {
189
- let child = flatArray.find(_ => _[id] === nodeId);
190
- const { parent, parentId, ...other } = child;
191
- let ids = [nodeId], nodes = [other];
192
- while (child && child.parentId) {
193
- ids = [child.parentId, ...ids];
194
- nodes = [child.parent, ...nodes];
195
- child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
196
- }
197
- return [ids, nodes];
198
- };
199
- return getIds(toFlatArray(tree));
200
- }
201
112
 
202
113
  exports.arrayEach = arrayEach;
203
114
  exports.arrayEachAsync = arrayEachAsync;
204
115
  exports.arrayInsertBefore = arrayInsertBefore;
205
116
  exports.arrayLike = arrayLike;
206
117
  exports.arrayRemove = arrayRemove;
207
- exports.forEachDeep = forEachDeep;
208
- exports.searchTreeById = searchTreeById;
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -26,6 +26,7 @@ var random = require('./random.js');
26
26
  var number = require('./number.js');
27
27
  var unique = require('./unique.js');
28
28
  var tooltip = require('./tooltip.js');
29
+ var tree = require('./tree.js');
29
30
 
30
31
 
31
32
 
@@ -34,8 +35,6 @@ exports.arrayEachAsync = array.arrayEachAsync;
34
35
  exports.arrayInsertBefore = array.arrayInsertBefore;
35
36
  exports.arrayLike = array.arrayLike;
36
37
  exports.arrayRemove = array.arrayRemove;
37
- exports.forEachDeep = array.forEachDeep;
38
- exports.searchTreeById = array.searchTreeById;
39
38
  exports.copyText = clipboard.copyText;
40
39
  exports.cookieDel = cookie.cookieDel;
41
40
  exports.cookieGet = cookie.cookieGet;
@@ -127,3 +126,7 @@ exports.UNIQUE_NUMBER_SAFE_LENGTH = unique.UNIQUE_NUMBER_SAFE_LENGTH;
127
126
  exports.uniqueNumber = unique.uniqueNumber;
128
127
  exports.uniqueString = unique.uniqueString;
129
128
  exports.tooltipEvent = tooltip.tooltipEvent;
129
+ exports.buildTree = tree.buildTree;
130
+ exports.forEachDeep = tree.forEachDeep;
131
+ exports.formatTree = tree.formatTree;
132
+ exports.searchTreeById = tree.searchTreeById;
package/lib/cjs/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -0,0 +1,212 @@
1
+ /*!
2
+ * sculp-js v1.1.0
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
10
+ /**
11
+ * 自定义深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
12
+ * @param {ArrayLike<V>} tree 树形数据
13
+ * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
14
+ * @param {string} children 定制子元素的key
15
+ * @param {boolean} isReverse 是否反向遍历
16
+ * @returns {*}
17
+ */
18
+ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
19
+ let level = 0, isBreak = false;
20
+ const walk = (arr, parent) => {
21
+ if (isReverse) {
22
+ for (let i = arr.length - 1; i >= 0; i--) {
23
+ if (isBreak) {
24
+ break;
25
+ }
26
+ const re = iterator(arr[i], i, arr, tree, parent, level);
27
+ if (re === false) {
28
+ isBreak = true;
29
+ break;
30
+ }
31
+ else if (re === true) {
32
+ continue;
33
+ }
34
+ // @ts-ignore
35
+ if (arr[i] && Array.isArray(arr[i][children])) {
36
+ ++level;
37
+ // @ts-ignore
38
+ walk(arr[i][children], arr[i]);
39
+ }
40
+ }
41
+ }
42
+ else {
43
+ for (let i = 0; i < arr.length; i++) {
44
+ if (isBreak) {
45
+ break;
46
+ }
47
+ const re = iterator(arr[i], i, arr, tree, parent, level);
48
+ if (re === false) {
49
+ isBreak = true;
50
+ break;
51
+ }
52
+ else if (re === true) {
53
+ continue;
54
+ }
55
+ // @ts-ignore
56
+ if (arr[i] && Array.isArray(arr[i][children])) {
57
+ ++level;
58
+ // @ts-ignore
59
+ walk(arr[i][children], arr[i]);
60
+ }
61
+ }
62
+ }
63
+ };
64
+ walk(tree, null);
65
+ }
66
+ /**
67
+ * 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
68
+ *
69
+ * @param {ArrayLike<T>} tree - 树形数据
70
+ * @param {IdLike} nodeId - 元素ID
71
+ * @param {ITreeConf} config - 迭代配置项
72
+ * @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
73
+ */
74
+ function searchTreeById(tree, nodeId, config) {
75
+ const { children = 'children', id = 'id' } = config || {};
76
+ const toFlatArray = (tree, parentId, parent) => {
77
+ return tree.reduce((t, _) => {
78
+ const child = _[children];
79
+ return [
80
+ ...t,
81
+ parentId ? { ..._, parentId, parent } : _,
82
+ ...(child && child.length ? toFlatArray(child, _[id], _) : [])
83
+ ];
84
+ }, []);
85
+ };
86
+ const getIds = (flatArray) => {
87
+ let child = flatArray.find(_ => _[id] === nodeId);
88
+ const { parent, parentId, ...other } = child;
89
+ let ids = [nodeId], nodes = [other];
90
+ while (child && child.parentId) {
91
+ ids = [child.parentId, ...ids];
92
+ nodes = [child.parent, ...nodes];
93
+ child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
94
+ }
95
+ return [ids, nodes];
96
+ };
97
+ return getIds(toFlatArray(tree));
98
+ }
99
+ /**
100
+ * 使用迭代函数转换数组
101
+ * @param {T} array
102
+ * @param {Function} callback 迭代函数
103
+ * @returns {Array}
104
+ */
105
+ function flatMap(array, callback) {
106
+ const result = [];
107
+ array.forEach((value, index) => {
108
+ result.push(...callback(value, index, array));
109
+ });
110
+ return result;
111
+ }
112
+ /**
113
+ * 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
114
+ * 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
115
+ * 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
116
+ * @param {string} idProp 元素ID
117
+ * @param {string} parentIdProp 父元素ID
118
+ * @param {object[]} items 一维数组
119
+ * @returns {WithChildren<T>[]} 树
120
+ * @example
121
+ * const array = [
122
+ * { id: 'node-1', parent: 'root' },
123
+ * { id: 'node-2', parent: 'root' },
124
+ * { id: 'node-3', parent: 'node-2' },
125
+ * { id: 'node-4', parent: 'node-2' },
126
+ * { id: 'node-5', parent: 'node-4' },
127
+ * ]
128
+ * const tree = buildTree('id', 'parent', array)
129
+ * expect(tree).toEqual([
130
+ * { id: 'node-1', parent: 'root' },
131
+ * {
132
+ * id: 'node-2',
133
+ * parent: 'root',
134
+ * children: [
135
+ * { id: 'node-3', parent: 'node-2' },
136
+ * {
137
+ * id: 'node-4',
138
+ * parent: 'node-2',
139
+ * children: [{ id: 'node-5', parent: 'node-4' }],
140
+ * },
141
+ * ],
142
+ * },
143
+ * ])
144
+ */
145
+ function buildTree(idProp, parentIdProp, items) {
146
+ const wrapperMap = new Map();
147
+ const ensure = (id) => {
148
+ if (wrapperMap.has(id)) {
149
+ return wrapperMap.get(id);
150
+ }
151
+ //@ts-ignore
152
+ const wrapper = { id, parent: null, item: null, children: [] };
153
+ wrapperMap.set(id, wrapper);
154
+ return wrapper;
155
+ };
156
+ for (const item of items) {
157
+ const parentWrapper = ensure(item[parentIdProp]);
158
+ const itemWrapper = ensure(item[idProp]);
159
+ //@ts-ignore
160
+ itemWrapper.parent = parentWrapper;
161
+ //@ts-ignore
162
+ parentWrapper.children.push(itemWrapper);
163
+ //@ts-ignore
164
+ itemWrapper.item = item;
165
+ }
166
+ const topLevelWrappers = flatMap(Array.from(wrapperMap.values()).filter(wrapper => wrapper.parent === null), wrapper => wrapper.children);
167
+ return unwrapRecursively(topLevelWrappers);
168
+ function unwrapRecursively(wrapperArray) {
169
+ const result = [];
170
+ for (const wrapper of wrapperArray) {
171
+ if (wrapper.children.length === 0) {
172
+ result.push(wrapper.item);
173
+ }
174
+ else {
175
+ result.push({
176
+ ...wrapper.item,
177
+ children: unwrapRecursively(wrapper.children)
178
+ });
179
+ }
180
+ }
181
+ return result;
182
+ }
183
+ }
184
+ /**
185
+ * 扁平化数组转换成树(效率高于buildTree)
186
+ * @param {any[]} list
187
+ * @param {IFieldOptions} options
188
+ * @returns {any[]}
189
+ */
190
+ function formatTree(list, options = defaultFieldOptions) {
191
+ const { keyField, childField, pidField } = options;
192
+ const treeArr = [];
193
+ const sourceMap = {};
194
+ list.forEach(item => {
195
+ sourceMap[item[keyField]] = item;
196
+ });
197
+ list.forEach(item => {
198
+ const parent = sourceMap[item[pidField]];
199
+ if (parent) {
200
+ (parent[childField] || (parent[childField] = [])).push(item);
201
+ }
202
+ else {
203
+ treeArr.push(item);
204
+ }
205
+ });
206
+ return treeArr;
207
+ }
208
+
209
+ exports.buildTree = buildTree;
210
+ exports.forEachDeep = forEachDeep;
211
+ exports.formatTree = formatTree;
212
+ exports.searchTreeById = searchTreeById;
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.1.0
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */