sculp-js 1.2.0 → 1.2.1
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/README.md +6 -1
- package/lib/cjs/array.js +1 -1
- package/lib/cjs/async.js +1 -1
- package/lib/cjs/clipboard.js +1 -1
- package/lib/cjs/cookie.js +1 -1
- package/lib/cjs/date.js +1 -1
- package/lib/cjs/dom.js +1 -1
- package/lib/cjs/download.js +1 -1
- package/lib/cjs/easing.js +1 -1
- package/lib/cjs/file.js +1 -1
- package/lib/cjs/func.js +1 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/number.js +1 -1
- package/lib/cjs/object.js +1 -1
- package/lib/cjs/path.js +1 -1
- package/lib/cjs/qs.js +1 -1
- package/lib/cjs/random.js +1 -1
- package/lib/cjs/string.js +1 -1
- package/lib/cjs/tooltip.js +1 -1
- package/lib/cjs/tree.js +73 -2
- package/lib/cjs/type.js +1 -1
- package/lib/cjs/unique.js +1 -1
- package/lib/cjs/url.js +1 -1
- package/lib/cjs/watermark.js +1 -1
- package/lib/es/array.js +1 -1
- package/lib/es/async.js +1 -1
- package/lib/es/clipboard.js +1 -1
- package/lib/es/cookie.js +1 -1
- package/lib/es/date.js +1 -1
- package/lib/es/dom.js +1 -1
- package/lib/es/download.js +1 -1
- package/lib/es/easing.js +1 -1
- package/lib/es/file.js +1 -1
- package/lib/es/func.js +1 -1
- package/lib/es/index.js +2 -2
- package/lib/es/number.js +1 -1
- package/lib/es/object.js +1 -1
- package/lib/es/path.js +1 -1
- package/lib/es/qs.js +1 -1
- package/lib/es/random.js +1 -1
- package/lib/es/string.js +1 -1
- package/lib/es/tooltip.js +1 -1
- package/lib/es/tree.js +73 -3
- package/lib/es/type.js +1 -1
- package/lib/es/unique.js +1 -1
- package/lib/es/url.js +1 -1
- package/lib/es/watermark.js +1 -1
- package/lib/index.d.ts +11 -2
- package/lib/umd/index.js +73 -2
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -28,9 +28,14 @@
|
|
|
28
28
|
|
|
29
29
|
- arrayLike 判断类数组
|
|
30
30
|
- arrayEachAsync 异步遍历数组,可中断,支持倒序
|
|
31
|
-
|
|
31
|
+
|
|
32
|
+
- Tree
|
|
33
|
+
|
|
34
|
+
- forEachDeep 高性能的深度优先遍历函数, 支持continue、break,可定制id、children
|
|
35
|
+
- forEachMap 高性能的深度优先遍历的Map函数, 支持continue、break,可定制id、children
|
|
32
36
|
- searchTreeById 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
33
37
|
- buildTree 根据 id 与 parentId 从对象数组中构建对应的树
|
|
38
|
+
- formatTree 高性能的数组转树函数
|
|
34
39
|
|
|
35
40
|
- Object
|
|
36
41
|
|
package/lib/cjs/array.js
CHANGED
package/lib/cjs/async.js
CHANGED
package/lib/cjs/clipboard.js
CHANGED
package/lib/cjs/cookie.js
CHANGED
package/lib/cjs/date.js
CHANGED
package/lib/cjs/dom.js
CHANGED
package/lib/cjs/download.js
CHANGED
package/lib/cjs/easing.js
CHANGED
package/lib/cjs/file.js
CHANGED
package/lib/cjs/func.js
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.1
|
|
2
|
+
* sculp-js v1.2.1
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -128,5 +128,6 @@ exports.uniqueString = unique.uniqueString;
|
|
|
128
128
|
exports.tooltipEvent = tooltip.tooltipEvent;
|
|
129
129
|
exports.buildTree = tree.buildTree;
|
|
130
130
|
exports.forEachDeep = tree.forEachDeep;
|
|
131
|
+
exports.forEachMap = tree.forEachMap;
|
|
131
132
|
exports.formatTree = tree.formatTree;
|
|
132
133
|
exports.searchTreeById = tree.searchTreeById;
|
package/lib/cjs/number.js
CHANGED
package/lib/cjs/object.js
CHANGED
package/lib/cjs/path.js
CHANGED
package/lib/cjs/qs.js
CHANGED
package/lib/cjs/random.js
CHANGED
package/lib/cjs/string.js
CHANGED
package/lib/cjs/tooltip.js
CHANGED
package/lib/cjs/tree.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.1
|
|
2
|
+
* sculp-js v1.2.1
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
12
12
|
* @param {ArrayLike<V>} tree 树形数据
|
|
13
13
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
14
14
|
* @param {string} children 定制子元素的key
|
|
@@ -63,6 +63,76 @@ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
|
63
63
|
};
|
|
64
64
|
walk(tree, null);
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
68
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
69
|
+
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
70
|
+
* @param {string} children 定制子元素的key
|
|
71
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
72
|
+
* @returns {any[]} 新的一棵树
|
|
73
|
+
*/
|
|
74
|
+
function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
75
|
+
let level = 0, isBreak = false;
|
|
76
|
+
const newTree = [];
|
|
77
|
+
const walk = (arr, parent, newTree) => {
|
|
78
|
+
if (isReverse) {
|
|
79
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
80
|
+
if (isBreak) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
84
|
+
if (re === false) {
|
|
85
|
+
isBreak = true;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
else if (re === true) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
newTree.push(re);
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
94
|
+
++level;
|
|
95
|
+
newTree[newTree.length - 1][children] = [];
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
walk(arr[i][children], arr[i], newTree[newTree.length - 1][children]);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// children非有效数组时,移除该属性字段
|
|
101
|
+
delete re[children];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
for (let i = 0; i < arr.length; i++) {
|
|
107
|
+
if (isBreak) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
111
|
+
if (re === false) {
|
|
112
|
+
isBreak = true;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
else if (re === true) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
newTree.push(re);
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
121
|
+
++level;
|
|
122
|
+
newTree[newTree.length - 1][children] = [];
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
walk(arr[i][children], arr[i], newTree[newTree.length - 1][children]);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
// children非有效数组时,移除该属性字段
|
|
128
|
+
delete re[children];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
walk(tree, null, newTree);
|
|
134
|
+
return newTree;
|
|
135
|
+
}
|
|
66
136
|
/**
|
|
67
137
|
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
68
138
|
*
|
|
@@ -208,5 +278,6 @@ function formatTree(list, options = defaultFieldOptions) {
|
|
|
208
278
|
|
|
209
279
|
exports.buildTree = buildTree;
|
|
210
280
|
exports.forEachDeep = forEachDeep;
|
|
281
|
+
exports.forEachMap = forEachMap;
|
|
211
282
|
exports.formatTree = formatTree;
|
|
212
283
|
exports.searchTreeById = searchTreeById;
|
package/lib/cjs/type.js
CHANGED
package/lib/cjs/unique.js
CHANGED
package/lib/cjs/url.js
CHANGED
package/lib/cjs/watermark.js
CHANGED
package/lib/es/array.js
CHANGED
package/lib/es/async.js
CHANGED
package/lib/es/clipboard.js
CHANGED
package/lib/es/cookie.js
CHANGED
package/lib/es/date.js
CHANGED
package/lib/es/dom.js
CHANGED
package/lib/es/download.js
CHANGED
package/lib/es/easing.js
CHANGED
package/lib/es/file.js
CHANGED
package/lib/es/func.js
CHANGED
package/lib/es/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.1
|
|
2
|
+
* sculp-js v1.2.1
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -24,4 +24,4 @@ export { STRING_POOL, randomNumber, randomString, randomUuid } from './random.js
|
|
|
24
24
|
export { HEX_POOL, formatNumber, numberAbbr, numberToHex } from './number.js';
|
|
25
25
|
export { UNIQUE_NUMBER_SAFE_LENGTH, uniqueNumber, uniqueString } from './unique.js';
|
|
26
26
|
export { tooltipEvent } from './tooltip.js';
|
|
27
|
-
export { buildTree, forEachDeep, formatTree, searchTreeById } from './tree.js';
|
|
27
|
+
export { buildTree, forEachDeep, forEachMap, formatTree, searchTreeById } from './tree.js';
|
package/lib/es/number.js
CHANGED
package/lib/es/object.js
CHANGED
package/lib/es/path.js
CHANGED
package/lib/es/qs.js
CHANGED
package/lib/es/random.js
CHANGED
package/lib/es/string.js
CHANGED
package/lib/es/tooltip.js
CHANGED
package/lib/es/tree.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.1
|
|
2
|
+
* sculp-js v1.2.1
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
10
10
|
* @param {ArrayLike<V>} tree 树形数据
|
|
11
11
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
12
12
|
* @param {string} children 定制子元素的key
|
|
@@ -61,6 +61,76 @@ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
|
61
61
|
};
|
|
62
62
|
walk(tree, null);
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
66
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
67
|
+
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
68
|
+
* @param {string} children 定制子元素的key
|
|
69
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
70
|
+
* @returns {any[]} 新的一棵树
|
|
71
|
+
*/
|
|
72
|
+
function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
73
|
+
let level = 0, isBreak = false;
|
|
74
|
+
const newTree = [];
|
|
75
|
+
const walk = (arr, parent, newTree) => {
|
|
76
|
+
if (isReverse) {
|
|
77
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
78
|
+
if (isBreak) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
82
|
+
if (re === false) {
|
|
83
|
+
isBreak = true;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
else if (re === true) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
newTree.push(re);
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
92
|
+
++level;
|
|
93
|
+
newTree[newTree.length - 1][children] = [];
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
walk(arr[i][children], arr[i], newTree[newTree.length - 1][children]);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// children非有效数组时,移除该属性字段
|
|
99
|
+
delete re[children];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
for (let i = 0; i < arr.length; i++) {
|
|
105
|
+
if (isBreak) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
109
|
+
if (re === false) {
|
|
110
|
+
isBreak = true;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
else if (re === true) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
newTree.push(re);
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
119
|
+
++level;
|
|
120
|
+
newTree[newTree.length - 1][children] = [];
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
walk(arr[i][children], arr[i], newTree[newTree.length - 1][children]);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// children非有效数组时,移除该属性字段
|
|
126
|
+
delete re[children];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
walk(tree, null, newTree);
|
|
132
|
+
return newTree;
|
|
133
|
+
}
|
|
64
134
|
/**
|
|
65
135
|
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
66
136
|
*
|
|
@@ -204,4 +274,4 @@ function formatTree(list, options = defaultFieldOptions) {
|
|
|
204
274
|
return treeArr;
|
|
205
275
|
}
|
|
206
276
|
|
|
207
|
-
export { buildTree, forEachDeep, formatTree, searchTreeById };
|
|
277
|
+
export { buildTree, forEachDeep, forEachMap, formatTree, searchTreeById };
|
package/lib/es/type.js
CHANGED
package/lib/es/unique.js
CHANGED
package/lib/es/url.js
CHANGED
package/lib/es/watermark.js
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -689,7 +689,7 @@ interface IFieldOptions {
|
|
|
689
689
|
pidField: string;
|
|
690
690
|
}
|
|
691
691
|
/**
|
|
692
|
-
*
|
|
692
|
+
* 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
693
693
|
* @param {ArrayLike<V>} tree 树形数据
|
|
694
694
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
695
695
|
* @param {string} children 定制子元素的key
|
|
@@ -697,6 +697,15 @@ interface IFieldOptions {
|
|
|
697
697
|
* @returns {*}
|
|
698
698
|
*/
|
|
699
699
|
declare function forEachDeep<V>(tree: ArrayLike<V>, iterator: (val: V, i: number, currentArr: ArrayLike<V>, tree: ArrayLike<V>, parent: V | null, level: number) => boolean | void, children?: string, isReverse?: boolean): void;
|
|
700
|
+
/**
|
|
701
|
+
* 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
702
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
703
|
+
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
704
|
+
* @param {string} children 定制子元素的key
|
|
705
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
706
|
+
* @returns {any[]} 新的一棵树
|
|
707
|
+
*/
|
|
708
|
+
declare function forEachMap<V>(tree: ArrayLike<V>, iterator: (val: V, i: number, currentArr: ArrayLike<V>, tree: ArrayLike<V>, parent: V | null, level: number) => boolean | any, children?: string, isReverse?: boolean): any[];
|
|
700
709
|
type IdLike = number | string;
|
|
701
710
|
interface ITreeConf {
|
|
702
711
|
id: string | number;
|
|
@@ -758,4 +767,4 @@ declare function buildTree<ID extends string, PID extends string, T extends {
|
|
|
758
767
|
*/
|
|
759
768
|
declare function formatTree(list: any[], options?: IFieldOptions): any[];
|
|
760
769
|
|
|
761
|
-
export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type DateObj, type DateValue, type DebounceFunc, type FileType, HEX_POOL, type ICanvasWM, type IFieldOptions, type ITreeConf, type IdLike, type LooseParamValue, type LooseParams, type ObjectAssignItem, type OnceFunc, type Params, type PartialDeep, type RandomString, type ReadyCallback, type Replacer, STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_POOL, STRING_UPPERCASE_ALPHA, type SetStyle, type SmoothScrollOptions, type Style, type ThrottleFunc, UNIQUE_NUMBER_SAFE_LENGTH, type UniqueString, type Url, type WithChildren, addClass, arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, asyncMap, buildTree, calculateDate, calculateDateTime, chooseLocalFile, cloneDeep, cookieDel, cookieGet, cookieSet, copyText, dateParse, dateToEnd, dateToStart, debounce, downloadBlob, downloadData, downloadHref, downloadURL, forEachDeep, formatDate, formatNumber, formatTree, genCanvasWM, getComputedCssVal, getGlobal, getStrWidthPx, getStyle, hasClass, isArray, isBigInt, isBoolean, isDate, isDomReady, isError, isFunction, isNaN, isNull, isNumber, isObject, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, isValidDate, numberAbbr, numberToHex, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectHas, objectMap, objectAssign as objectMerge, objectOmit, objectPick, onDomReady, once, pathJoin, pathNormalize, qsParse, qsStringify, randomNumber, randomString, randomUuid, removeClass, searchTreeById, setGlobal, setStyle, smoothScroll, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, urlDelParams, urlParse, urlSetParams, urlStringify, wait };
|
|
770
|
+
export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type DateObj, type DateValue, type DebounceFunc, type FileType, HEX_POOL, type ICanvasWM, type IFieldOptions, type ITreeConf, type IdLike, type LooseParamValue, type LooseParams, type ObjectAssignItem, type OnceFunc, type Params, type PartialDeep, type RandomString, type ReadyCallback, type Replacer, STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_POOL, STRING_UPPERCASE_ALPHA, type SetStyle, type SmoothScrollOptions, type Style, type ThrottleFunc, UNIQUE_NUMBER_SAFE_LENGTH, type UniqueString, type Url, type WithChildren, addClass, arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, asyncMap, buildTree, calculateDate, calculateDateTime, chooseLocalFile, cloneDeep, cookieDel, cookieGet, cookieSet, copyText, dateParse, dateToEnd, dateToStart, debounce, downloadBlob, downloadData, downloadHref, downloadURL, forEachDeep, forEachMap, formatDate, formatNumber, formatTree, genCanvasWM, getComputedCssVal, getGlobal, getStrWidthPx, getStyle, hasClass, isArray, isBigInt, isBoolean, isDate, isDomReady, isError, isFunction, isNaN, isNull, isNumber, isObject, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, isValidDate, numberAbbr, numberToHex, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectHas, objectMap, objectAssign as objectMerge, objectOmit, objectPick, onDomReady, once, pathJoin, pathNormalize, qsParse, qsStringify, randomNumber, randomString, randomUuid, removeClass, searchTreeById, setGlobal, setStyle, smoothScroll, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, urlDelParams, urlParse, urlSetParams, urlStringify, wait };
|
package/lib/umd/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.1
|
|
2
|
+
* sculp-js v1.2.1
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -1825,7 +1825,7 @@
|
|
|
1825
1825
|
|
|
1826
1826
|
const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
|
|
1827
1827
|
/**
|
|
1828
|
-
*
|
|
1828
|
+
* 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
1829
1829
|
* @param {ArrayLike<V>} tree 树形数据
|
|
1830
1830
|
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
1831
1831
|
* @param {string} children 定制子元素的key
|
|
@@ -1880,6 +1880,76 @@
|
|
|
1880
1880
|
};
|
|
1881
1881
|
walk(tree, null);
|
|
1882
1882
|
}
|
|
1883
|
+
/**
|
|
1884
|
+
* 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
|
|
1885
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
1886
|
+
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
|
|
1887
|
+
* @param {string} children 定制子元素的key
|
|
1888
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
1889
|
+
* @returns {any[]} 新的一棵树
|
|
1890
|
+
*/
|
|
1891
|
+
function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
1892
|
+
let level = 0, isBreak = false;
|
|
1893
|
+
const newTree = [];
|
|
1894
|
+
const walk = (arr, parent, newTree) => {
|
|
1895
|
+
if (isReverse) {
|
|
1896
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
1897
|
+
if (isBreak) {
|
|
1898
|
+
break;
|
|
1899
|
+
}
|
|
1900
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
1901
|
+
if (re === false) {
|
|
1902
|
+
isBreak = true;
|
|
1903
|
+
break;
|
|
1904
|
+
}
|
|
1905
|
+
else if (re === true) {
|
|
1906
|
+
continue;
|
|
1907
|
+
}
|
|
1908
|
+
newTree.push(re);
|
|
1909
|
+
// @ts-ignore
|
|
1910
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
1911
|
+
++level;
|
|
1912
|
+
newTree[newTree.length - 1][children] = [];
|
|
1913
|
+
// @ts-ignore
|
|
1914
|
+
walk(arr[i][children], arr[i], newTree[newTree.length - 1][children]);
|
|
1915
|
+
}
|
|
1916
|
+
else {
|
|
1917
|
+
// children非有效数组时,移除该属性字段
|
|
1918
|
+
delete re[children];
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
else {
|
|
1923
|
+
for (let i = 0; i < arr.length; i++) {
|
|
1924
|
+
if (isBreak) {
|
|
1925
|
+
break;
|
|
1926
|
+
}
|
|
1927
|
+
const re = iterator(arr[i], i, arr, tree, parent, level);
|
|
1928
|
+
if (re === false) {
|
|
1929
|
+
isBreak = true;
|
|
1930
|
+
break;
|
|
1931
|
+
}
|
|
1932
|
+
else if (re === true) {
|
|
1933
|
+
continue;
|
|
1934
|
+
}
|
|
1935
|
+
newTree.push(re);
|
|
1936
|
+
// @ts-ignore
|
|
1937
|
+
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
1938
|
+
++level;
|
|
1939
|
+
newTree[newTree.length - 1][children] = [];
|
|
1940
|
+
// @ts-ignore
|
|
1941
|
+
walk(arr[i][children], arr[i], newTree[newTree.length - 1][children]);
|
|
1942
|
+
}
|
|
1943
|
+
else {
|
|
1944
|
+
// children非有效数组时,移除该属性字段
|
|
1945
|
+
delete re[children];
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
walk(tree, null, newTree);
|
|
1951
|
+
return newTree;
|
|
1952
|
+
}
|
|
1883
1953
|
/**
|
|
1884
1954
|
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
1885
1955
|
*
|
|
@@ -2054,6 +2124,7 @@
|
|
|
2054
2124
|
exports.downloadHref = downloadHref;
|
|
2055
2125
|
exports.downloadURL = downloadURL;
|
|
2056
2126
|
exports.forEachDeep = forEachDeep;
|
|
2127
|
+
exports.forEachMap = forEachMap;
|
|
2057
2128
|
exports.formatDate = formatDate;
|
|
2058
2129
|
exports.formatNumber = formatNumber;
|
|
2059
2130
|
exports.formatTree = formatTree;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sculp-js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"packageManager": "npm@8.19.2",
|
|
5
5
|
"description": "js工具库",
|
|
6
6
|
"scripts": {
|
|
@@ -37,8 +37,9 @@
|
|
|
37
37
|
"lib"
|
|
38
38
|
],
|
|
39
39
|
"keywords": [
|
|
40
|
-
"
|
|
41
|
-
"js-utils"
|
|
40
|
+
"sculp-js",
|
|
41
|
+
"js-utils",
|
|
42
|
+
"typescript"
|
|
42
43
|
],
|
|
43
44
|
"engines": {
|
|
44
45
|
"node": ">=16"
|