sculp-js 1.13.3-beta.0 → 1.13.6
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.
- package/dist/cjs/_virtual/_commonjsHelpers.js +3 -3
- package/dist/cjs/array.js +36 -46
- package/dist/cjs/async.js +41 -43
- package/dist/cjs/base64.js +77 -66
- package/dist/cjs/clipboard.js +54 -53
- package/dist/cjs/cloneDeep.js +90 -91
- package/dist/cjs/cookie.js +27 -30
- package/dist/cjs/date.js +101 -98
- package/dist/cjs/dom.js +114 -121
- package/dist/cjs/download.js +77 -78
- package/dist/cjs/easing.js +29 -32
- package/dist/cjs/file.js +147 -152
- package/dist/cjs/func.js +64 -78
- package/dist/cjs/index.js +1 -3
- package/dist/cjs/math.js +32 -38
- package/dist/cjs/node_modules/bezier-easing/src/index.js +35 -23
- package/dist/cjs/number.js +58 -61
- package/dist/cjs/object.js +112 -126
- package/dist/cjs/path.js +39 -43
- package/dist/cjs/qs.js +34 -41
- package/dist/cjs/random.js +31 -34
- package/dist/cjs/string.js +62 -63
- package/dist/cjs/tooltip.js +66 -69
- package/dist/cjs/tree.js +265 -263
- package/dist/cjs/type.js +42 -46
- package/dist/cjs/unique.js +39 -42
- package/dist/cjs/url.js +47 -48
- package/dist/cjs/validator.js +38 -38
- package/dist/cjs/variable.js +27 -21
- package/dist/cjs/watermark.js +78 -67
- package/dist/esm/array.js +36 -46
- package/dist/esm/async.js +41 -43
- package/dist/esm/base64.js +77 -66
- package/dist/esm/clipboard.js +55 -54
- package/dist/esm/cloneDeep.js +90 -91
- package/dist/esm/cookie.js +27 -30
- package/dist/esm/date.js +101 -98
- package/dist/esm/dom.js +114 -121
- package/dist/esm/download.js +78 -79
- package/dist/esm/easing.js +29 -32
- package/dist/esm/file.js +147 -152
- package/dist/esm/func.js +64 -78
- package/dist/esm/index.js +96 -8
- package/dist/esm/math.js +32 -38
- package/dist/esm/number.js +58 -61
- package/dist/esm/object.js +124 -127
- package/dist/esm/path.js +39 -43
- package/dist/esm/qs.js +34 -41
- package/dist/esm/random.js +31 -34
- package/dist/esm/string.js +74 -64
- package/dist/esm/tooltip.js +66 -69
- package/dist/esm/tree.js +265 -263
- package/dist/esm/type.js +68 -47
- package/dist/esm/unique.js +39 -42
- package/dist/esm/url.js +47 -48
- package/dist/esm/validator.js +56 -39
- package/dist/esm/variable.js +27 -21
- package/dist/esm/watermark.js +79 -68
- package/dist/types/array.d.ts +0 -1
- package/dist/types/async.d.ts +0 -1
- package/dist/types/base64.d.ts +0 -1
- package/dist/types/clipboard.d.ts +0 -1
- package/dist/types/cloneDeep.d.ts +0 -1
- package/dist/types/cookie.d.ts +0 -1
- package/dist/types/date.d.ts +0 -1
- package/dist/types/dom.d.ts +0 -1
- package/dist/types/download.d.ts +0 -1
- package/dist/types/easing.d.ts +0 -1
- package/dist/types/file.d.ts +0 -1
- package/dist/types/func.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/math.d.ts +0 -1
- package/dist/types/number.d.ts +0 -1
- package/dist/types/object.d.ts +0 -1
- package/dist/types/path.d.ts +0 -1
- package/dist/types/qs.d.ts +0 -1
- package/dist/types/random.d.ts +0 -1
- package/dist/types/string.d.ts +0 -1
- package/dist/types/tooltip.d.ts +0 -1
- package/dist/types/tree.d.ts +2 -2
- package/dist/types/type.d.ts +0 -1
- package/dist/types/unique.d.ts +0 -1
- package/dist/types/url.d.ts +0 -1
- package/dist/types/validator.d.ts +0 -1
- package/dist/types/variable.d.ts +0 -1
- package/dist/types/watermark.d.ts +0 -1
- package/dist/umd/index.min.js +2 -2
- package/package.json +12 -6
- package/dist/types/array.d.ts.map +0 -1
- package/dist/types/async.d.ts.map +0 -1
- package/dist/types/base64.d.ts.map +0 -1
- package/dist/types/clipboard.d.ts.map +0 -1
- package/dist/types/cloneDeep.d.ts.map +0 -1
- package/dist/types/cookie.d.ts.map +0 -1
- package/dist/types/core-index.d.ts +0 -17
- package/dist/types/core-index.d.ts.map +0 -1
- package/dist/types/date.d.ts.map +0 -1
- package/dist/types/dom.d.ts.map +0 -1
- package/dist/types/download.d.ts.map +0 -1
- package/dist/types/easing.d.ts.map +0 -1
- package/dist/types/file.d.ts.map +0 -1
- package/dist/types/func.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -3278
- package/dist/types/math.d.ts.map +0 -1
- package/dist/types/number.d.ts.map +0 -1
- package/dist/types/object.d.ts.map +0 -1
- package/dist/types/path.d.ts.map +0 -1
- package/dist/types/qs.d.ts.map +0 -1
- package/dist/types/random.d.ts.map +0 -1
- package/dist/types/string.d.ts.map +0 -1
- package/dist/types/tooltip.d.ts.map +0 -1
- package/dist/types/tree.d.ts.map +0 -1
- package/dist/types/type.d.ts.map +0 -1
- package/dist/types/unique.d.ts.map +0 -1
- package/dist/types/url.d.ts.map +0 -1
- package/dist/types/validator.d.ts.map +0 -1
- package/dist/types/variable.d.ts.map +0 -1
- package/dist/types/watermark.d.ts.map +0 -1
package/dist/cjs/watermark.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -20,74 +20,85 @@ var type = require('./type.js');
|
|
|
20
20
|
* @example genCanvasWM({ content: 'QQMusicFE' })
|
|
21
21
|
*/
|
|
22
22
|
function genCanvasWM(content = '请勿外传', canvasWM) {
|
|
23
|
-
|
|
23
|
+
const {
|
|
24
|
+
rootContainer = document.body,
|
|
25
|
+
width = '300px',
|
|
26
|
+
height = '150px',
|
|
27
|
+
textAlign = 'center',
|
|
28
|
+
textBaseline = 'middle',
|
|
29
|
+
font = '20px PingFangSC-Medium,PingFang SC',
|
|
24
30
|
// fontWeight = 500,
|
|
25
|
-
fillStyle = 'rgba(189, 177, 167, .3)',
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
31
|
+
fillStyle = 'rgba(189, 177, 167, .3)',
|
|
32
|
+
rotate = -20,
|
|
33
|
+
zIndex = 2147483647,
|
|
34
|
+
watermarkId = '__wm'
|
|
35
|
+
} = type.isNullOrUnDef(canvasWM) ? {} : canvasWM;
|
|
36
|
+
const container = type.isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
|
|
37
|
+
if (!container) {
|
|
38
|
+
throw new Error(`${rootContainer} is not valid Html Element or element selector`);
|
|
39
|
+
}
|
|
40
|
+
const canvas = document.createElement('canvas');
|
|
41
|
+
canvas.setAttribute('width', width);
|
|
42
|
+
canvas.setAttribute('height', height);
|
|
43
|
+
const ctx = canvas.getContext('2d');
|
|
44
|
+
ctx.textAlign = textAlign;
|
|
45
|
+
ctx.textBaseline = textBaseline;
|
|
46
|
+
ctx.font = font;
|
|
47
|
+
// ctx!.fontWeight = fontWeight;
|
|
48
|
+
ctx.fillStyle = fillStyle;
|
|
49
|
+
ctx.rotate((Math.PI / 180) * rotate);
|
|
50
|
+
ctx.fillText(content, parseFloat(width) / 4, parseFloat(height) / 2);
|
|
51
|
+
const base64Url = canvas.toDataURL();
|
|
52
|
+
const __wm = document.querySelector(`#${watermarkId}`);
|
|
53
|
+
const watermarkDiv = __wm || document.createElement('div');
|
|
54
|
+
const styleStr = `opacity: 1 !important; display: block !important; visibility: visible !important; position:absolute; left:0; top:0; width:100%; height:100%; z-index:${zIndex}; pointer-events:none; background-repeat:repeat; background-image:url('${base64Url}')`;
|
|
55
|
+
watermarkDiv.setAttribute('style', styleStr);
|
|
56
|
+
watermarkDiv.setAttribute('id', watermarkId);
|
|
57
|
+
watermarkDiv.classList.add('nav-height');
|
|
58
|
+
if (!__wm) {
|
|
59
|
+
container.style.position = 'relative';
|
|
60
|
+
container.appendChild(watermarkDiv);
|
|
61
|
+
}
|
|
62
|
+
const getMutableStyle = ele => {
|
|
63
|
+
const computedStyle = getComputedStyle(ele);
|
|
64
|
+
return {
|
|
65
|
+
opacity: computedStyle.getPropertyValue('opacity'),
|
|
66
|
+
zIndex: computedStyle.getPropertyValue('z-index'),
|
|
67
|
+
display: computedStyle.getPropertyValue('display'),
|
|
68
|
+
visibility: computedStyle.getPropertyValue('visibility')
|
|
60
69
|
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}
|
|
70
|
+
};
|
|
71
|
+
//@ts-ignore
|
|
72
|
+
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
|
73
|
+
if (MutationObserver) {
|
|
74
|
+
let mo = new MutationObserver(function () {
|
|
75
|
+
let __wm = document.querySelector(`#${watermarkId}`); // 只在__wm元素变动才重新调用 __canvasWM
|
|
76
|
+
if (!__wm) {
|
|
77
|
+
// 避免一直触发
|
|
78
|
+
// console.log('regenerate watermark by delete::')
|
|
79
|
+
mo.disconnect();
|
|
80
|
+
mo = null;
|
|
81
|
+
genCanvasWM(content, canvasWM);
|
|
82
|
+
} else {
|
|
83
|
+
const { opacity, zIndex, display, visibility } = getMutableStyle(__wm);
|
|
84
|
+
if (
|
|
85
|
+
(__wm && __wm.getAttribute('style') !== styleStr) ||
|
|
86
|
+
!__wm ||
|
|
87
|
+
!(opacity === '1' && zIndex === '2147483647' && display === 'block' && visibility === 'visible')
|
|
88
|
+
) {
|
|
89
|
+
// 避免一直触发
|
|
90
|
+
// console.log('regenerate watermark by inline style changed ::')
|
|
91
|
+
mo.disconnect();
|
|
92
|
+
mo = null;
|
|
93
|
+
container.removeChild(__wm);
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
__wm = null;
|
|
96
|
+
genCanvasWM(content, canvasWM);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
mo.observe(container, { attributes: true, subtree: true, childList: true });
|
|
101
|
+
}
|
|
91
102
|
}
|
|
92
103
|
|
|
93
104
|
exports.genCanvasWM = genCanvasWM;
|
package/dist/esm/array.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -13,26 +13,21 @@
|
|
|
13
13
|
* @returns {*}
|
|
14
14
|
*/
|
|
15
15
|
function arrayEach(array, iterator, reverse = false) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
else if (re === true)
|
|
22
|
-
continue;
|
|
23
|
-
}
|
|
16
|
+
if (reverse) {
|
|
17
|
+
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
18
|
+
const re = iterator(array[idx], idx, array);
|
|
19
|
+
if (re === false) break;
|
|
20
|
+
else if (re === true) continue;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
else if (re === true)
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
22
|
+
} else {
|
|
23
|
+
for (let idx = 0, len = array.length; idx < len; idx++) {
|
|
24
|
+
const re = iterator(array[idx], idx, array);
|
|
25
|
+
if (re === false) break;
|
|
26
|
+
else if (re === true) continue;
|
|
33
27
|
}
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
}
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
array = null;
|
|
36
31
|
}
|
|
37
32
|
/**
|
|
38
33
|
* 异步遍历数组,返回 false 中断遍历
|
|
@@ -58,18 +53,15 @@ function arrayEach(array, iterator, reverse = false) {
|
|
|
58
53
|
* }
|
|
59
54
|
*/
|
|
60
55
|
async function arrayEachAsync(array, iterator, reverse = false) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
56
|
+
if (reverse) {
|
|
57
|
+
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
58
|
+
if ((await iterator(array[idx], idx)) === false) break;
|
|
66
59
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
60
|
+
} else {
|
|
61
|
+
for (let idx = 0, len = array.length; idx < len; idx++) {
|
|
62
|
+
if ((await iterator(array[idx], idx)) === false) break;
|
|
72
63
|
}
|
|
64
|
+
}
|
|
73
65
|
}
|
|
74
66
|
/**
|
|
75
67
|
* 插入到目标位置之前
|
|
@@ -79,11 +71,10 @@ async function arrayEachAsync(array, iterator, reverse = false) {
|
|
|
79
71
|
* @returns {*}
|
|
80
72
|
*/
|
|
81
73
|
function arrayInsertBefore(array, start, to) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
array.splice(insertIndex, 0, source);
|
|
74
|
+
if (start === to || start + 1 === to) return;
|
|
75
|
+
const [source] = array.splice(start, 1);
|
|
76
|
+
const insertIndex = to < start ? to : to - 1;
|
|
77
|
+
array.splice(insertIndex, 0, source);
|
|
87
78
|
}
|
|
88
79
|
/**
|
|
89
80
|
* 数组删除指定项目
|
|
@@ -92,18 +83,17 @@ function arrayInsertBefore(array, start, to) {
|
|
|
92
83
|
* @returns {V[]}
|
|
93
84
|
*/
|
|
94
85
|
function arrayRemove(array, expect) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return array;
|
|
86
|
+
const indexes = [];
|
|
87
|
+
// 这里重命名一下:是为了杜绝 jest 里的 expect 与之产生检查错误
|
|
88
|
+
// eslint 的 jest 语法检查是遇到 expect 函数就以为是单元测试
|
|
89
|
+
const _expect = expect;
|
|
90
|
+
arrayEach(array, (val, idx) => {
|
|
91
|
+
if (_expect(val, idx)) indexes.push(idx);
|
|
92
|
+
});
|
|
93
|
+
indexes.forEach((val, idx) => {
|
|
94
|
+
array.splice(val - idx, 1);
|
|
95
|
+
});
|
|
96
|
+
return array;
|
|
107
97
|
}
|
|
108
98
|
|
|
109
99
|
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayRemove };
|
package/dist/esm/async.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @returns {Promise<void>}
|
|
11
11
|
*/
|
|
12
12
|
function wait(timeout = 1) {
|
|
13
|
-
|
|
13
|
+
return new Promise(resolve => setTimeout(resolve, timeout));
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* 异步遍历
|
|
@@ -22,39 +22,37 @@ function wait(timeout = 1) {
|
|
|
22
22
|
* @returns {Promise<R[]>}
|
|
23
23
|
*/
|
|
24
24
|
function asyncMap(list, mapper, concurrency = Infinity) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
});
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const iterator = list[Symbol.iterator]();
|
|
27
|
+
const limit = Math.min(list.length, concurrency);
|
|
28
|
+
const resolves = [];
|
|
29
|
+
let resolvedLength = 0;
|
|
30
|
+
let rejected;
|
|
31
|
+
let index = 0;
|
|
32
|
+
const next = () => {
|
|
33
|
+
if (rejected) return reject(rejected);
|
|
34
|
+
const it = iterator.next();
|
|
35
|
+
if (it.done) {
|
|
36
|
+
if (resolvedLength === list.length) resolve(resolves);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const current = index++;
|
|
40
|
+
mapper(it.value, current, list)
|
|
41
|
+
.then(value => {
|
|
42
|
+
resolvedLength++;
|
|
43
|
+
resolves[current] = value;
|
|
44
|
+
next();
|
|
45
|
+
})
|
|
46
|
+
.catch(err => {
|
|
47
|
+
rejected = err;
|
|
48
|
+
next();
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
// 开始
|
|
52
|
+
for (let i = 0; i < limit; i++) {
|
|
53
|
+
next();
|
|
54
|
+
}
|
|
55
|
+
});
|
|
58
56
|
}
|
|
59
57
|
/**
|
|
60
58
|
* Execute a promise safely
|
|
@@ -76,14 +74,14 @@ function asyncMap(list, mapper, concurrency = Infinity) {
|
|
|
76
74
|
}
|
|
77
75
|
*/
|
|
78
76
|
function safeAwait(promise, errorExt) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
77
|
+
return promise
|
|
78
|
+
.then(data => [null, data])
|
|
79
|
+
.catch(err => {
|
|
80
|
+
if (errorExt) {
|
|
81
|
+
const parsedError = Object.assign({}, err, errorExt);
|
|
82
|
+
return [parsedError, undefined];
|
|
83
|
+
}
|
|
84
|
+
return [err, undefined];
|
|
87
85
|
});
|
|
88
86
|
}
|
|
89
87
|
|
package/dist/esm/base64.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
2
|
+
* sculp-js v1.13.6
|
|
3
3
|
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -16,22 +16,29 @@ const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3
|
|
|
16
16
|
* @returns {string}
|
|
17
17
|
*/
|
|
18
18
|
function weBtoa(string) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
// 同window.btoa: 字符串编码成Base64
|
|
20
|
+
string = String(string);
|
|
21
|
+
let bitmap,
|
|
22
|
+
a,
|
|
23
|
+
b,
|
|
24
|
+
c,
|
|
25
|
+
result = '',
|
|
26
|
+
i = 0;
|
|
27
|
+
const strLen = string.length;
|
|
28
|
+
const rest = strLen % 3;
|
|
29
|
+
for (; i < strLen; ) {
|
|
30
|
+
if ((a = string.charCodeAt(i++)) > 255 || (b = string.charCodeAt(i++)) > 255 || (c = string.charCodeAt(i++)) > 255)
|
|
31
|
+
throw new TypeError(
|
|
32
|
+
"Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."
|
|
33
|
+
);
|
|
34
|
+
bitmap = (a << 16) | (b << 8) | c;
|
|
35
|
+
result +=
|
|
36
|
+
b64.charAt((bitmap >> 18) & 63) +
|
|
37
|
+
b64.charAt((bitmap >> 12) & 63) +
|
|
38
|
+
b64.charAt((bitmap >> 6) & 63) +
|
|
39
|
+
b64.charAt(bitmap & 63);
|
|
40
|
+
}
|
|
41
|
+
return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
|
|
35
42
|
}
|
|
36
43
|
/**
|
|
37
44
|
* Base64解码为原始字符串,平替浏览器的atob, 不包含中文的处理(适用于任何环境,包括小程序)
|
|
@@ -39,38 +46,42 @@ function weBtoa(string) {
|
|
|
39
46
|
* @returns {string}
|
|
40
47
|
*/
|
|
41
48
|
function weAtob(string) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
// 同window.atob: Base64解码为原始字符串
|
|
50
|
+
string = String(string).replace(/[\t\n\f\r ]+/g, '');
|
|
51
|
+
if (!b64re.test(string))
|
|
52
|
+
throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
|
|
53
|
+
string += '=='.slice(2 - (string.length & 3));
|
|
54
|
+
let bitmap,
|
|
55
|
+
result = '',
|
|
56
|
+
r1,
|
|
57
|
+
r2,
|
|
58
|
+
i = 0;
|
|
59
|
+
for (const strLen = string.length; i < strLen; ) {
|
|
60
|
+
bitmap =
|
|
61
|
+
(b64.indexOf(string.charAt(i++)) << 18) |
|
|
62
|
+
(b64.indexOf(string.charAt(i++)) << 12) |
|
|
63
|
+
((r1 = b64.indexOf(string.charAt(i++))) << 6) |
|
|
64
|
+
(r2 = b64.indexOf(string.charAt(i++)));
|
|
65
|
+
result +=
|
|
66
|
+
r1 === 64
|
|
67
|
+
? String.fromCharCode((bitmap >> 16) & 255)
|
|
68
|
+
: r2 === 64
|
|
69
|
+
? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
|
|
70
|
+
: String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
62
73
|
}
|
|
63
74
|
function stringToUint8Array(str) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
75
|
+
const utf8 = encodeURIComponent(str); // 将字符串转换为 UTF-8 编码
|
|
76
|
+
const uint8Array = new Uint8Array(utf8.length); // 创建 Uint8Array
|
|
77
|
+
for (let i = 0; i < utf8.length; i++) {
|
|
78
|
+
uint8Array[i] = utf8.charCodeAt(i); // 填充 Uint8Array
|
|
79
|
+
}
|
|
80
|
+
return uint8Array;
|
|
70
81
|
}
|
|
71
82
|
function uint8ArrayToString(uint8Array) {
|
|
72
|
-
|
|
73
|
-
|
|
83
|
+
const utf8 = String.fromCharCode.apply(null, uint8Array); // 将 Uint8Array 转为字符串
|
|
84
|
+
return decodeURIComponent(utf8); // 将 UTF-8 字符串解码回正常字符串
|
|
74
85
|
}
|
|
75
86
|
/**
|
|
76
87
|
* 将base64编码的字符串转换为原始字符串,包括对中文内容的处理(高性能,且支持Web、Node、小程序等任意平台)
|
|
@@ -78,16 +89,16 @@ function uint8ArrayToString(uint8Array) {
|
|
|
78
89
|
* @returns 原始字符串,包括中文内容
|
|
79
90
|
*/
|
|
80
91
|
function b64decode(base64) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
92
|
+
const binaryString = !isNullOrUnDef(getGlobal('atob')) ? getGlobal('atob')(base64) : weAtob(base64);
|
|
93
|
+
const len = binaryString.length;
|
|
94
|
+
const bytes = new Uint8Array(len);
|
|
95
|
+
for (let i = 0; i < len; i++) {
|
|
96
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
97
|
+
}
|
|
98
|
+
// 使用TextDecoder将Uint8Array转换为原始字符串,包括中文内容
|
|
99
|
+
return !isNullOrUnDef(getGlobal('TextDecoder'))
|
|
100
|
+
? new (getGlobal('TextDecoder'))('utf-8').decode(bytes)
|
|
101
|
+
: uint8ArrayToString(bytes);
|
|
91
102
|
}
|
|
92
103
|
/**
|
|
93
104
|
* 将原始字符串,包括中文内容,转换为base64编码的字符串(高性能,且支持Web、Node、小程序等任意平台)
|
|
@@ -95,17 +106,17 @@ function b64decode(base64) {
|
|
|
95
106
|
* @returns base64编码的字符串
|
|
96
107
|
*/
|
|
97
108
|
function b64encode(rawStr) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
const utf8Array = !isNullOrUnDef(getGlobal('TextEncoder'))
|
|
110
|
+
? new (getGlobal('TextEncoder'))().encode(rawStr)
|
|
111
|
+
: stringToUint8Array(rawStr);
|
|
112
|
+
// 将 Uint8Array 转换为二进制字符串
|
|
113
|
+
let binaryString = '';
|
|
114
|
+
const len = utf8Array.length;
|
|
115
|
+
for (let i = 0; i < len; i++) {
|
|
116
|
+
binaryString += String.fromCharCode(utf8Array[i]);
|
|
117
|
+
}
|
|
118
|
+
// 将二进制字符串转换为base64编码的字符串
|
|
119
|
+
return !isNullOrUnDef(getGlobal('btoa')) ? getGlobal('btoa')(binaryString) : weBtoa(binaryString);
|
|
109
120
|
}
|
|
110
121
|
|
|
111
122
|
export { b64decode, b64encode, weAtob, weBtoa };
|