sculp-js 1.7.1 → 1.8.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 (60) hide show
  1. package/README.md +5 -3
  2. package/lib/cjs/array.js +1 -1
  3. package/lib/cjs/async.js +1 -1
  4. package/lib/cjs/base64.js +1 -1
  5. package/lib/cjs/clipboard.js +1 -1
  6. package/lib/cjs/cookie.js +1 -1
  7. package/lib/cjs/date.js +1 -1
  8. package/lib/cjs/dom.js +1 -28
  9. package/lib/cjs/download.js +1 -1
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +1 -1
  12. package/lib/cjs/func.js +1 -1
  13. package/lib/cjs/index.js +3 -4
  14. package/lib/cjs/math.js +1 -1
  15. package/lib/cjs/number.js +1 -1
  16. package/lib/cjs/object.js +190 -24
  17. package/lib/cjs/path.js +1 -1
  18. package/lib/cjs/qs.js +1 -1
  19. package/lib/cjs/random.js +1 -1
  20. package/lib/cjs/string.js +1 -1
  21. package/lib/cjs/tooltip.js +1 -1
  22. package/lib/cjs/tree.js +30 -96
  23. package/lib/cjs/type.js +11 -4
  24. package/lib/cjs/unique.js +1 -1
  25. package/lib/cjs/url.js +1 -1
  26. package/lib/cjs/validator.js +1 -1
  27. package/lib/cjs/variable.js +1 -1
  28. package/lib/cjs/watermark.js +1 -1
  29. package/lib/cjs/we-decode.js +1 -1
  30. package/lib/es/array.js +1 -1
  31. package/lib/es/async.js +1 -1
  32. package/lib/es/base64.js +1 -1
  33. package/lib/es/clipboard.js +1 -1
  34. package/lib/es/cookie.js +1 -1
  35. package/lib/es/date.js +1 -1
  36. package/lib/es/dom.js +2 -27
  37. package/lib/es/download.js +1 -1
  38. package/lib/es/easing.js +1 -1
  39. package/lib/es/file.js +1 -1
  40. package/lib/es/func.js +1 -1
  41. package/lib/es/index.js +5 -5
  42. package/lib/es/math.js +1 -1
  43. package/lib/es/number.js +1 -1
  44. package/lib/es/object.js +190 -25
  45. package/lib/es/path.js +1 -1
  46. package/lib/es/qs.js +1 -1
  47. package/lib/es/random.js +1 -1
  48. package/lib/es/string.js +1 -1
  49. package/lib/es/tooltip.js +1 -1
  50. package/lib/es/tree.js +31 -96
  51. package/lib/es/type.js +11 -5
  52. package/lib/es/unique.js +1 -1
  53. package/lib/es/url.js +1 -1
  54. package/lib/es/validator.js +1 -1
  55. package/lib/es/variable.js +1 -1
  56. package/lib/es/watermark.js +1 -1
  57. package/lib/es/we-decode.js +1 -1
  58. package/lib/index.d.ts +52 -54
  59. package/lib/umd/index.js +228 -149
  60. package/package.json +3 -10
package/lib/cjs/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,12 +7,14 @@
7
7
  'use strict';
8
8
 
9
9
  var object = require('./object.js');
10
+ var type = require('./type.js');
10
11
 
11
12
  const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
12
13
  const defaultSearchTreeOptions = {
13
14
  childField: 'children',
14
15
  nameField: 'name',
15
- ignoreEmptyChild: false
16
+ removeEmptyChild: false,
17
+ ignoreCase: true
16
18
  };
17
19
  /**
18
20
  * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
@@ -69,7 +71,9 @@ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
69
71
  walk(tree, null);
70
72
  }
71
73
  /**
72
- * 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
74
+ * 创建一个新数组, 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
75
+ *
76
+ * 可遍历任何带有 length 属性和数字键的类数组对象
73
77
  * @param {ArrayLike<V>} tree 树形数据
74
78
  * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
75
79
  * @param {string} children 定制子元素的key
@@ -169,91 +173,6 @@ function searchTreeById(tree, nodeId, config) {
169
173
  };
170
174
  return getIds(toFlatArray(tree));
171
175
  }
172
- /**
173
- * 使用迭代函数转换数组
174
- * @param {T} array
175
- * @param {Function} callback 迭代函数
176
- * @returns {Array}
177
- */
178
- function flatMap(array, callback) {
179
- const result = [];
180
- array.forEach((value, index) => {
181
- result.push(...callback(value, index, array));
182
- });
183
- return result;
184
- }
185
- /**
186
- * 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
187
- * 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
188
- * 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
189
- * @param {string} idProp 元素ID
190
- * @param {string} parentIdProp 父元素ID
191
- * @param {object[]} items 一维数组
192
- * @returns {WithChildren<T>[]} 树
193
- * @example
194
- * const array = [
195
- * { id: 'node-1', parent: 'root' },
196
- * { id: 'node-2', parent: 'root' },
197
- * { id: 'node-3', parent: 'node-2' },
198
- * { id: 'node-4', parent: 'node-2' },
199
- * { id: 'node-5', parent: 'node-4' },
200
- * ]
201
- * const tree = buildTree('id', 'parent', array)
202
- * expect(tree).toEqual([
203
- * { id: 'node-1', parent: 'root' },
204
- * {
205
- * id: 'node-2',
206
- * parent: 'root',
207
- * children: [
208
- * { id: 'node-3', parent: 'node-2' },
209
- * {
210
- * id: 'node-4',
211
- * parent: 'node-2',
212
- * children: [{ id: 'node-5', parent: 'node-4' }],
213
- * },
214
- * ],
215
- * },
216
- * ])
217
- */
218
- function buildTree(idProp, parentIdProp, items) {
219
- const wrapperMap = new Map();
220
- const ensure = (id) => {
221
- if (wrapperMap.has(id)) {
222
- return wrapperMap.get(id);
223
- }
224
- //@ts-ignore
225
- const wrapper = { id, parent: null, item: null, children: [] };
226
- wrapperMap.set(id, wrapper);
227
- return wrapper;
228
- };
229
- for (const item of items) {
230
- const parentWrapper = ensure(item[parentIdProp]);
231
- const itemWrapper = ensure(item[idProp]);
232
- //@ts-ignore
233
- itemWrapper.parent = parentWrapper;
234
- //@ts-ignore
235
- parentWrapper.children.push(itemWrapper);
236
- //@ts-ignore
237
- itemWrapper.item = item;
238
- }
239
- const topLevelWrappers = flatMap(Array.from(wrapperMap.values()).filter(wrapper => wrapper.parent === null), wrapper => wrapper.children);
240
- return unwrapRecursively(topLevelWrappers);
241
- function unwrapRecursively(wrapperArray) {
242
- const result = [];
243
- for (const wrapper of wrapperArray) {
244
- if (wrapper.children.length === 0) {
245
- result.push(wrapper.item);
246
- }
247
- else {
248
- result.push({
249
- ...wrapper.item,
250
- children: unwrapRecursively(wrapper.children)
251
- });
252
- }
253
- }
254
- return result;
255
- }
256
- }
257
176
  /**
258
177
  * 扁平化数组转换成树(效率高于buildTree)
259
178
  * @param {any[]} list
@@ -305,20 +224,36 @@ function flatTree(treeList, options = defaultFieldOptions) {
305
224
  }
306
225
  /**
307
226
  * 模糊搜索函数,返回包含搜索字符的节点及其祖先节点, 适用于树型组件的字符过滤功能
308
- * @param {any[]} nodes
309
- * @param {string} query
227
+ * 以下搜索条件二选一,按先后优先级处理:
228
+ * 1. 过滤函数filter, 返回true/false
229
+ * 2. 匹配关键词,支持是否启用忽略大小写来判断
230
+ *
231
+ * 有以下特性:
232
+ * 1. 可配置removeEmptyChild字段,来决定是否移除搜索结果中的空children字段
233
+ * 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
234
+ * @param {V[]} nodes
235
+ * @param {IFilterCondition} filterCondition
310
236
  * @param {ISearchTreeOpts} options
311
- * @returns {any[]}
237
+ * @returns {V[]}
312
238
  */
