sculp-js 1.8.2 → 1.8.4

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 (63) hide show
  1. package/lib/cjs/array.js +2 -3
  2. package/lib/cjs/async.js +1 -1
  3. package/lib/cjs/base64.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cloneDeep.js +117 -0
  6. package/lib/cjs/cookie.js +1 -1
  7. package/lib/cjs/date.js +1 -1
  8. package/lib/cjs/dom.js +1 -1
  9. package/lib/cjs/download.js +29 -5
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +1 -1
  12. package/lib/cjs/func.js +1 -28
  13. package/lib/cjs/index.js +5 -3
  14. package/lib/cjs/isEqual.js +133 -0
  15. package/lib/cjs/math.js +1 -1
  16. package/lib/cjs/number.js +1 -1
  17. package/lib/cjs/object.js +1 -235
  18. package/lib/cjs/path.js +1 -1
  19. package/lib/cjs/qs.js +1 -1
  20. package/lib/cjs/random.js +1 -1
  21. package/lib/cjs/string.js +1 -1
  22. package/lib/cjs/tooltip.js +1 -1
  23. package/lib/cjs/tree.js +1 -1
  24. package/lib/cjs/type.js +1 -1
  25. package/lib/cjs/unique.js +1 -1
  26. package/lib/cjs/url.js +1 -1
  27. package/lib/cjs/validator.js +1 -1
  28. package/lib/cjs/variable.js +3 -2
  29. package/lib/cjs/watermark.js +1 -1
  30. package/lib/cjs/we-decode.js +1 -1
  31. package/lib/es/array.js +2 -3
  32. package/lib/es/async.js +1 -1
  33. package/lib/es/base64.js +1 -1
  34. package/lib/es/clipboard.js +1 -1
  35. package/lib/es/cloneDeep.js +115 -0
  36. package/lib/es/cookie.js +1 -1
  37. package/lib/es/date.js +1 -1
  38. package/lib/es/dom.js +1 -1
  39. package/lib/es/download.js +30 -6
  40. package/lib/es/easing.js +1 -1
  41. package/lib/es/file.js +1 -1
  42. package/lib/es/func.js +1 -28
  43. package/lib/es/index.js +4 -2
  44. package/lib/es/isEqual.js +131 -0
  45. package/lib/es/math.js +1 -1
  46. package/lib/es/number.js +1 -1
  47. package/lib/es/object.js +2 -234
  48. package/lib/es/path.js +1 -1
  49. package/lib/es/qs.js +1 -1
  50. package/lib/es/random.js +1 -1
  51. package/lib/es/string.js +1 -1
  52. package/lib/es/tooltip.js +1 -1
  53. package/lib/es/tree.js +1 -1
  54. package/lib/es/type.js +1 -1
  55. package/lib/es/unique.js +1 -1
  56. package/lib/es/url.js +1 -1
  57. package/lib/es/validator.js +1 -1
  58. package/lib/es/variable.js +4 -3
  59. package/lib/es/watermark.js +1 -1
  60. package/lib/es/we-decode.js +1 -1
  61. package/lib/index.d.ts +32 -21
  62. package/lib/umd/index.js +262 -267
  63. package/package.json +1 -1
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.1
2
+ * sculp-js v1.8.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -68,7 +68,6 @@ async function arrayEachAsync(array, iterator, reverse = false) {
68
68
  function arrayInsertBefore(array, start, to) {
69
69
  if (start === to || start + 1 === to)
70
70
  return;
71
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
72
71
  const [source] = array.splice(start, 1);
73
72
  const insertIndex = to < start ? to : to - 1;
74
73
  array.splice(insertIndex, 0, source);
@@ -88,7 +87,7 @@ function arrayRemove(array, expect) {
88
87
  if (_expect(val, idx))
89
88
  indexes.push(idx);
90
89
  });
91
- arrayEach(indexes, (val, idx) => {
90
+ indexes.forEach((val, idx) => {
92
91
  array.splice(val - idx, 1);
93
92
  });
94
93
  return array;
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -0,0 +1,117 @@
1
+ /*!
2
+ * sculp-js v1.8.4
3
+ * (c) 2023-present chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ /**
10
+ * 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
11
+ *
12
+ * 包含对null、原始值、对象循环引用的处理
13
+ *
14
+ * 对Map、Set、ArrayBuffer、Date、RegExp、Array、Object及原型链属性方法执行深拷贝
15
+ * @param {T} source
16
+ * @param {WeakMap} map
17
+ * @returns {T}
18
+ */
19
+ function cloneDeep(source, map = new WeakMap()) {
20
+ // 处理原始类型和 null/undefined
21
+ if (source === null || typeof source !== 'object') {
22
+ return source;
23
+ }
24
+ // 处理循环引用
25
+ if (map.has(source)) {
26
+ return map.get(source);
27
+ }
28
+ // 处理 ArrayBuffer
29
+ if (source instanceof ArrayBuffer) {
30
+ const copy = new ArrayBuffer(source.byteLength);
31
+ new Uint8Array(copy).set(new Uint8Array(source));
32
+ map.set(source, copy);
33
+ return copy;
34
+ }
35
+ // 处理 DataView 和 TypedArray (Uint8Array 等)
36
+ if (ArrayBuffer.isView(source)) {
37
+ const constructor = source.constructor;
38
+ const bufferCopy = cloneDeep(source.buffer, map);
39
+ return new constructor(bufferCopy, source.byteOffset, source.length);
40
+ }
41
+ // 处理 Date 对象
42
+ if (source instanceof Date) {
43
+ const copy = new Date(source.getTime());
44
+ map.set(source, copy);
45
+ return copy;
46
+ }
47
+ // 处理 RegExp 对象
48
+ if (source instanceof RegExp) {
49
+ const copy = new RegExp(source.source, source.flags);
50
+ copy.lastIndex = source.lastIndex; // 保留匹配状态
51
+ map.set(source, copy);
52
+ return copy;
53
+ }
54
+ // 处理 Map
55
+ if (source instanceof Map) {
56
+ const copy = new Map();
57
+ map.set(source, copy);
58
+ source.forEach((value, key) => {
59
+ copy.set(cloneDeep(key, map), cloneDeep(value, map));
60
+ });
61
+ return copy;
62
+ }
63
+ // 处理 Set
64
+ if (source instanceof Set) {
65
+ const copy = new Set();
66
+ map.set(source, copy);
67
+ source.forEach(value => {
68
+ copy.add(cloneDeep(value, map));
69
+ });
70
+ return copy;
71
+ }
72
+ // 处理数组 (包含稀疏数组)
73
+ if (Array.isArray(source)) {
74
+ const copy = new Array(source.length);
75
+ map.set(source, copy);
76
+ // 克隆所有有效索引
77
+ for (let i = 0, len = source.length; i < len; i++) {
78
+ if (i in source) {
79
+ // 保留空位
80
+ copy[i] = cloneDeep(source[i], map);
81
+ }
82
+ }
83
+ // 克隆数组的自定义属性
84
+ const descriptors = Object.getOwnPropertyDescriptors(source);
85
+ for (const key of Reflect.ownKeys(descriptors)) {
86
+ Object.defineProperty(copy, key, {
87
+ ...descriptors[key],
88
+ value: cloneDeep(descriptors[key].value, map)
89
+ });
90
+ }
91
+ return copy;
92
+ }
93
+ // 处理普通对象和类实例
94
+ const copy = Object.create(Object.getPrototypeOf(source));
95
+ map.set(source, copy);
96
+ const descriptors = Object.getOwnPropertyDescriptors(source);
97
+ for (const key of Reflect.ownKeys(descriptors)) {
98
+ const descriptor = descriptors[key];
99
+ if ('value' in descriptor) {
100
+ // 克隆数据属性
101
+ descriptor.value = cloneDeep(descriptor.value, map);
102
+ }
103
+ else {
104
+ // 处理访问器属性 (getter/setter)
105
+ if (descriptor.get) {
106
+ descriptor.get = cloneDeep(descriptor.get, map);
107
+ }
108
+ if (descriptor.set) {
109
+ descriptor.set = cloneDeep(descriptor.set, map);
110
+ }
111
+ }
112
+ Object.defineProperty(copy, key, descriptor);
113
+ }
114
+ return copy;
115
+ }
116
+
117
+ exports.cloneDeep = cloneDeep;
package/lib/cjs/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -55,17 +55,41 @@ function downloadBlob(blob, filename, callback) {
55
55
  }
56
56
  /**
57
57
  * 根据URL下载文件(解决跨域a.download不生效问题)
58
+ *
59
+ * 可定制下载成功的状态码status(浏览器原生状态码)
60
+ *
61
+ * 支持下载操作成功、失败后的回调
58
62
  * @param {string} url
59
63
  * @param {string} filename
60
- * @param {Function} callback
64
+ * @param {CrossOriginDownloadParams} options
61
65
  */
62
- function crossOriginDownload(url, filename, callback) {
66
+ function crossOriginDownload(url, filename, options) {
67
+ const { successCode = 200, successCallback, failCallback } = type.isNullOrUnDef(options) ? { successCode: 200, successCallback: void 0, failCallback: void 0 } : options;
63
68
  const xhr = new XMLHttpRequest();
64
69
  xhr.open('GET', url, true);
65
70
  xhr.responseType = 'blob';
66
71
  xhr.onload = function () {
67
- if (xhr.status === 200)
68
- downloadBlob(xhr.response, filename, callback);
72
+ if (xhr.status === successCode)
73
+ downloadBlob(xhr.response, filename, successCallback);
74
+ else if (type.isFunction(failCallback)) {
75
+ const status = xhr.status;
76
+ const responseType = xhr.getResponseHeader('Content-Type');
77
+ if (type.isString(responseType) && responseType.includes('application/json')) {
78
+ const reader = new FileReader();
79
+ reader.onload = () => {
80
+ failCallback({ status, response: reader.result });
81
+ };
82
+ reader.readAsText(xhr.response);
83
+ }
84
+ else {
85
+ failCallback(xhr);
86
+ }
87
+ }
88
+ };
89
+ xhr.onerror = e => {
90
+ if (type.isFunction(failCallback)) {
91
+ failCallback({ status: 0, code: 'ERROR_CONNECTION_REFUSED' });
92
+ }
69
93
  };
70
94
  xhr.send();
71
95
  }
package/lib/cjs/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -86,13 +86,10 @@ const once = (func) => {
86
86
  let called = false;
87
87
  let result;
88
88
  return function (...args) {
89
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
90
89
  if (called)
91
90
  return result;
92
91
  called = true;
93
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
94
92
  result = func.call(this, ...args);
95
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
96
93
  return result;
97
94
  };
98
95
  };
@@ -102,24 +99,12 @@ const once = (func) => {
102
99
  * @param val
103
100
  */
104
101
  function setGlobal(key, val) {
105
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
106
- // @ts-ignore
107
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
108
102
  if (typeof globalThis !== 'undefined')
109
103
  globalThis[key] = val;
110
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
111
- // @ts-ignore
112
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
113
104
  else if (typeof window !== 'undefined')
114
105
  window[key] = val;
115
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
116
- // @ts-ignore
117
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
118
106
  else if (typeof global !== 'undefined')
119
107
  global[key] = val;
120
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
121
- // @ts-ignore
122
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
123
108
  else if (typeof self !== 'undefined')
124
109
  self[key] = val;
125
110
  else
@@ -131,24 +116,12 @@ function setGlobal(key, val) {
131
116
  * @param val
132
117
  */
133
118
  function getGlobal(key) {
134
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
135
- // @ts-ignore
136
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
137
119
  if (typeof globalThis !== 'undefined')
138
120
  return globalThis[key];
139
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
140
- // @ts-ignore
141
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
142
121
  else if (typeof window !== 'undefined')
143
122
  return window[key];
144
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
145
- // @ts-ignore
146
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
147
123
  else if (typeof global !== 'undefined')
148
124
  return global[key];
149
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
150
- // @ts-ignore
151
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
152
125
  else if (typeof self !== 'undefined')
153
126
  return self[key];
154
127
  }
package/lib/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.1
2
+ * sculp-js v1.8.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -32,6 +32,8 @@ var weDecode = require('./we-decode.js');
32
32
  var base64 = require('./base64.js');
33
33
  var validator = require('./validator.js');
34
34
  var variable = require('./variable.js');
35
+ var cloneDeep = require('./cloneDeep.js');
36
+ var isEqual = require('./isEqual.js');
35
37
 
36
38
 
37
39
 
@@ -63,8 +65,6 @@ exports.downloadBlob = download.downloadBlob;
63
65
  exports.downloadData = download.downloadData;
64
66
  exports.downloadHref = download.downloadHref;
65
67
  exports.downloadURL = download.downloadURL;
66
- exports.cloneDeep = object.cloneDeep;
67
- exports.isEqual = object.isEqual;
68
68
  exports.isPlainObject = object.isPlainObject;
69
69
  exports.objectAssign = object.objectAssign;
70
70
  exports.objectEach = object.objectEach;
@@ -174,3 +174,5 @@ exports.executeInScope = variable.executeInScope;
174
174
  exports.parseVarFromString = variable.parseVarFromString;
175
175
  exports.replaceVarFromString = variable.replaceVarFromString;
176
176
  exports.uniqueSymbol = variable.uniqueSymbol;
177
+ exports.cloneDeep = cloneDeep.cloneDeep;
178
+ exports.isEqual = isEqual.isEqual;
@@ -0,0 +1,133 @@
1
+ /*!
2
+ * sculp-js v1.8.4
3
+ * (c) 2023-present chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ var type = require('./type.js');
10
+
11
+ /**
12
+ * 比较两值是否相等,适用所有数据类型
13
+ * @param {Comparable} a
14
+ * @param {Comparable} b
15
+ * @returns {boolean}
16
+ */
17
+ function isEqual(a, b) {
18
+ return deepEqual(a, b);
19
+ }
20
+ function deepEqual(a, b, compared = new WeakMap()) {
21
+ // 相同值快速返回
22
+ if (Object.is(a, b))
23
+ return true;
24
+ // 类型不同直接返回false
25
+ const typeA = Object.prototype.toString.call(a);
26
+ const typeB = Object.prototype.toString.call(b);
27
+ if (typeA !== typeB)
28
+ return false;
29
+ // 只缓存对象类型
30
+ if (type.isObject(a) && type.isObject(b)) {
31
+ if (compared.has(a))
32
+ return compared.get(a) === b;
33
+ compared.set(a, b);
34
+ compared.set(b, a);
35
+ }
36
+ // 处理特殊对象类型
37
+ switch (typeA) {
38
+ case '[object Date]':
39
+ return a.getTime() === b.getTime();
40
+ case '[object RegExp]':
41
+ return a.toString() === b.toString();
42
+ case '[object Map]':
43
+ return compareMap(a, b, compared);
44
+ case '[object Set]':
45
+ return compareSet(a, b, compared);
46
+ case '[object ArrayBuffer]':
47
+ return compareArrayBuffer(a, b);
48
+ case '[object DataView]':
49
+ return compareDataView(a, b, compared);
50
+ case '[object Int8Array]':
51
+ case '[object Uint8Array]':
52
+ case '[object Uint8ClampedArray]':
53
+ case '[object Int16Array]':
54
+ case '[object Uint16Array]':
55
+ case '[object Int32Array]':
56
+ case '[object Uint32Array]':
57
+ case '[object Float32Array]':
58
+ case '[object Float64Array]':
59
+ return compareTypedArray(a, b, compared);
60
+ case '[object Object]':
61
+ return compareObjects(a, b, compared);
62
+ case '[object Array]':
63
+ return compareArrays(a, b, compared);
64
+ }
65
+ return false;
66
+ }
67
+ // 辅助比较函数
68
+ function compareMap(a, b, compared) {
69
+ if (a.size !== b.size)
70
+ return false;
71
+ for (const [key, value] of a) {
72
+ if (!b.has(key) || !deepEqual(value, b.get(key), compared))
73
+ return false;
74
+ }
75
+ return true;
76
+ }
77
+ function compareSet(a, b, compared) {
78
+ if (a.size !== b.size)
79
+ return false;
80
+ for (const value of a) {
81
+ let found = false;
82
+ for (const bValue of b) {
83
+ if (deepEqual(value, bValue, compared)) {
84
+ found = true;
85
+ break;
86
+ }
87
+ }
88
+ if (!found)
89
+ return false;
90
+ }
91
+ return true;
92
+ }
93
+ function compareArrayBuffer(a, b) {
94
+ if (a.byteLength !== b.byteLength)
95
+ return false;
96
+ return new DataView(a).getInt32(0) === new DataView(b).getInt32(0);
97
+ }
98
+ function compareDataView(a, b, compared) {
99
+ return a.byteLength === b.byteLength && deepEqual(new Uint8Array(a.buffer), new Uint8Array(b.buffer), compared);
100
+ }
101
+ function compareTypedArray(a, b, compared) {
102
+ return a.byteLength === b.byteLength && deepEqual(Array.from(a), Array.from(b), compared);
103
+ }
104
+ function compareObjects(a, b, compared) {
105
+ const keysA = Reflect.ownKeys(a);
106
+ const keysB = Reflect.ownKeys(b);
107
+ if (keysA.length !== keysB.length)
108
+ return false;
109
+ for (const key of keysA) {
110
+ if (!keysB.includes(key))
111
+ return false;
112
+ if (!deepEqual(a[key], b[key], compared))
113
+ return false;
114
+ }
115
+ // 原型链比较
116
+ return Object.getPrototypeOf(a) === Object.getPrototypeOf(b);
117
+ }
118
+ function compareArrays(a, b, compared) {
119
+ // 增加有效索引检查
120
+ const keysA = Object.keys(a).map(Number);
121
+ const keysB = Object.keys(b).map(Number);
122
+ if (keysA.length !== keysB.length)
123
+ return false;
124
+ // 递归比较每个元素
125
+ for (let i = 0, len = a.length; i < len; i++) {
126
+ if (!deepEqual(a[i], b[i], compared))
127
+ return false;
128
+ }
129
+ // 比较数组对象的其他属性
130
+ return compareObjects(a, b, compared);
131
+ }
132
+
133
+ exports.isEqual = isEqual;
package/lib/cjs/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.1
2
+ * sculp-js v1.8.4
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.8.1
2
+ * sculp-js v1.8.4
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */