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/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  # sculp-js
9
9
 
10
- [API文档](https://chandq.github.io/sculp-js/)
10
+ [API Doc](https://chandq.github.io/sculp-js/)
11
11
 
12
12
  > TS + Rollup, native implementation, without relying on any third-party libraries, outputs products of three module modes: ESM, CJS, and UMD. sculp-js only used to Web environment, @sculp/core can be used to Web、Node.js、Mini Program.
13
13
 
@@ -29,13 +29,14 @@
29
29
  - isNumber
30
30
  - isUndefined
31
31
  - isNull
32
- - isNullOrUnDef
32
+ - isNullish
33
33
  - isPrimitive
34
34
  - isFunction
35
35
  - isObject
36
36
  - isArray
37
37
  - typeIs
38
38
  - isJsonString
39
+ - isEmpty
39
40
 
40
41
  - encode/decode
41
42
 
@@ -60,11 +61,12 @@
60
61
 
61
62
  - isPlainObject
62
63
  - objectHas
63
- - objectAssign 合并
64
+ - objectAssign
64
65
  - objectEach
65
66
  - objectEachAsync
66
67
  - objectGet
67
68
  - cloneDeep
69
+ - isEqual
68
70
 
69
71
  - Number
70
72
 
package/lib/cjs/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/cjs/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/cjs/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/cjs/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/cjs/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/cjs/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
  */
@@ -118,31 +118,6 @@ function smoothScroll(options) {
118
118
  render();
119
119
  });
120
120
  }
121
- const domReadyCallbacks = [];
122
- const eventType = 'DOMContentLoaded';
123
- const listener = () => {
124
- domReadyCallbacks.forEach(callback => callback());
125
- domReadyCallbacks.length = 0;
126
- document.removeEventListener(eventType, listener);
127
- };
128
- document.addEventListener(eventType, listener);
129
- let readied = false;
130
- function isDomReady() {
131
- if (readied)
132
- return true;
133
- readied = ['complete', 'loaded', 'interactive'].indexOf(document.readyState) !== -1;
134
- return readied;
135
- }
136
- function onDomReady(callback) {
137
- // document readied
138
- if (isDomReady()) {
139
- setTimeout(callback, 0);
140
- }
141
- // listen document to ready
142
- else {
143
- domReadyCallbacks.push(callback);
144
- }
145
- }
146
121
  /**
147
122
  * 获取元素样式属性的计算值
148
123
  * @param {HTMLElement} el
@@ -190,8 +165,6 @@ exports.getComputedCssVal = getComputedCssVal;
190
165
  exports.getStrWidthPx = getStrWidthPx;
191
166
  exports.getStyle = getStyle;
192
167
  exports.hasClass = hasClass;
193
- exports.isDomReady = isDomReady;
194
- exports.onDomReady = onDomReady;
195
168
  exports.removeClass = removeClass;
196
169
  exports.setStyle = setStyle;
197
170
  exports.smoothScroll = 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/cjs/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/cjs/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/cjs/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/cjs/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
  */
@@ -55,8 +55,6 @@ exports.getComputedCssVal = dom.getComputedCssVal;
55
55
  exports.getStrWidthPx = dom.getStrWidthPx;
56
56
  exports.getStyle = dom.getStyle;
57
57
  exports.hasClass = dom.hasClass;
58
- exports.isDomReady = dom.isDomReady;
59
- exports.onDomReady = dom.onDomReady;
60
58
  exports.removeClass = dom.removeClass;
61
59
  exports.setStyle = dom.setStyle;
62
60
  exports.smoothScroll = dom.smoothScroll;
@@ -66,6 +64,7 @@ exports.downloadData = download.downloadData;
66
64
  exports.downloadHref = download.downloadHref;
67
65
  exports.downloadURL = download.downloadURL;
68
66
  exports.cloneDeep = object.cloneDeep;
67
+ exports.isEqual = object.isEqual;
69
68
  exports.isPlainObject = object.isPlainObject;
70
69
  exports.objectAssign = object.objectAssign;
71
70
  exports.objectEach = object.objectEach;
@@ -102,6 +101,7 @@ exports.isJsonString = type.isJsonString;
102
101
  exports.isNaN = type.isNaN;
103
102
  exports.isNull = type.isNull;
104
103
  exports.isNullOrUnDef = type.isNullOrUnDef;
104
+ exports.isNullish = type.isNullOrUnDef;
105
105
  exports.isNumber = type.isNumber;
106
106
  exports.isObject = type.isObject;
107
107
  exports.isPrimitive = type.isPrimitive;
@@ -138,7 +138,6 @@ exports.UNIQUE_NUMBER_SAFE_LENGTH = unique.UNIQUE_NUMBER_SAFE_LENGTH;
138
138
  exports.uniqueNumber = unique.uniqueNumber;
139
139
  exports.uniqueString = unique.uniqueString;
140
140
  exports.tooltipEvent = tooltip.tooltipEvent;
141
- exports.buildTree = tree.buildTree;
142
141
  exports.flatTree = tree.flatTree;
143
142
  exports.forEachDeep = tree.forEachDeep;
144
143
  exports.formatTree = tree.formatTree;
package/lib/cjs/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/cjs/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
  */
package/lib/cjs/object.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
  */
@@ -83,7 +83,7 @@ function objectPick(obj, keys) {
83
83
  return obj2;
84
84
  }
85
85
  /**
86
- * 对象祛除
86
+ * 对象去除
87
87
  * @param {O} obj
88
88
  * @param {K} keys
89
89
  * @returns {Pick<O, ArrayElements<K>>}
@@ -168,6 +168,13 @@ function objectFill(source, target, fillable) {
168
168
  });
169
169
  return source;
170
170
  }
171
+ /**
172
+ * 获取对象指定层级下的属性值(现在可用ES6+的可选链?.来替代)
173
+ * @param {AnyObject} obj
174
+ * @param {string} path
175
+ * @param {boolean} strict
176
+ * @returns
177
+ */
171
178
  function objectGet(obj, path, strict = false) {
172
179
  path = path.replace(/\[(\w+)\]/g, '.$1');
173
180
  path = path.replace(/^\./, '');
@@ -198,12 +205,16 @@ function objectGet(obj, path, strict = false) {
198
205
  }
199
206
  /**
200
207
  * 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
208
+ *
209
+ * 包含对null、原始值、对象循环引用的处理
210
+ *
211
+ * 对Map、Set、ArrayBuffer、Date、RegExp、Array、Object及原型链属性方法执行深拷贝
201
212
  * @param {T} source
202
213
  * @param {WeakMap} map
203
214
  * @returns {T}
204
215
  */
205
216
  function cloneDeep(source, map = new WeakMap()) {
206
- // 处理原始类型(非对象/数组)和 null/undefined
217
+ // 处理原始类型和 null/undefined
207
218
  if (source === null || typeof source !== 'object') {
208
219
  return source;
209
220
  }
@@ -211,28 +222,33 @@ function cloneDeep(source, map = new WeakMap()) {
211
222
  if (map.has(source)) {
212
223
  return map.get(source);
213
224
  }
214
- // 处理 Date 类型
225
+ // 处理 ArrayBuffer
226
+ if (source instanceof ArrayBuffer) {
227
+ const copy = new ArrayBuffer(source.byteLength);
228
+ new Uint8Array(copy).set(new Uint8Array(source));
229
+ map.set(source, copy);
230
+ return copy;
231
+ }
232
+ // 处理 DataView 和 TypedArray (Uint8Array 等)
233
+ if (ArrayBuffer.isView(source)) {
234
+ const constructor = source.constructor;
235
+ const bufferCopy = cloneDeep(source.buffer, map);
236
+ return new constructor(bufferCopy, source.byteOffset, source.length);
237
+ }
238
+ // 处理 Date 对象
215
239
  if (source instanceof Date) {
216
240
  const copy = new Date(source.getTime());
217
241
  map.set(source, copy);
218
242
  return copy;
219
243
  }
220
- // 处理 RegExp 类型
244
+ // 处理 RegExp 对象
221
245
  if (source instanceof RegExp) {
222
246
  const copy = new RegExp(source.source, source.flags);
247
+ copy.lastIndex = source.lastIndex; // 保留匹配状态
223
248
  map.set(source, copy);
224
249
  return copy;
225
250
  }
226
- // 处理数组类型
227
- if (Array.isArray(source)) {
228
- const copy = [];
229
- map.set(source, copy);
230
- for (const item of source) {
231
- copy.push(cloneDeep(item, map));
232
- }
233
- return copy;
234
- }
235
- // 处理 Map 类型
251
+ // 处理 Map
236
252
  if (source instanceof Map) {
237
253
  const copy = new Map();
238
254
  map.set(source, copy);
@@ -241,7 +257,7 @@ function cloneDeep(source, map = new WeakMap()) {
241
257
  });
242
258
  return copy;
243
259
  }
244
- // 处理 Set 类型
260
+ // 处理 Set
245
261
  if (source instanceof Set) {
246
262
  const copy = new Set();
247
263
  map.set(source, copy);
@@ -250,24 +266,174 @@ function cloneDeep(source, map = new WeakMap()) {
250
266
  });
251
267
  return copy;
252
268
  }
253
- // 处理 ArrayBuffer 类型
254
- if (source instanceof ArrayBuffer) {
255
- const copy = new ArrayBuffer(source.byteLength);
256
- new Uint8Array(copy).set(new Uint8Array(source));
269
+ // 处理数组 (包含稀疏数组)
270
+ if (Array.isArray(source)) {
271
+ const copy = Array.from({ length: source.length });
257
272
  map.set(source, copy);
273
+ // 克隆所有有效索引
274
+ for (let i = 0; i < source.length; i++) {
275
+ if (i in source) {
276
+ // 保留空位
277
+ copy[i] = cloneDeep(source[i], map);
278
+ }
279
+ }
280
+ // 克隆数组的自定义属性
281
+ const descriptors = Object.getOwnPropertyDescriptors(source);
282
+ for (const key of Reflect.ownKeys(descriptors)) {
283
+ Object.defineProperty(copy, key, {
284
+ ...descriptors[key],
285
+ value: cloneDeep(descriptors[key].value, map)
286
+ });
287
+ }
258
288
  return copy;
259
289
  }
260
- // 处理普通对象(包括原型链继承)
290
+ // 处理普通对象和类实例
261
291
  const copy = Object.create(Object.getPrototypeOf(source));
262
292
  map.set(source, copy);
263
- for (const key of Reflect.ownKeys(source)) {
264
- const value = source[key];
265
- copy[key] = cloneDeep(value, map);
293
+ const descriptors = Object.getOwnPropertyDescriptors(source);
294
+ for (const key of Reflect.ownKeys(descriptors)) {
295
+ const descriptor = descriptors[key];
296
+ if ('value' in descriptor) {
297
+ // 克隆数据属性
298
+ descriptor.value = cloneDeep(descriptor.value, map);
299
+ }
300
+ else {
301
+ // 处理访问器属性 (getter/setter)
302
+ if (descriptor.get) {
303
+ descriptor.get = cloneDeep(descriptor.get, map);
304
+ }
305
+ if (descriptor.set) {
306
+ descriptor.set = cloneDeep(descriptor.set, map);
307
+ }
308
+ }
309
+ Object.defineProperty(copy, key, descriptor);
266
310
  }
267
311
  return copy;
268
312
  }
313
+ /**
314
+ * 比较两值是否相等,适用所有数据类型
315
+ * @param {Comparable} a
316
+ * @param {Comparable} b
317
+ * @returns {boolean}
318
+ */
319
+ function isEqual(a, b) {
320
+ return deepEqual(a, b);
321
+ }
322
+ function deepEqual(a, b, compared = new WeakMap()) {
323
+ // 相同值快速返回
324
+ if (Object.is(a, b))
325
+ return true;
326
+ // 类型不同直接返回false
327
+ const typeA = Object.prototype.toString.call(a);
328
+ const typeB = Object.prototype.toString.call(b);
329
+ if (typeA !== typeB)
330
+ return false;
331
+ // 只缓存对象类型
332
+ if (type.isObject(a) && type.isObject(b)) {
333
+ if (compared.has(a))
334
+ return compared.get(a) === b;
335
+ compared.set(a, b);
336
+ compared.set(b, a);
337
+ }
338
+ // 处理特殊对象类型
339
+ switch (typeA) {
340
+ case '[object Date]':
341
+ return a.getTime() === b.getTime();
342
+ case '[object RegExp]':
343
+ return a.toString() === b.toString();
344
+ case '[object Map]':
345
+ return compareMap(a, b, compared);
346
+ case '[object Set]':
347
+ return compareSet(a, b, compared);
348
+ case '[object ArrayBuffer]':
349
+ return compareArrayBuffer(a, b);
350
+ case '[object DataView]':
351
+ return compareDataView(a, b, compared);
352
+ case '[object Int8Array]':
353
+ case '[object Uint8Array]':
354
+ case '[object Uint8ClampedArray]':
355
+ case '[object Int16Array]':
356
+ case '[object Uint16Array]':
357
+ case '[object Int32Array]':
358
+ case '[object Uint32Array]':
359
+ case '[object Float32Array]':
360
+ case '[object Float64Array]':
361
+ return compareTypedArray(a, b, compared);
362
+ case '[object Object]':
363
+ return compareObjects(a, b, compared);
364
+ case '[object Array]':
365
+ return compareArrays(a, b, compared);
366
+ }
367
+ return false;
368
+ }
369
+ // 辅助比较函数
370
+ function compareMap(a, b, compared) {
371
+ if (a.size !== b.size)
372
+ return false;
373
+ for (const [key, value] of a) {
374
+ if (!b.has(key) || !deepEqual(value, b.get(key), compared))
375
+ return false;
376
+ }
377
+ return true;
378
+ }
379
+ function compareSet(a, b, compared) {
380
+ if (a.size !== b.size)
381
+ return false;
382
+ for (const value of a) {
383
+ let found = false;
384
+ for (const bValue of b) {
385
+ if (deepEqual(value, bValue, compared)) {
386
+ found = true;
387
+ break;
388
+ }
389
+ }
390
+ if (!found)
391
+ return false;
392
+ }
393
+ return true;
394
+ }
395
+ function compareArrayBuffer(a, b) {
396
+ if (a.byteLength !== b.byteLength)
397
+ return false;
398
+ return new DataView(a).getInt32(0) === new DataView(b).getInt32(0);
399
+ }
400
+ function compareDataView(a, b, compared) {
401
+ return a.byteLength === b.byteLength && deepEqual(new Uint8Array(a.buffer), new Uint8Array(b.buffer), compared);
402
+ }
403
+ function compareTypedArray(a, b, compared) {
404
+ return a.byteLength === b.byteLength && deepEqual(Array.from(a), Array.from(b), compared);
405
+ }
406
+ function compareObjects(a, b, compared) {
407
+ const keysA = Reflect.ownKeys(a);
408
+ const keysB = Reflect.ownKeys(b);
409
+ if (keysA.length !== keysB.length)
410
+ return false;
411
+ for (const key of keysA) {
412
+ if (!keysB.includes(key))
413
+ return false;
414
+ if (!deepEqual(a[key], b[key], compared))
415
+ return false;
416
+ }
417
+ // 原型链比较
418
+ return Object.getPrototypeOf(a) === Object.getPrototypeOf(b);
419
+ }
420
+ function compareArrays(a, b, compared) {
421
+ // 增加有效索引检查
422
+ const keysA = Object.keys(a).map(Number);
423
+ const keysB = Object.keys(b).map(Number);
424
+ if (keysA.length !== keysB.length)
425
+ return false;
426
+ // 递归比较每个元素
427
+ for (let i = 0; i < a.length; i++) {
428
+ if (!deepEqual(a[i], b[i], compared))
429
+ return false;
430
+ }
431
+ // 比较数组对象的其他属性
432
+ return compareObjects(a, b, compared);
433
+ }
269
434
 
270
435
  exports.cloneDeep = cloneDeep;
436
+ exports.isEqual = isEqual;
271
437
  exports.isPlainObject = isPlainObject;
272
438
  exports.objectAssign = objectAssign;
273
439
  exports.objectEach = objectEach;
package/lib/cjs/path.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/qs.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/random.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/string.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
  */