sculp-js 0.0.1
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/LICENSE.md +21 -0
- package/README.md +3 -0
- package/lib/cjs/array.js +217 -0
- package/lib/cjs/async.js +63 -0
- package/lib/cjs/clipboard.js +36 -0
- package/lib/cjs/cookie.js +63 -0
- package/lib/cjs/date.js +114 -0
- package/lib/cjs/dom.js +152 -0
- package/lib/cjs/download.js +82 -0
- package/lib/cjs/easing.js +40 -0
- package/lib/cjs/file.js +30 -0
- package/lib/cjs/index.js +41 -0
- package/lib/cjs/object.js +241 -0
- package/lib/cjs/path.js +66 -0
- package/lib/cjs/qs.js +71 -0
- package/lib/cjs/string.js +134 -0
- package/lib/cjs/type.js +47 -0
- package/lib/cjs/url.js +84 -0
- package/lib/cjs/watermark.js +91 -0
- package/lib/es/array.js +208 -0
- package/lib/es/async.js +60 -0
- package/lib/es/clipboard.js +34 -0
- package/lib/es/cookie.js +59 -0
- package/lib/es/date.js +110 -0
- package/lib/es/dom.js +143 -0
- package/lib/es/download.js +77 -0
- package/lib/es/easing.js +38 -0
- package/lib/es/file.js +28 -0
- package/lib/es/index.js +35 -0
- package/lib/es/object.js +228 -0
- package/lib/es/path.js +63 -0
- package/lib/es/qs.js +68 -0
- package/lib/es/string.js +124 -0
- package/lib/es/type.js +28 -0
- package/lib/es/url.js +79 -0
- package/lib/es/watermark.js +89 -0
- package/lib/index.d.ts +644 -0
- package/lib/umd/index.js +1458 -0
- package/package.json +80 -0
package/lib/cjs/type.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
10
|
+
|
|
11
|
+
const typeIs = (any) => Object.prototype.toString.call(any).slice(8, -1);
|
|
12
|
+
// 基本数据类型判断
|
|
13
|
+
const isString = (any) => typeof any === 'string';
|
|
14
|
+
const isBoolean = (any) => typeof any === 'boolean';
|
|
15
|
+
const isSymbol = (any) => typeof any === 'symbol';
|
|
16
|
+
const isBigInt = (any) => typeof any === 'bigint';
|
|
17
|
+
const isNumber = (any) => typeof any === 'number' && !Number.isNaN(any);
|
|
18
|
+
const isUndefined = (any) => typeof any === 'undefined';
|
|
19
|
+
const isNull = (any) => any === null;
|
|
20
|
+
const isPrimitive = (any) => any === null || typeof any !== 'object';
|
|
21
|
+
// 复合数据类型判断
|
|
22
|
+
const isObject = (any) => typeIs(any) === 'Object';
|
|
23
|
+
const isArray = (any) => Array.isArray(any);
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
25
|
+
const isFunction = (any) => typeof any === 'function';
|
|
26
|
+
// 对象类型判断
|
|
27
|
+
const isNaN = (any) => Number.isNaN(any);
|
|
28
|
+
const isDate = (any) => typeIs(any) === 'Date';
|
|
29
|
+
const isError = (any) => typeIs(any) === 'Error';
|
|
30
|
+
const isRegExp = (any) => typeIs(any) === 'RegExp';
|
|
31
|
+
|
|
32
|
+
exports.default = typeIs;
|
|
33
|
+
exports.isArray = isArray;
|
|
34
|
+
exports.isBigInt = isBigInt;
|
|
35
|
+
exports.isBoolean = isBoolean;
|
|
36
|
+
exports.isDate = isDate;
|
|
37
|
+
exports.isError = isError;
|
|
38
|
+
exports.isFunction = isFunction;
|
|
39
|
+
exports.isNaN = isNaN;
|
|
40
|
+
exports.isNull = isNull;
|
|
41
|
+
exports.isNumber = isNumber;
|
|
42
|
+
exports.isObject = isObject;
|
|
43
|
+
exports.isPrimitive = isPrimitive;
|
|
44
|
+
exports.isRegExp = isRegExp;
|
|
45
|
+
exports.isString = isString;
|
|
46
|
+
exports.isSymbol = isSymbol;
|
|
47
|
+
exports.isUndefined = isUndefined;
|
package/lib/cjs/url.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var path = require('./path.js');
|
|
10
|
+
var qs = require('./qs.js');
|
|
11
|
+
|
|
12
|
+
const anchorEl = document.createElement('a');
|
|
13
|
+
/**
|
|
14
|
+
* url 解析
|
|
15
|
+
* @param {string} url
|
|
16
|
+
* @returns {Url}
|
|
17
|
+
*/
|
|
18
|
+
const urlParse = (url) => {
|
|
19
|
+
anchorEl.href = url;
|
|
20
|
+
const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = anchorEl;
|
|
21
|
+
// fix: ie 浏览器下,解析出来的 pathname 是没有 / 根的
|
|
22
|
+
const pathname = path.pathJoin('/', _pathname);
|
|
23
|
+
const auth = username && password ? `${username}:${password}` : '';
|
|
24
|
+
const query = search.replace(/^\?/, '');
|
|
25
|
+
const searchParams = qs.qsParse(query);
|
|
26
|
+
const path$1 = `${pathname}${search}`;
|
|
27
|
+
return {
|
|
28
|
+
protocol,
|
|
29
|
+
auth,
|
|
30
|
+
username,
|
|
31
|
+
password,
|
|
32
|
+
host,
|
|
33
|
+
port,
|
|
34
|
+
hostname,
|
|
35
|
+
hash,
|
|
36
|
+
search,
|
|
37
|
+
searchParams,
|
|
38
|
+
query,
|
|
39
|
+
pathname,
|
|
40
|
+
path: path$1,
|
|
41
|
+
href: url
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* url 字符化,url 对象里的 searchParams 会覆盖 url 原有的查询参数
|
|
46
|
+
* @param {Url} url
|
|
47
|
+
* @returns {string}
|
|
48
|
+
*/
|
|
49
|
+
const urlStringify = (url) => {
|
|
50
|
+
const { protocol, auth, host, pathname, searchParams, hash } = url;
|
|
51
|
+
const authorize = auth ? `${auth}@` : '';
|
|
52
|
+
const querystring = qs.qsStringify(searchParams);
|
|
53
|
+
const search = querystring ? `?${querystring}` : '';
|
|
54
|
+
let hashstring = hash.replace(/^#/, '');
|
|
55
|
+
hashstring = hashstring ? '#' + hashstring : '';
|
|
56
|
+
return `${protocol}//${authorize}${host}${pathname}${search}${hashstring}`;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* 设置 url 查询参数
|
|
60
|
+
* @param {string} url
|
|
61
|
+
* @param {AnyObject} setter
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
const urlSetParams = (url, setter) => {
|
|
65
|
+
const p = urlParse(url);
|
|
66
|
+
Object.assign(p.searchParams, setter);
|
|
67
|
+
return urlStringify(p);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* 删除 url 查询参数
|
|
71
|
+
* @param {string} url
|
|
72
|
+
* @param {string[]} removeKeys
|
|
73
|
+
* @returns {string}
|
|
74
|
+
*/
|
|
75
|
+
const urlDelParams = (url, removeKeys) => {
|
|
76
|
+
const p = urlParse(url);
|
|
77
|
+
removeKeys.forEach(key => delete p.searchParams[key]);
|
|
78
|
+
return urlStringify(p);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.urlDelParams = urlDelParams;
|
|
82
|
+
exports.urlParse = urlParse;
|
|
83
|
+
exports.urlSetParams = urlSetParams;
|
|
84
|
+
exports.urlStringify = urlStringify;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* @created: Saturday, 2020-04-18 14:38:23
|
|
11
|
+
* @author: chendq
|
|
12
|
+
* ---------
|
|
13
|
+
* @desc 网页加水印的工具类
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* canvas 实现 watermark
|
|
17
|
+
* @param {ICanvasWM} canvasWM
|
|
18
|
+
*/
|
|
19
|
+
const genCanvasWM = (canvasWM) => {
|
|
20
|
+
const { container = document.body, width = '300px', height = '200px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
|
|
21
|
+
// fontWeight = 500,
|
|
22
|
+
fillStyle = 'rgba(189, 177, 167, .3)', content = '请勿外传', rotate = 30, zIndex = 2147483647 } = canvasWM;
|
|
23
|
+
// 仅限主页面添加水印
|
|
24
|
+
if (!location.pathname.includes('/home')) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const args = canvasWM;
|
|
28
|
+
const canvas = document.createElement('canvas');
|
|
29
|
+
canvas.setAttribute('width', width);
|
|
30
|
+
canvas.setAttribute('height', height);
|
|
31
|
+
const ctx = canvas.getContext('2d');
|
|
32
|
+
ctx.textAlign = textAlign;
|
|
33
|
+
ctx.textBaseline = textBaseline;
|
|
34
|
+
ctx.font = font;
|
|
35
|
+
// ctx!.fontWeight = fontWeight;
|
|
36
|
+
ctx.fillStyle = fillStyle;
|
|
37
|
+
ctx.rotate((Math.PI / 180) * rotate);
|
|
38
|
+
ctx.fillText(content, parseFloat(width) / 4, parseFloat(height) / 2);
|
|
39
|
+
const base64Url = canvas.toDataURL();
|
|
40
|
+
const __wm = document.querySelector('.__wm');
|
|
41
|
+
const watermarkDiv = __wm || document.createElement('div');
|
|
42
|
+
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}')`;
|
|
43
|
+
watermarkDiv.setAttribute('style', styleStr);
|
|
44
|
+
watermarkDiv.classList.add('__wm');
|
|
45
|
+
watermarkDiv.classList.add('nav-height');
|
|
46
|
+
if (!__wm) {
|
|
47
|
+
container.style.position = 'relative';
|
|
48
|
+
container.appendChild(watermarkDiv);
|
|
49
|
+
}
|
|
50
|
+
const getMutableStyle = (ele) => {
|
|
51
|
+
const computedStle = getComputedStyle(ele);
|
|
52
|
+
return {
|
|
53
|
+
opacity: computedStle.getPropertyValue('opacity'),
|
|
54
|
+
zIndex: computedStle.getPropertyValue('z-index'),
|
|
55
|
+
display: computedStle.getPropertyValue('display'),
|
|
56
|
+
visibility: computedStle.getPropertyValue('visibility')
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
//@ts-ignore
|
|
60
|
+
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
|
61
|
+
if (MutationObserver) {
|
|
62
|
+
let mo = new MutationObserver(function () {
|
|
63
|
+
const __wm = document.querySelector('.__wm'); // 只在__wm元素变动才重新调用 __canvasWM
|
|
64
|
+
if (!__wm) {
|
|
65
|
+
// 避免一直触发
|
|
66
|
+
// console.log('regenerate watermark by delete::')
|
|
67
|
+
mo.disconnect();
|
|
68
|
+
mo = null;
|
|
69
|
+
genCanvasWM(JSON.parse(JSON.stringify(args)));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
const { opacity, zIndex, display, visibility } = getMutableStyle(__wm);
|
|
73
|
+
if ((__wm && __wm.getAttribute('style') !== styleStr) ||
|
|
74
|
+
!__wm ||
|
|
75
|
+
!(opacity === '1' && zIndex === '2147483647' && display === 'block' && visibility === 'visible')) {
|
|
76
|
+
// 避免一直触发
|
|
77
|
+
// console.log('regenerate watermark by inline style changed ::')
|
|
78
|
+
mo.disconnect();
|
|
79
|
+
mo = null;
|
|
80
|
+
container.removeChild(__wm);
|
|
81
|
+
genCanvasWM(JSON.parse(JSON.stringify(args)));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
mo.observe(container, { attributes: true, subtree: true, childList: true });
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
// 调用
|
|
89
|
+
// __canvasWM({ content: 'QQMusicFE' })
|
|
90
|
+
|
|
91
|
+
exports.genCanvasWM = genCanvasWM;
|
package/lib/es/array.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { objectHas } from './object.js';
|
|
8
|
+
import { isArray, isString, isObject } from './type.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 判断一个对象是否为类数组
|
|
12
|
+
* @param any
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
const arrayLike = (any) => {
|
|
16
|
+
if (isArray(any))
|
|
17
|
+
return true;
|
|
18
|
+
if (isString(any))
|
|
19
|
+
return true;
|
|
20
|
+
if (!isObject(any))
|
|
21
|
+
return false;
|
|
22
|
+
return objectHas(any, 'length');
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* 遍历数组,返回 false 中断遍历
|
|
26
|
+
* @param {ArrayLike<V>} array
|
|
27
|
+
* @param {(val: V, idx: number) => any} iterator
|
|
28
|
+
* @param reverse {boolean} 是否倒序
|
|
29
|
+
*/
|
|
30
|
+
const arrayEach = (array, iterator, reverse = false) => {
|
|
31
|
+
if (reverse) {
|
|
32
|
+
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
33
|
+
const val = array[idx];
|
|
34
|
+
if (iterator(val, idx) === false)
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
for (let idx = 0; idx < array.length; idx++) {
|
|
40
|
+
const val = array[idx];
|
|
41
|
+
if (iterator(val, idx) === false)
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 异步遍历数组,返回 false 中断遍历
|
|
48
|
+
* @param {ArrayLike<V>} array
|
|
49
|
+
* @param {(val: V, idx: number) => Promise<any>} iterator
|
|
50
|
+
* @param {boolean} reverse
|
|
51
|
+
*/
|
|
52
|
+
async function arrayEachAsync(array, iterator, reverse = false) {
|
|
53
|
+
if (reverse) {
|
|
54
|
+
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
55
|
+
const val = array[idx];
|
|
56
|
+
if ((await iterator(val, idx)) === false)
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
for (let idx = 0; idx < array.length; idx++) {
|
|
62
|
+
const val = array[idx];
|
|
63
|
+
if ((await iterator(val, idx)) === false)
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 插入到目标位置之前
|
|
70
|
+
* @param {AnyArray} array
|
|
71
|
+
* @param {number} start
|
|
72
|
+
* @param {number} to
|
|
73
|
+
*/
|
|
74
|
+
const arrayInsertBefore = (array, start, to) => {
|
|
75
|
+
if (start === to || start + 1 === to)
|
|
76
|
+
return;
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
78
|
+
const [source] = array.splice(start, 1);
|
|
79
|
+
const insertIndex = to < start ? to : to - 1;
|
|
80
|
+
array.splice(insertIndex, 0, source);
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* 数组删除指定项目
|
|
84
|
+
* @param {V[]} array
|
|
85
|
+
* @param {(val: V, idx: number) => boolean} expect
|
|
86
|
+
* @returns {V[]}
|
|
87
|
+
*/
|
|
88
|
+
function arrayRemove(array, expect) {
|
|
89
|
+
const indexes = [];
|
|
90
|
+
// 这里重命名一下:是为了杜绝 jest 里的 expect 与之产生检查错误
|
|
91
|
+
// eslint 的 jest 语法检查是遇到 expect 函数就以为是单元测试
|
|
92
|
+
const _expect = expect;
|
|
93
|
+
arrayEach(array, (val, idx) => {
|
|
94
|
+
if (_expect(val, idx))
|
|
95
|
+
indexes.push(idx);
|
|
96
|
+
});
|
|
97
|
+
indexes.forEach((val, idx) => array.splice(val - idx, 1));
|
|
98
|
+
return array;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 自定义深度优先遍历函数(支持continue和break操作)
|
|
102
|
+
* @param {array} deepList
|
|
103
|
+
* @param {function} iterator
|
|
104
|
+
* @param {array} children
|
|
105
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
106
|
+
*/
|
|
107
|
+
const deepTraversal = (deepList, iterator, children = 'children', isReverse = false) => {
|
|
108
|
+
let level = 0;
|
|
109
|
+
const walk = (arr, parent) => {
|
|
110
|
+
if (isReverse) {
|
|
111
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
112
|
+
const re = iterator(arr[i], i, deepList, parent, level);
|
|
113
|
+
if (re === 'break') {
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
else if (re === 'continue') {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
if (Array.isArray(arr[i][children])) {
|
|
121
|
+
++level;
|
|
122
|
+
// @ts-ignore
|
|
123
|
+
walk(arr[i][children], arr[i]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
for (let i = 0; i < arr.length; i++) {
|
|
129
|
+
const re = iterator(arr[i], i, deepList, parent, level);
|
|
130
|
+
if (re === 'break') {
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
else if (re === 'continue') {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
// @ts-ignore
|
|
137
|
+
if (Array.isArray(arr[i][children])) {
|
|
138
|
+
++level;
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
walk(arr[i][children], arr[i]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
walk(deepList, null);
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
149
|
+
* @param {ArrayLike<T>} tree
|
|
150
|
+
* @param {IdLike} nodeId
|
|
151
|
+
* @param {ITreeConf} config
|
|
152
|
+
* @return {[IdLike[], ITreeItem<V>[]]}
|
|
153
|
+
*/
|
|
154
|
+
function getTreeIds(tree, nodeId, config) {
|
|
155
|
+
const { children = 'children', id = 'id' } = config || {};
|
|
156
|
+
const toFlatArray = (tree, parentId, parent) => {
|
|
157
|
+
return tree.reduce((t, _) => {
|
|
158
|
+
const child = _[children];
|
|
159
|
+
return [
|
|
160
|
+
...t,
|
|
161
|
+
parentId ? { ..._, parentId, parent } : _,
|
|
162
|
+
...(child && child.length ? toFlatArray(child, _[id], _) : [])
|
|
163
|
+
];
|
|
164
|
+
}, []);
|
|
165
|
+
};
|
|
166
|
+
const getIds = (flatArray) => {
|
|
167
|
+
let child = flatArray.find(_ => _[id] === nodeId);
|
|
168
|
+
const { parent, parentId, ...other } = child;
|
|
169
|
+
let ids = [nodeId], nodes = [other];
|
|
170
|
+
while (child && child.parentId) {
|
|
171
|
+
ids = [child.parentId, ...ids];
|
|
172
|
+
nodes = [child.parent, ...nodes];
|
|
173
|
+
child = flatArray.find(_ => _[id] === child.parentId);
|
|
174
|
+
}
|
|
175
|
+
return [ids, nodes];
|
|
176
|
+
};
|
|
177
|
+
return getIds(toFlatArray(tree));
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 异步ForEach函数
|
|
181
|
+
* @param {array} array
|
|
182
|
+
* @param {asyncFuntion} callback
|
|
183
|
+
* // asyncForEach 使用范例如下
|
|
184
|
+
// const start = async () => {
|
|
185
|
+
// await asyncForEach(result, async (item) => {
|
|
186
|
+
// await request(item);
|
|
187
|
+
// count++;
|
|
188
|
+
// });
|
|
189
|
+
|
|
190
|
+
// console.log('发送次数', count);
|
|
191
|
+
// }
|
|
192
|
+
|
|
193
|
+
// for await...of 使用范例如下
|
|
194
|
+
// const loadImages = async (images) => {
|
|
195
|
+
// for await (const item of images) {
|
|
196
|
+
// await request(item);
|
|
197
|
+
// count++;
|
|
198
|
+
// }
|
|
199
|
+
// }
|
|
200
|
+
* @return {*}
|
|
201
|
+
*/
|
|
202
|
+
async function asyncForEach(array, callback) {
|
|
203
|
+
for (let index = 0, len = array.length; index < len; index++) {
|
|
204
|
+
await callback(array[index], index, array);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, asyncForEach, deepTraversal, getTreeIds };
|
package/lib/es/async.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 等待一段时间
|
|
9
|
+
* @param {number} timeout 等待时间,单位毫秒
|
|
10
|
+
* @returns {Promise<void>}
|
|
11
|
+
*/
|
|
12
|
+
async function wait(timeout = 1) {
|
|
13
|
+
return new Promise(resolve => setTimeout(resolve, timeout));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 异步遍历
|
|
17
|
+
* @ref https://github.com/Kevnz/async-tools/blob/master/src/mapper.js
|
|
18
|
+
* @ref https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator
|
|
19
|
+
* @param {Array<T>} list
|
|
20
|
+
* @param {(val: T, idx: number, list: ArrayLike<T>) => Promise<R>} mapper
|
|
21
|
+
* @param {number} concurrency 并发数量,默认无限
|
|
22
|
+
* @returns {Promise<R[]>}
|
|
23
|
+
*/
|
|
24
|
+
async function asyncMap(list, mapper, concurrency = Infinity) {
|
|
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)
|
|
34
|
+
return reject(rejected);
|
|
35
|
+
const it = iterator.next();
|
|
36
|
+
if (it.done) {
|
|
37
|
+
if (resolvedLength === list.length)
|
|
38
|
+
resolve(resolves);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const current = index++;
|
|
42
|
+
mapper(it.value, current, list)
|
|
43
|
+
.then(value => {
|
|
44
|
+
resolvedLength++;
|
|
45
|
+
resolves[current] = value;
|
|
46
|
+
next();
|
|
47
|
+
})
|
|
48
|
+
.catch(err => {
|
|
49
|
+
rejected = err;
|
|
50
|
+
next();
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
// 开始
|
|
54
|
+
for (let i = 0; i < limit; i++) {
|
|
55
|
+
next();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { asyncMap, wait };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { setStyle } from './dom.js';
|
|
8
|
+
|
|
9
|
+
const textEl = document.createElement('textarea');
|
|
10
|
+
setStyle(textEl, {
|
|
11
|
+
position: 'absolute',
|
|
12
|
+
top: '-9999px',
|
|
13
|
+
left: '-9999px',
|
|
14
|
+
opacity: 0
|
|
15
|
+
});
|
|
16
|
+
document.body.appendChild(textEl);
|
|
17
|
+
/**
|
|
18
|
+
* 复制文本
|
|
19
|
+
* @param {string} text
|
|
20
|
+
*/
|
|
21
|
+
const copyText = (text) => {
|
|
22
|
+
textEl.value = text;
|
|
23
|
+
textEl.focus({ preventScroll: true });
|
|
24
|
+
textEl.select();
|
|
25
|
+
try {
|
|
26
|
+
document.execCommand('copy');
|
|
27
|
+
textEl.blur();
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
// ignore
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { copyText };
|
package/lib/es/cookie.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isNumber, isDate } from './type.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 获取cookie
|
|
11
|
+
* @param {string} name
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
const cookieGet = (name) => {
|
|
15
|
+
const { cookie } = document;
|
|
16
|
+
if (!cookie)
|
|
17
|
+
return '';
|
|
18
|
+
const result = cookie.split(';');
|
|
19
|
+
for (let i = 0; i < result.length; i++) {
|
|
20
|
+
const item = result[i];
|
|
21
|
+
const [key, val = ''] = item.split('=');
|
|
22
|
+
if (key === name)
|
|
23
|
+
return decodeURIComponent(val);
|
|
24
|
+
}
|
|
25
|
+
return '';
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 设置 cookie
|
|
29
|
+
* @param {string} name
|
|
30
|
+
* @param {string} value
|
|
31
|
+
* @param {number | Date} [maxAge]
|
|
32
|
+
*/
|
|
33
|
+
const cookieSet = (name, value, maxAge) => {
|
|
34
|
+
const metas = [];
|
|
35
|
+
const EXPIRES = 'expires';
|
|
36
|
+
metas.push([name, encodeURIComponent(value)]);
|
|
37
|
+
if (isNumber(maxAge)) {
|
|
38
|
+
const d = new Date();
|
|
39
|
+
d.setTime(d.getTime() + maxAge);
|
|
40
|
+
metas.push([EXPIRES, d.toUTCString()]);
|
|
41
|
+
}
|
|
42
|
+
else if (isDate(maxAge)) {
|
|
43
|
+
metas.push([EXPIRES, maxAge.toUTCString()]);
|
|
44
|
+
}
|
|
45
|
+
metas.push(['path', '/']);
|
|
46
|
+
document.cookie = metas
|
|
47
|
+
.map(item => {
|
|
48
|
+
const [key, val] = item;
|
|
49
|
+
return `${key}=${val}`;
|
|
50
|
+
})
|
|
51
|
+
.join(';');
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* 删除单个 cookie
|
|
55
|
+
* @param name cookie 名称
|
|
56
|
+
*/
|
|
57
|
+
const cookieDel = (name) => cookieSet(name, '', -1);
|
|
58
|
+
|
|
59
|
+
export { cookieDel, cookieGet, cookieSet };
|
package/lib/es/date.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v0.0.1
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 格式化为日期对象(带自定义格式化模板)
|
|
9
|
+
* @param {DateValue} value 可以是数值、字符串或 Date 对象
|
|
10
|
+
* @param {string} [format] 模板,默认是 YYYY-MM-DD HH:mm:ss,模板字符:
|
|
11
|
+
* - YYYY:年
|
|
12
|
+
* - yyyy: 年
|
|
13
|
+
* - MM:月
|
|
14
|
+
* - DD:日
|
|
15
|
+
* - dd: 日
|
|
16
|
+
* - HH:时(24 小时制)
|
|
17
|
+
* - hh:时(12 小时制)
|
|
18
|
+
* - mm:分
|
|
19
|
+
* - ss:秒
|
|
20
|
+
* - SSS:毫秒
|
|
21
|
+
* - ww: 周
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
const formatDate = (date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') => {
|
|
25
|
+
let fmt = format;
|
|
26
|
+
let ret;
|
|
27
|
+
const opt = {
|
|
28
|
+
'Y+': `${date.getFullYear()}`,
|
|
29
|
+
'y+': `${date.getFullYear()}`,
|
|
30
|
+
'M+': `${date.getMonth() + 1}`,
|
|
31
|
+
'D+': `${date.getDate()}`,
|
|
32
|
+
'd+': `${date.getDate()}`,
|
|
33
|
+
'H+': `${date.getHours()}`,
|
|
34
|
+
'm+': `${date.getMinutes()}`,
|
|
35
|
+
's+': `${date.getSeconds()}`,
|
|
36
|
+
'S+': `${date.getMilliseconds()}`,
|
|
37
|
+
'w+': ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()] // 周
|
|
38
|
+
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
|
39
|
+
};
|
|
40
|
+
for (const k in opt) {
|
|
41
|
+
ret = new RegExp('(' + k + ')').exec(fmt);
|
|
42
|
+
if (ret) {
|
|
43
|
+
fmt = fmt.replace(ret[1], ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return fmt;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* 计算向前或向后N天的具体日期
|
|
50
|
+
* @param {string} strDate 参考日期
|
|
51
|
+
* @param {number} n 正数:向后推算;负数:向前推算
|
|
52
|
+
* @param {string} sep 日期格式的分隔符
|
|
53
|
+
* @return {*} 目标日期
|
|
54
|
+
*/
|
|
55
|
+
function calculateDate(strDate, n, sep = '-') {
|
|
56
|
+
//strDate 为字符串日期 如:'2019-01-01' n为你要传入的参数,当前为0,前一天为-1,后一天为1
|
|
57
|
+
let dateArr = strDate.split(sep); //这边给定一个特定时间
|
|
58
|
+
let newDate = new Date(+dateArr[0], +dateArr[1] - 1, +dateArr[2]);
|
|
59
|
+
let befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(String(n)); //计算前几天用减,计算后几天用加,最后一个就是多少天的数量
|
|
60
|
+
let beforeDat = new Date();
|
|
61
|
+
beforeDat.setTime(befminuts);
|
|
62
|
+
let befMonth = beforeDat.getMonth() + 1;
|
|
63
|
+
let mon = befMonth >= 10 ? befMonth : '0' + befMonth;
|
|
64
|
+
let befDate = beforeDat.getDate();
|
|
65
|
+
let da = befDate >= 10 ? befDate : '0' + befDate;
|
|
66
|
+
let finalNewDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
|
|
67
|
+
return finalNewDate;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 计算向前或向后N天的具体时间日期
|
|
71
|
+
* @param {number} n 正数:向后推算;负数:向前推算
|
|
72
|
+
* @param {string} dateSep 日期分隔符
|
|
73
|
+
* @param {string} timeSep 时间分隔符
|
|
74
|
+
* @return {*}
|
|
75
|
+
*/
|
|
76
|
+
function calculateDateTime(n, dateSep = '-', timeSep = ':') {
|
|
77
|
+
let date = new Date();
|
|
78
|
+
let separator1 = '-';
|
|
79
|
+
let separator2 = ':';
|
|
80
|
+
let year = date.getFullYear();
|
|
81
|
+
let month = date.getMonth() + 1;
|
|
82
|
+
let strDate = date.getDate() + n;
|
|
83
|
+
if (strDate > new Date(year, month, 0).getDate()) {
|
|
84
|
+
month += 1;
|
|
85
|
+
strDate -= new Date(year, month, 0).getDate();
|
|
86
|
+
if (month > 12) {
|
|
87
|
+
year += 1;
|
|
88
|
+
month = 1;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (month >= 1 && month <= 9) {
|
|
92
|
+
month = '0' + month;
|
|
93
|
+
}
|
|
94
|
+
if (strDate >= 0 && strDate <= 9) {
|
|
95
|
+
strDate = '0' + strDate;
|
|
96
|
+
}
|
|
97
|
+
return (year +
|
|
98
|
+
separator1 +
|
|
99
|
+
month +
|
|
100
|
+
separator1 +
|
|
101
|
+
strDate +
|
|
102
|
+
' ' +
|
|
103
|
+
date.getHours() +
|
|
104
|
+
separator2 +
|
|
105
|
+
date.getMinutes() +
|
|
106
|
+
separator2 +
|
|
107
|
+
date.getSeconds());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { calculateDate, calculateDateTime, formatDate };
|