sculp-js 1.3.2 → 1.4.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 (52) hide show
  1. package/README.md +1 -1
  2. package/lib/cjs/array.js +1 -1
  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 +3 -1
  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 +80 -1
  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/cjs/we-decode.js +1 -1
  26. package/lib/es/array.js +1 -1
  27. package/lib/es/async.js +1 -1
  28. package/lib/es/clipboard.js +1 -1
  29. package/lib/es/cookie.js +1 -1
  30. package/lib/es/date.js +1 -1
  31. package/lib/es/dom.js +1 -1
  32. package/lib/es/download.js +1 -1
  33. package/lib/es/easing.js +1 -1
  34. package/lib/es/file.js +1 -1
  35. package/lib/es/func.js +1 -1
  36. package/lib/es/index.js +2 -2
  37. package/lib/es/number.js +1 -1
  38. package/lib/es/object.js +1 -1
  39. package/lib/es/path.js +1 -1
  40. package/lib/es/qs.js +1 -1
  41. package/lib/es/random.js +1 -1
  42. package/lib/es/string.js +1 -1
  43. package/lib/es/tooltip.js +1 -1
  44. package/lib/es/tree.js +79 -2
  45. package/lib/es/type.js +1 -1
  46. package/lib/es/unique.js +1 -1
  47. package/lib/es/url.js +1 -1
  48. package/lib/es/watermark.js +1 -1
  49. package/lib/es/we-decode.js +1 -1
  50. package/lib/index.d.ts +21 -1
  51. package/lib/umd/index.js +81 -2
  52. package/package.json +1 -1
package/README.md CHANGED
@@ -34,8 +34,8 @@
34
34
  - forEachDeep 高性能的深度优先遍历函数, 支持continue、break,可定制id、children
35
35
  - forEachMap 高性能的深度优先遍历的Map函数, 支持continue、break,可定制id、children
36
36
  - searchTreeById 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
37
- - buildTree 根据 id 与 parentId 从对象数组中构建对应的树
38
37
  - formatTree 高性能的数组转树函数
38
+ - fuzzySearchTree 模糊搜索函数,返回包含搜索字符的节点及其祖先节点的树
39
39
 
40
40
  - Object
41
41
 
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -131,9 +131,11 @@ exports.uniqueNumber = unique.uniqueNumber;
131
131
  exports.uniqueString = unique.uniqueString;
132
132
  exports.tooltipEvent = tooltip.tooltipEvent;
133
133
  exports.buildTree = tree.buildTree;
134
+ exports.flatTree = tree.flatTree;
134
135
  exports.forEachDeep = tree.forEachDeep;
135
136
  exports.forEachMap = tree.forEachMap;
136
137
  exports.formatTree = tree.formatTree;
138
+ exports.fuzzySearchTree = tree.fuzzySearchTree;
137
139
  exports.searchTreeById = tree.searchTreeById;
138
140
  exports.weAppJwtDecode = weDecode.weAppJwtDecode;
139
141
  exports.weAtob = weDecode.weAtob;
package/lib/cjs/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,6 +7,11 @@
7
7
  'use strict';
8
8
 
9
9
  const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
10
+ const defaultSearchTreeOptions = {
11
+ childField: 'children',
12
+ nameField: 'name',
13
+ ignoreEmptyChild: false
14
+ };
10
15
  /**
11
16
  * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
12
17
  * @param {ArrayLike<V>} tree 树形数据
@@ -271,9 +276,83 @@ function formatTree(list, options = defaultFieldOptions) {
271
276
  });
272
277
  return treeArr;
273
278
  }
279
+ /**
280
+ * 树形结构转扁平化
281
+ * @param {any} treeList
282
+ * @param {IFieldOptions} options
283
+ * @return {*}
284
+ */
285
+ function flatTree(treeList, options = defaultFieldOptions) {
286
+ const { childField, keyField, pidField } = options;
287
+ return treeList.reduce((res, node) => {
288
+ const item = {
289
+ ...node,
290
+ [childField]: [] // 清空子级
291
+ };
292
+ item.hasOwnProperty([childField]) && delete item[childField];
293
+ res.push(item);
294
+ if (node[childField]) {
295
+ const children = node[childField].map(item => ({
296
+ ...item,
297
+ [pidField]: node[keyField] || item.pid // 给子级设置pid
298
+ }));
299
+ res = res.concat(flatTree(children, options));
300
+ }
301
+ return res;
302
+ }, []);
303
+ }
304
+ /**
305
+ * 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
306
+ * @param {any[]} nodes
307
+ * @param {string} query
308
+ * @param {ISearchTreeOpts} options
309
+ * @return {any[]}
310
+ */
311
+ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
312
+ const result = [];
313
+ for (const node of nodes) {
314
+ // 递归检查子节点是否匹配
315
+ const matchedChildren = node[options.childField] && node[options.childField].length > 0
316
+ ? fuzzySearchTree(node[options.childField] || [], query, options)
317
+ : [];
318
+ // 检查当前节点是否匹配或者有匹配的子节点
319
+ if (node[options.nameField].toLowerCase().includes(query.toLowerCase()) || matchedChildren.length > 0) {
320
+ // 将当前节点加入结果中
321
+ if (node[options.childField]) {
322
+ if (matchedChildren.length > 0) {
323
+ result.push({
324
+ ...node,
325
+ [options.childField]: matchedChildren // 包含匹配的子节点
326
+ });
327
+ }
328
+ else if (options.ignoreEmptyChild) {
329
+ node[options.childField] && delete node[options.childField];
330
+ result.push({
331
+ ...node
332
+ });
333
+ }
334
+ else {
335
+ result.push({
336
+ ...node,
337
+ ...{ [options.childField]: [] }
338
+ });
339
+ }
340
+ }
341
+ else {
342
+ node[options.childField] && delete node[options.childField];
343
+ result.push({
344
+ ...node
345
+ });
346
+ }
347
+ }
348
+ }
349
+ return result;
350
+ }
274
351
 
275
352
  exports.buildTree = buildTree;
353
+ exports.flatTree = flatTree;
276
354
  exports.forEachDeep = forEachDeep;
277
355
  exports.forEachMap = forEachMap;
278
356
  exports.formatTree = formatTree;
357
+ exports.fuzzySearchTree = fuzzySearchTree;
279
358
  exports.searchTreeById = searchTreeById;
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -24,5 +24,5 @@ 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, forEachMap, formatTree, searchTreeById } from './tree.js';
27
+ export { buildTree, flatTree, forEachDeep, forEachMap, formatTree, fuzzySearchTree, searchTreeById } from './tree.js';
28
28
  export { weAppJwtDecode, weAtob, weBtoa } from './we-decode.js';
package/lib/es/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tooltip.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tree.js CHANGED
@@ -1,10 +1,15 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
7
  const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
8
+ const defaultSearchTreeOptions = {
9
+ childField: 'children',
10
+ nameField: 'name',
11
+ ignoreEmptyChild: false
12
+ };
8
13
  /**
9
14
  * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
10
15
  * @param {ArrayLike<V>} tree 树形数据
@@ -269,5 +274,77 @@ function formatTree(list, options = defaultFieldOptions) {
269
274
  });
270
275
  return treeArr;
271
276
  }
277
+ /**
278
+ * 树形结构转扁平化
279
+ * @param {any} treeList
280
+ * @param {IFieldOptions} options
281
+ * @return {*}
282
+ */
283
+ function flatTree(treeList, options = defaultFieldOptions) {
284
+ const { childField, keyField, pidField } = options;
285
+ return treeList.reduce((res, node) => {
286
+ const item = {
287
+ ...node,
288
+ [childField]: [] // 清空子级
289
+ };
290
+ item.hasOwnProperty([childField]) && delete item[childField];
291
+ res.push(item);
292
+ if (node[childField]) {
293
+ const children = node[childField].map(item => ({
294
+ ...item,
295
+ [pidField]: node[keyField] || item.pid // 给子级设置pid
296
+ }));
297
+ res = res.concat(flatTree(children, options));
298
+ }
299
+ return res;
300
+ }, []);
301
+ }
302
+ /**
303
+ * 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
304
+ * @param {any[]} nodes
305
+ * @param {string} query
306
+ * @param {ISearchTreeOpts} options
307
+ * @return {any[]}
308
+ */
309
+ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
310
+ const result = [];
311
+ for (const node of nodes) {
312
+ // 递归检查子节点是否匹配
313
+ const matchedChildren = node[options.childField] && node[options.childField].length > 0
314
+ ? fuzzySearchTree(node[options.childField] || [], query, options)
315
+ : [];
316
+ // 检查当前节点是否匹配或者有匹配的子节点
317
+ if (node[options.nameField].toLowerCase().includes(query.toLowerCase()) || matchedChildren.length > 0) {
318
+ // 将当前节点加入结果中
319
+ if (node[options.childField]) {
320
+ if (matchedChildren.length > 0) {
321
+ result.push({
322
+ ...node,
323
+ [options.childField]: matchedChildren // 包含匹配的子节点
324
+ });
325
+ }
326
+ else if (options.ignoreEmptyChild) {
327
+ node[options.childField] && delete node[options.childField];
328
+ result.push({
329
+ ...node
330
+ });
331
+ }
332
+ else {
333
+ result.push({
334
+ ...node,
335
+ ...{ [options.childField]: [] }
336
+ });
337
+ }
338
+ }
339
+ else {
340
+ node[options.childField] && delete node[options.childField];
341
+ result.push({
342
+ ...node
343
+ });
344
+ }
345
+ }
346
+ }
347
+ return result;
348
+ }
272
349
 
273
- export { buildTree, forEachDeep, forEachMap, formatTree, searchTreeById };
350
+ export { buildTree, flatTree, forEachDeep, forEachMap, formatTree, fuzzySearchTree, searchTreeById };
package/lib/es/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/index.d.ts CHANGED
@@ -709,6 +709,11 @@ interface IFieldOptions {
709
709
  childField: string;
710
710
  pidField: string;
711
711
  }
712
+ interface ISearchTreeOpts {
713
+ childField: string;
714
+ nameField: string;
715
+ ignoreEmptyChild: boolean;
716
+ }
712
717
  /**
713
718
  * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
714
719
  * @param {ArrayLike<V>} tree 树形数据
@@ -787,6 +792,21 @@ declare function buildTree<ID extends string, PID extends string, T extends {
787
792
  * @returns {any[]}
788
793
  */
789
794
  declare function formatTree(list: any[], options?: IFieldOptions): any[];
795
+ /**
796
+ * 树形结构转扁平化
797
+ * @param {any} treeList
798
+ * @param {IFieldOptions} options
799
+ * @return {*}
800
+ */
801
+ declare function flatTree(treeList: any[], options?: IFieldOptions): any[];
802
+ /**
803
+ * 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
804
+ * @param {any[]} nodes
805
+ * @param {string} query
806
+ * @param {ISearchTreeOpts} options
807
+ * @return {any[]}
808
+ */
809
+ declare function fuzzySearchTree(nodes: any[], query: string, options?: ISearchTreeOpts): any[];
790
810
 
791
811
  /**
792
812
  * 字符串编码成Base64 (适用于任何环境,包括小程序)
@@ -802,4 +822,4 @@ declare function weBtoa(string: string): string;
802
822
  declare function weAtob(string: string): string;
803
823
  declare function weAppJwtDecode(token: any, options: any): any;
804
824
 
805
- export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type DateObj, type DateValue, type DebounceFunc, type FileType, HEX_POOL, type ICanvasWM, type ICompressOptions, 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, compressImg, 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, isNullOrUnDef, 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, supportCanvas, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, urlDelParams, urlParse, urlSetParams, urlStringify, wait, weAppJwtDecode, weAtob, weBtoa };
825
+ export { type AnyArray, type AnyFunc, type AnyObject, type ArrayElements, type DateObj, type DateValue, type DebounceFunc, type FileType, HEX_POOL, type ICanvasWM, type ICompressOptions, type IFieldOptions, type ISearchTreeOpts, 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, compressImg, cookieDel, cookieGet, cookieSet, copyText, dateParse, dateToEnd, dateToStart, debounce, downloadBlob, downloadData, downloadHref, downloadURL, flatTree, forEachDeep, forEachMap, formatDate, formatNumber, formatTree, fuzzySearchTree, genCanvasWM, getComputedCssVal, getGlobal, getStrWidthPx, getStyle, hasClass, isArray, isBigInt, isBoolean, isDate, isDomReady, isError, isFunction, isNaN, isNull, isNullOrUnDef, 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, supportCanvas, throttle, tooltipEvent, typeIs, uniqueNumber, uniqueString, urlDelParams, urlParse, urlSetParams, urlStringify, wait, weAppJwtDecode, weAtob, weBtoa };
package/lib/umd/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core v1.3.1
2
+ * sculp-js v1.4.0
3
3
  * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,7 +7,7 @@
7
7
  (function (global, factory) {
8
8
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('bezier-easing')) :
9
9
  typeof define === 'function' && define.amd ? define(['exports', 'bezier-easing'], factory) :
10
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.core = {}, global.bezier));
10
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sculpJs = {}, global.bezier));
11
11
  })(this, (function (exports, bezier) { 'use strict';
12
12
 
13
13
  /**
@@ -2029,6 +2029,11 @@
2029
2029
  const tooltipEvent = { handleMouseEnter, handleMouseLeave };
2030
2030
 
2031
2031
  const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
2032
+ const defaultSearchTreeOptions = {
2033
+ childField: 'children',
2034
+ nameField: 'name',
2035
+ ignoreEmptyChild: false
2036
+ };
2032
2037
  /**
2033
2038
  * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
2034
2039
  * @param {ArrayLike<V>} tree 树形数据
@@ -2293,6 +2298,78 @@
2293
2298
  });
2294
2299
  return treeArr;
2295
2300
  }
2301
+ /**
2302
+ * 树形结构转扁平化
2303
+ * @param {any} treeList
2304
+ * @param {IFieldOptions} options
2305
+ * @return {*}
2306
+ */
2307
+ function flatTree(treeList, options = defaultFieldOptions) {
2308
+ const { childField, keyField, pidField } = options;
2309
+ return treeList.reduce((res, node) => {
2310
+ const item = {
2311
+ ...node,
2312
+ [childField]: [] // 清空子级
2313
+ };
2314
+ item.hasOwnProperty([childField]) && delete item[childField];
2315
+ res.push(item);
2316
+ if (node[childField]) {
2317
+ const children = node[childField].map(item => ({
2318
+ ...item,
2319
+ [pidField]: node[keyField] || item.pid // 给子级设置pid
2320
+ }));
2321
+ res = res.concat(flatTree(children, options));
2322
+ }
2323
+ return res;
2324
+ }, []);
2325
+ }
2326
+ /**
2327
+ * 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
2328
+ * @param {any[]} nodes
2329
+ * @param {string} query
2330
+ * @param {ISearchTreeOpts} options
2331
+ * @return {any[]}
2332
+ */
2333
+ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
2334
+ const result = [];
2335
+ for (const node of nodes) {
2336
+ // 递归检查子节点是否匹配
2337
+ const matchedChildren = node[options.childField] && node[options.childField].length > 0
2338
+ ? fuzzySearchTree(node[options.childField] || [], query, options)
2339
+ : [];
2340
+ // 检查当前节点是否匹配或者有匹配的子节点
2341
+ if (node[options.nameField].toLowerCase().includes(query.toLowerCase()) || matchedChildren.length > 0) {
2342
+ // 将当前节点加入结果中
2343
+ if (node[options.childField]) {
2344
+ if (matchedChildren.length > 0) {
2345
+ result.push({
2346
+ ...node,
2347
+ [options.childField]: matchedChildren // 包含匹配的子节点
2348
+ });
2349
+ }
2350
+ else if (options.ignoreEmptyChild) {
2351
+ node[options.childField] && delete node[options.childField];
2352
+ result.push({
2353
+ ...node
2354
+ });
2355
+ }
2356
+ else {
2357
+ result.push({
2358
+ ...node,
2359
+ ...{ [options.childField]: [] }
2360
+ });
2361
+ }
2362
+ }
2363
+ else {
2364
+ node[options.childField] && delete node[options.childField];
2365
+ result.push({
2366
+ ...node
2367
+ });
2368
+ }
2369
+ }
2370
+ }
2371
+ return result;
2372
+ }
2296
2373
 
2297
2374
  exports.HEX_POOL = HEX_POOL;
2298
2375
  exports.STRING_ARABIC_NUMERALS = STRING_ARABIC_NUMERALS;
@@ -2325,11 +2402,13 @@
2325
2402
  exports.downloadData = downloadData;
2326
2403
  exports.downloadHref = downloadHref;
2327
2404
  exports.downloadURL = downloadURL;
2405
+ exports.flatTree = flatTree;
2328
2406
  exports.forEachDeep = forEachDeep;
2329
2407
  exports.forEachMap = forEachMap;
2330
2408
  exports.formatDate = formatDate;
2331
2409
  exports.formatNumber = formatNumber;
2332
2410
  exports.formatTree = formatTree;
2411
+ exports.fuzzySearchTree = fuzzySearchTree;
2333
2412
  exports.genCanvasWM = genCanvasWM;
2334
2413
  exports.getComputedCssVal = getComputedCssVal;
2335
2414
  exports.getGlobal = getGlobal;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sculp-js",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "packageManager": "npm@8.19.2",
5
5
  "description": "js工具库",
6
6
  "scripts": {