313
- function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
239
+ function fuzzySearchTree(nodes, filterCondition, options = defaultSearchTreeOptions) {
240
+ if (!type.objectHas(filterCondition, 'filter') &&
241
+ (!type.objectHas(filterCondition, 'keyword') || type.isEmpty(filterCondition.keyword))) {
242
+ return nodes;
243
+ }
314
244
  const result = [];
315
245
  for (const node of nodes) {
316
246
  // 递归检查子节点是否匹配
317
247
  const matchedChildren = node[options.childField] && node[options.childField].length > 0
318
- ? fuzzySearchTree(node[options.childField] || [], query, options)
248
+ ? fuzzySearchTree(node[options.childField] || [], filterCondition, options)
319
249
  : [];
320
250
  // 检查当前节点是否匹配或者有匹配的子节点
321
- if (node[options.nameField].toLowerCase().includes(query.toLowerCase()) || matchedChildren.length > 0) {
251
+ if ((type.objectHas(filterCondition, 'filter')
252
+ ? filterCondition.filter(node)
253
+ : !options.ignoreCase
254
+ ? node[options.nameField].includes(filterCondition.keyword)
255
+ : node[options.nameField].toLowerCase().includes(filterCondition.keyword.toLowerCase())) ||
256
+ matchedChildren.length > 0) {
322
257
  // 将当前节点加入结果中
323
258
  if (node[options.childField]) {
324
259
  if (matchedChildren.length > 0) {
@@ -327,7 +262,7 @@ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
327
262
  [options.childField]: matchedChildren // 包含匹配的子节点
328
263
  });
329
264
  }
330
- else if (options.ignoreEmptyChild) {
265
+ else if (options.removeEmptyChild) {
331
266
  node[options.childField] && delete node[options.childField];
332
267
  result.push({
333
268
  ...node
@@ -351,7 +286,6 @@ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
351
286
  return result;
352
287
  }
353
288
 
354
- exports.buildTree = buildTree;
355
289
  exports.flatTree = flatTree;
356
290
  exports.forEachDeep = forEachDeep;
357
291
  exports.formatTree = formatTree;
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -34,11 +34,17 @@ function arrayLike(any) {
34
34
  return objectHas(any, 'length');
35
35
  }
36
36
  /**
37
- * 判断任意值的数据类型
37
+ * 判断任意值的数据类型,检查非对象时不如typeof、instanceof的性能高
38
+ *
39
+ * 当检查类对象时是不可靠的,对象可以通过定义 Symbol.toStringTag 属性来更改检查结果
40
+ *
41
+ * 详见:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
38
42
  * @param {unknown} any
39
- * @returns {string}
43
+ * @returns
40
44
  */
41
- const typeIs = (any) => Object.prototype.toString.call(any).slice(8, -1);
45
+ function typeIs(any) {
46
+ return Object.prototype.toString.call(any).slice(8, -1);
47
+ }
42
48
  // 基本数据类型判断
43
49
  const isString = (any) => typeof any === 'string';
44
50
  const isBoolean = (any) => typeof any === 'boolean';
@@ -131,6 +137,7 @@ exports.isJsonString = isJsonString;
131
137
  exports.isNaN = isNaN;
132
138
  exports.isNull = isNull;
133
139
  exports.isNullOrUnDef = isNullOrUnDef;
140
+ exports.isNullish = isNullOrUnDef;
134
141
  exports.isNumber = isNumber;
135
142
  exports.isObject = isObject;
136
143
  exports.isPrimitive = isPrimitive;
package/lib/cjs/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present 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.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/base64.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -116,31 +116,6 @@ function smoothScroll(options) {
116
116
  render();
117
117
  });
118
118
  }
119
- const domReadyCallbacks = [];
120
- const eventType = 'DOMContentLoaded';
121
- const listener = () => {
122
- domReadyCallbacks.forEach(callback => callback());
123
- domReadyCallbacks.length = 0;
124
- document.removeEventListener(eventType, listener);
125
- };
126
- document.addEventListener(eventType, listener);
127
- let readied = false;
128
- function isDomReady() {
129
- if (readied)
130
- return true;
131
- readied = ['complete', 'loaded', 'interactive'].indexOf(document.readyState) !== -1;
132
- return readied;
133
- }
134
- function onDomReady(callback) {
135
- // document readied
136
- if (isDomReady()) {
137
- setTimeout(callback, 0);
138
- }
139
- // listen document to ready
140
- else {
141
- domReadyCallbacks.push(callback);
142
- }
143
- }
144
119
  /**
145
120
  * 获取元素样式属性的计算值
146
121
  * @param {HTMLElement} el
@@ -183,4 +158,4 @@ function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
183
158
  return strWidth;
184
159
  }
185
160
 
186
- export { addClass, getComputedCssVal, getStrWidthPx, getStyle, hasClass, isDomReady, onDomReady, removeClass, setStyle, smoothScroll };
161
+ export { addClass, getComputedCssVal, getStrWidthPx, getStyle, hasClass, removeClass, setStyle, smoothScroll };
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -8,13 +8,13 @@ export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayRemove } from './arr
8
8
  export { copyText } from './clipboard.js';
9
9
  export { cookieDel, cookieGet, cookieSet } from './cookie.js';
10
10
  export { calculateDate, calculateDateTime, dateParse, dateToEnd, dateToStart, formatDate, isValidDate } from './date.js';
11
- export { addClass, getComputedCssVal, getStrWidthPx, getStyle, hasClass, isDomReady, onDomReady, removeClass, setStyle, smoothScroll } from './dom.js';
11
+ export { addClass, getComputedCssVal, getStrWidthPx, getStyle, hasClass, removeClass, setStyle, smoothScroll } from './dom.js';
12
12
  export { crossOriginDownload, downloadBlob, downloadData, downloadHref, downloadURL } from './download.js';
13
- export { cloneDeep, isPlainObject, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectMap, objectAssign as objectMerge, objectOmit, objectPick } from './object.js';
13
+ export { cloneDeep, isEqual, isPlainObject, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectMap, objectAssign as objectMerge, objectOmit, objectPick } from './object.js';
14
14
  export { pathJoin, pathNormalize } from './path.js';
15
15
  export { qsParse, qsStringify } from './qs.js';
16
16
  export { STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_UPPERCASE_ALPHA, parseQueryParams, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase } from './string.js';
17
- export { arrayLike, isArray, isBigInt, isBoolean, isDate, isEmpty, isError, isFunction, isJsonString, isNaN, isNull, isNullOrUnDef, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, objectHas, typeIs } from './type.js';
17
+ export { arrayLike, isArray, isBigInt, isBoolean, isDate, isEmpty, isError, isFunction, isJsonString, isNaN, isNull, isNullOrUnDef, isNullOrUnDef as isNullish, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, objectHas, typeIs } from './type.js';
18
18
  export { urlDelParams, urlParse, urlSetParams, urlStringify } from './url.js';
19
19
  export { asyncMap, wait } from './async.js';
20
20
  export { chooseLocalFile, compressImg, supportCanvas } from './file.js';
@@ -24,7 +24,7 @@ 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, flatTree, forEachDeep, formatTree, fuzzySearchTree, mapDeep, searchTreeById } from './tree.js';
27
+ export { flatTree, forEachDeep, formatTree, fuzzySearchTree, mapDeep, searchTreeById } from './tree.js';
28
28
  export { add, divide, multiply, strip, subtract } from './math.js';
29
29
  export { weAtob, weBtoa } from './we-decode.js';
30
30
  export { decodeFromBase64, encodeToBase64 } from './base64.js';
package/lib/es/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.7.0
2
+ * sculp-js v1.8.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */