sculp-js 1.2.0 → 1.3.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 (53) hide show
  1. package/README.md +6 -1
  2. package/lib/cjs/array.js +2 -2
  3. package/lib/cjs/async.js +2 -2
  4. package/lib/cjs/clipboard.js +2 -2
  5. package/lib/cjs/cookie.js +2 -2
  6. package/lib/cjs/date.js +2 -2
  7. package/lib/cjs/dom.js +2 -2
  8. package/lib/cjs/download.js +2 -2
  9. package/lib/cjs/easing.js +2 -2
  10. package/lib/cjs/file.js +126 -2
  11. package/lib/cjs/func.js +2 -2
  12. package/lib/cjs/index.js +10 -2
  13. package/lib/cjs/number.js +2 -2
  14. package/lib/cjs/object.js +2 -2
  15. package/lib/cjs/path.js +2 -2
  16. package/lib/cjs/qs.js +2 -2
  17. package/lib/cjs/random.js +2 -2
  18. package/lib/cjs/string.js +2 -2
  19. package/lib/cjs/tooltip.js +2 -2
  20. package/lib/cjs/tree.js +74 -3
  21. package/lib/cjs/type.js +6 -2
  22. package/lib/cjs/unique.js +2 -2
  23. package/lib/cjs/url.js +2 -2
  24. package/lib/cjs/watermark.js +2 -2
  25. package/lib/cjs/we-decode.js +107 -0
  26. package/lib/es/array.js +2 -2
  27. package/lib/es/async.js +2 -2
  28. package/lib/es/clipboard.js +2 -2
  29. package/lib/es/cookie.js +2 -2
  30. package/lib/es/date.js +2 -2
  31. package/lib/es/dom.js +2 -2
  32. package/lib/es/download.js +2 -2
  33. package/lib/es/easing.js +2 -2
  34. package/lib/es/file.js +125 -3
  35. package/lib/es/func.js +2 -2
  36. package/lib/es/index.js +6 -5
  37. package/lib/es/number.js +2 -2
  38. package/lib/es/object.js +2 -2
  39. package/lib/es/path.js +2 -2
  40. package/lib/es/qs.js +2 -2
  41. package/lib/es/random.js +2 -2
  42. package/lib/es/string.js +2 -2
  43. package/lib/es/tooltip.js +2 -2
  44. package/lib/es/tree.js +74 -4
  45. package/lib/es/type.js +6 -3
  46. package/lib/es/unique.js +2 -2
  47. package/lib/es/url.js +2 -2
  48. package/lib/es/watermark.js +2 -2
  49. package/lib/es/we-decode.js +103 -0
  50. package/lib/index.d.ts +45 -2
  51. package/lib/umd/index.js +299 -3
  52. package/package.json +6 -3
  53. package/lib/tsdoc-metadata.json +0 -11
package/lib/es/date.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/dom.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/easing.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/file.js CHANGED
@@ -1,9 +1,18 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
+ import { weAtob } from './we-decode.js';
8
+
9
+ /**
10
+ * 判断是否支持canvas
11
+ * @returns {boolean}
12
+ */
13
+ function supportCanvas() {
14
+ return !!document.createElement('canvas').getContext;
15
+ }
7
16
  /**
8
17
  * 选择本地文件
9
18
  * @param {string} accept 上传的文件类型,用于过滤
@@ -25,5 +34,118 @@ function chooseLocalFile(accept, changeCb) {
25
34
  };
26
35
  return inputObj;
27
36
  }
37
+ /**
38
+ * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
39
+ * @param {File | FileList} file 文件
40
+ * @param {ICompressOptions} options
41
+ * @returns {Promise<object> | undefined}
42
+ */
43
+ function compressImg(file, options) {
44
+ console.assert(file instanceof File || file instanceof FileList, `${file} 必须是File或FileList类型`);
45
+ console.assert(supportCanvas(), `当前环境不支持 Canvas`);
46
+ let targetQuality = 0.52;
47
+ if (file instanceof File) {
48
+ const sizeKB = +parseInt((file.size / 1024).toFixed(2));
49
+ if (sizeKB < 1 * 1024) {
50
+ targetQuality = 0.85;
51
+ }
52
+ else if (sizeKB >= 1 * 1024 && sizeKB < 5 * 1024) {
53
+ targetQuality = 0.62;
54
+ }
55
+ else if (sizeKB >= 5 * 1024) {
56
+ targetQuality = 0.52;
57
+ }
58
+ }
59
+ if (options.quality) {
60
+ targetQuality = options.quality;
61
+ }
62
+ if (file instanceof FileList) {
63
+ return Promise.all(Array.from(file).map(el => compressImg(el, { mime: options.mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
64
+ }
65
+ else if (file instanceof File) {
66
+ return new Promise(resolve => {
67
+ const sizeKB = +parseInt((file.size / 1024).toFixed(2));
68
+ if (+(file.size / 1024).toFixed(2) < 200) {
69
+ resolve({
70
+ file: file
71
+ });
72
+ }
73
+ else {
74
+ const reader = new FileReader(); // 创建 FileReader
75
+ // @ts-ignore
76
+ reader.onload = ({ target: { result: src } }) => {
77
+ const image = new Image(); // 创建 img 元素
78
+ image.onload = () => {
79
+ const canvas = document.createElement('canvas'); // 创建 canvas 元素
80
+ const context = canvas.getContext('2d');
81
+ let targetWidth = image.width;
82
+ let targetHeight = image.height;
83
+ const originWidth = image.width;
84
+ const originHeight = image.height;
85
+ if (1 * 1024 <= sizeKB && sizeKB < 10 * 1024) {
86
+ const maxWidth = 1600, maxHeight = 1600;
87
+ targetWidth = originWidth;
88
+ targetHeight = originHeight;
89
+ // 图片尺寸超过的限制
90
+ if (originWidth > maxWidth || originHeight > maxHeight) {
91
+ if (originWidth / originHeight > maxWidth / maxHeight) {
92
+ // 更宽,按照宽度限定尺寸
93
+ targetWidth = maxWidth;
94
+ targetHeight = Math.round(maxWidth * (originHeight / originWidth));
95
+ }
96
+ else {
97
+ targetHeight = maxHeight;
98
+ targetWidth = Math.round(maxHeight * (originWidth / originHeight));
99
+ }
100
+ }
101
+ }
102
+ if (10 * 1024 <= sizeKB && sizeKB <= 20 * 1024) {
103
+ const maxWidth = 1400, maxHeight = 1400;
104
+ targetWidth = originWidth;
105
+ targetHeight = originHeight;
106
+ // 图片尺寸超过的限制
107
+ if (originWidth > maxWidth || originHeight > maxHeight) {
108
+ if (originWidth / originHeight > maxWidth / maxHeight) {
109
+ // 更宽,按照宽度限定尺寸
110
+ targetWidth = maxWidth;
111
+ targetHeight = Math.round(maxWidth * (originHeight / originWidth));
112
+ }
113
+ else {
114
+ targetHeight = maxHeight;
115
+ targetWidth = Math.round(maxHeight * (originWidth / originHeight));
116
+ }
117
+ }
118
+ }
119
+ canvas.width = targetWidth;
120
+ canvas.height = targetHeight;
121
+ context.clearRect(0, 0, targetWidth, targetHeight);
122
+ context.drawImage(image, 0, 0, targetWidth, targetHeight); // 绘制 canvas
123
+ const canvasURL = canvas.toDataURL(options.mime, targetQuality);
124
+ const buffer = weAtob(canvasURL.split(',')[1]);
125
+ let length = buffer.length;
126
+ const bufferArray = new Uint8Array(new ArrayBuffer(length));
127
+ while (length--) {
128
+ bufferArray[length] = buffer.charCodeAt(length);
129
+ }
130
+ const miniFile = new File([bufferArray], file.name, {
131
+ type: options.mime
132
+ });
133
+ resolve({
134
+ file: miniFile,
135
+ bufferArray,
136
+ origin: file,
137
+ beforeSrc: src,
138
+ afterSrc: canvasURL,
139
+ beforeKB: Number((file.size / 1024).toFixed(2)),
140
+ afterKB: Number((miniFile.size / 1024).toFixed(2))
141
+ });
142
+ };
143
+ image.src = src;
144
+ };
145
+ reader.readAsDataURL(file);
146
+ }
147
+ });
148
+ }
149
+ }
28
150
 
29
- export { chooseLocalFile };
151
+ export { chooseLocalFile, compressImg, supportCanvas };
package/lib/es/func.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
@@ -14,14 +14,15 @@ export { cloneDeep, isPlainObject, objectAssign, objectEach, objectEachAsync, ob
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, getStrWidthPx, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase } from './string.js';
17
- export { isArray, isBigInt, isBoolean, isDate, isError, isFunction, isNaN, isNull, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, typeIs } from './type.js';
17
+ export { isArray, isBigInt, isBoolean, isDate, isError, isFunction, isNaN, isNull, isNullOrUnDef, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, typeIs } from './type.js';
18
18
  export { urlDelParams, urlParse, urlSetParams, urlStringify } from './url.js';
19
19
  export { asyncMap, wait } from './async.js';
20
- export { chooseLocalFile } from './file.js';
20
+ export { chooseLocalFile, compressImg, supportCanvas } from './file.js';
21
21
  export { genCanvasWM } from './watermark.js';
22
22
  export { debounce, getGlobal, once, setGlobal, throttle } from './func.js';
23
23
  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';
28
+ export { weAppJwtDecode, weAtob, weBtoa } from './we-decode.js';
package/lib/es/number.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/object.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/path.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/qs.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/random.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/string.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/tooltip.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/tree.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
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
8
  /**
9
- * 自定义深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
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
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
@@ -19,6 +19,9 @@ const isNumber = (any) => typeof any === 'number' && !Number.isNaN(any);
19
19
  const isUndefined = (any) => typeof any === 'undefined';
20
20
  const isNull = (any) => any === null;
21
21
  const isPrimitive = (any) => any === null || typeof any !== 'object';
22
+ function isNullOrUnDef(val) {
23
+ return isUndefined(val) || isNull(val);
24
+ }
22
25
  // 复合数据类型判断
23
26
  const isObject = (any) => typeIs(any) === 'Object';
24
27
  const isArray = (any) => Array.isArray(any);
@@ -34,4 +37,4 @@ const isDate = (any) => typeIs(any) === 'Date';
34
37
  const isError = (any) => typeIs(any) === 'Error';
35
38
  const isRegExp = (any) => typeIs(any) === 'RegExp';
36
39
 
37
- export { typeIs as default, isArray, isBigInt, isBoolean, isDate, isError, isFunction, isNaN, isNull, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, typeIs };
40
+ export { typeIs as default, isArray, isBigInt, isBoolean, isDate, isError, isFunction, isNaN, isNull, isNullOrUnDef, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, typeIs };
package/lib/es/unique.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
package/lib/es/url.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * sculp-js v1.1.0
3
- * (c) 2023-2023 chandq
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
@@ -0,0 +1,103 @@
1
+ /*!
2
+ * sculp-js v1.3.0
3
+ * (c) 2023-2024 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
8
+ // eslint-disable-next-line
9
+ const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
10
+ /**
11
+ * 字符串编码成Base64 (适用于任何环境,包括小程序)
12
+ * @param {string} string
13
+ * @return {string}
14
+ */
15
+ function weBtoa(string) {
16
+ // 同window.btoa: 字符串编码成Base64
17
+ string = String(string);
18
+ let bitmap, a, b, c, result = '', i = 0;
19
+ const rest = string.length % 3;
20
+ for (; i < string.length;) {
21
+ if ((a = string.charCodeAt(i++)) > 255 || (b = string.charCodeAt(i++)) > 255 || (c = string.charCodeAt(i++)) > 255)
22
+ throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
23
+ bitmap = (a << 16) | (b << 8) | c;
24
+ result +=
25
+ b64.charAt((bitmap >> 18) & 63) +
26
+ b64.charAt((bitmap >> 12) & 63) +
27
+ b64.charAt((bitmap >> 6) & 63) +
28
+ b64.charAt(bitmap & 63);
29
+ }
30
+ return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
31
+ }
32
+ /**
33
+ * Base64解码为原始字符串(适用于任何环境,包括小程序)
34
+ * @param {string} string
35
+ * @return {string}
36
+ */
37
+ function weAtob(string) {
38
+ // 同window.atob: Base64解码为原始字符串
39
+ string = String(string).replace(/[\t\n\f\r ]+/g, '');
40
+ if (!b64re.test(string))
41
+ throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
42
+ string += '=='.slice(2 - (string.length & 3));
43
+ let bitmap, result = '', r1, r2, i = 0;
44
+ for (; i < string.length;) {
45
+ bitmap =
46
+ (b64.indexOf(string.charAt(i++)) << 18) |
47
+ (b64.indexOf(string.charAt(i++)) << 12) |
48
+ ((r1 = b64.indexOf(string.charAt(i++))) << 6) |
49
+ (r2 = b64.indexOf(string.charAt(i++)));
50
+ result +=
51
+ r1 === 64
52
+ ? String.fromCharCode((bitmap >> 16) & 255)
53
+ : r2 === 64
54
+ ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
55
+ : String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
56
+ }
57
+ return result;
58
+ }
59
+ function b64DecodeUnicode(str) {
60
+ return decodeURIComponent(exports.weAtob(str).replace(/(.)/g, function (p) {
61
+ let code = p.charCodeAt(0).toString(16).toUpperCase();
62
+ if (code.length < 2) {
63
+ code = '0' + code;
64
+ }
65
+ return '%' + code;
66
+ }));
67
+ }
68
+ function base64_url_decode(str) {
69
+ let output = str.replace(/-/g, '+').replace(/_/g, '/');
70
+ switch (output.length % 4) {
71
+ case 0:
72
+ break;
73
+ case 2:
74
+ output += '==';
75
+ break;
76
+ case 3:
77
+ output += '=';
78
+ break;
79
+ default:
80
+ throw new Error('Illegal base64url string!');
81
+ }
82
+ try {
83
+ return b64DecodeUnicode(output);
84
+ }
85
+ catch (err) {
86
+ return exports.weAtob(output);
87
+ }
88
+ }
89
+ function weAppJwtDecode(token, options) {
90
+ if (typeof token !== 'string') {
91
+ throw new Error('Invalid token specified');
92
+ }
93
+ options = options || {};
94
+ const pos = options.header === true ? 0 : 1;
95
+ try {
96
+ return JSON.parse(base64_url_decode(token.split('.')[pos]));
97
+ }
98
+ catch (e) {
99
+ throw new Error('Invalid token specified: ' + e.message);
100
+ }
101
+ }
102
+
103
+ export { weAppJwtDecode, weAtob, weBtoa };
package/lib/index.d.ts CHANGED
@@ -23,6 +23,7 @@ declare const isNumber: (any: unknown) => any is number;
23
23
  declare const isUndefined: (any: unknown) => any is undefined;
24
24
  declare const isNull: (any: unknown) => any is null;
25
25
  declare const isPrimitive: (any: unknown) => boolean;
26
+ declare function isNullOrUnDef(val: unknown): val is null | undefined;
26
27
  declare const isObject: (any: unknown) => any is Record<string, unknown>;
27
28
  declare const isArray: (any: unknown) => any is unknown[];
28
29
  /**
@@ -504,6 +505,11 @@ declare function wait(timeout?: number): Promise<void>;
504
505
  */
505
506
  declare function asyncMap<T, R>(list: Array<T>, mapper: (val: T, idx: number, list: Array<T>) => Promise<R>, concurrency?: number): Promise<R[]>;
506
507
 
508
+ /**
509
+ * 判断是否支持canvas
510
+ * @returns {boolean}
511
+ */
512
+ declare function supportCanvas(): boolean;
507
513
  /**
508
514
  * 选择本地文件
509
515
  * @param {string} accept 上传的文件类型,用于过滤
@@ -511,6 +517,20 @@ declare function asyncMap<T, R>(list: Array<T>, mapper: (val: T, idx: number, li
511
517
  * @returns {HTMLInputElement}
512
518
  */
513
519
  declare function chooseLocalFile(accept: string, changeCb: (FileList: any) => any): HTMLInputElement;
520
+ type ImageType = 'image/jpeg' | 'image/png' | 'image/webp';
521
+ interface ICompressOptions {
522
+ /** 压缩质量 0 ~ 1 之间*/
523
+ quality?: number;
524
+ /** 图片类型 */
525
+ mime?: ImageType;
526
+ }
527
+ /**
528
+ * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
529
+ * @param {File | FileList} file 文件
530
+ * @param {ICompressOptions} options
531
+ * @returns {Promise<object> | undefined}
532
+ */
533
+ declare function compressImg(file: File | FileList, options: ICompressOptions): Promise<object> | undefined;
514
534
 
515
535
  interface ICanvasWM {
516
536
  container: HTMLElement;
@@ -689,7 +709,7 @@ interface IFieldOptions {
689
709
  pidField: string;
690
710
  }
691
711
  /**
692
- * 自定义深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
712
+ * 深度优先遍历函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
693
713
  * @param {ArrayLike<V>} tree 树形数据
694
714
  * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
695
715
  * @param {string} children 定制子元素的key
@@ -697,6 +717,15 @@ interface IFieldOptions {
697
717
  * @returns {*}
698
718
  */
699
719
  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;
720
+ /**
721
+ * 深度优先遍历的Map函数(支持continue和break操作), 可用于insert tree item 和 remove tree item
722
+ * @param {ArrayLike<V>} tree 树形数据
723
+ * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
724
+ * @param {string} children 定制子元素的key
725
+ * @param {boolean} isReverse 是否反向遍历
726
+ * @returns {any[]} 新的一棵树
727
+ */
728
+ 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
729
  type IdLike = number | string;
701
730
  interface ITreeConf {
702
731
  id: string | number;
@@ -758,4 +787,18 @@ declare function buildTree<ID extends string, PID extends string, T extends {
758
787
  */
759
788
  declare function formatTree(list: any[], options?: IFieldOptions): any[];
760
789
 
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 };
790
+ /**
791
+ * 字符串编码成Base64 (适用于任何环境,包括小程序)
792
+ * @param {string} string
793
+ * @return {string}
794
+ */
795
+ declare function weBtoa(string: string): string;
796
+ /**
797
+ * Base64解码为原始字符串(适用于任何环境,包括小程序)
798
+ * @param {string} string
799
+ * @return {string}
800
+ */
801
+ declare function weAtob(string: string): string;
802
+ declare function weAppJwtDecode(token: any, options: any): any;
803
+
804
+ 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 };