sculp-js 0.1.1 → 1.0.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.
- package/README.md +3 -0
- package/lib/cjs/array.js +29 -23
- package/lib/cjs/async.js +3 -3
- package/lib/cjs/clipboard.js +3 -3
- package/lib/cjs/cookie.js +5 -5
- package/lib/cjs/date.js +130 -12
- package/lib/cjs/dom.js +13 -11
- package/lib/cjs/download.js +9 -9
- package/lib/cjs/easing.js +1 -1
- package/lib/cjs/file.js +5 -4
- package/lib/cjs/func.js +1 -1
- package/lib/cjs/index.js +5 -1
- package/lib/cjs/number.js +4 -4
- package/lib/cjs/object.js +11 -9
- package/lib/cjs/path.js +1 -1
- package/lib/cjs/qs.js +5 -5
- package/lib/cjs/random.js +1 -1
- package/lib/cjs/string.js +8 -8
- package/lib/cjs/type.js +11 -2
- package/lib/cjs/unique.js +1 -1
- package/lib/cjs/url.js +1 -1
- package/lib/cjs/watermark.js +8 -9
- package/lib/es/array.js +29 -23
- package/lib/es/async.js +3 -3
- package/lib/es/clipboard.js +3 -3
- package/lib/es/cookie.js +5 -5
- package/lib/es/date.js +127 -13
- package/lib/es/dom.js +13 -11
- package/lib/es/download.js +9 -9
- package/lib/es/easing.js +1 -1
- package/lib/es/file.js +5 -4
- package/lib/es/func.js +1 -1
- package/lib/es/index.js +2 -2
- package/lib/es/number.js +4 -4
- package/lib/es/object.js +11 -9
- package/lib/es/path.js +1 -1
- package/lib/es/qs.js +5 -5
- package/lib/es/random.js +1 -1
- package/lib/es/string.js +8 -8
- package/lib/es/type.js +11 -2
- package/lib/es/unique.js +1 -1
- package/lib/es/url.js +1 -1
- package/lib/es/watermark.js +8 -9
- package/lib/index.d.ts +121 -62
- package/lib/umd/index.js +229 -94
- package/package.json +3 -2
package/lib/cjs/object.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -29,20 +29,22 @@ const isPlainObject = (obj) => {
|
|
|
29
29
|
* @param {string} key
|
|
30
30
|
* @returns {boolean}
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
function objectHas(obj, key) {
|
|
33
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
34
|
+
}
|
|
33
35
|
/**
|
|
34
36
|
* 遍历对象,返回 false 中断遍历
|
|
35
37
|
* @param {O} obj
|
|
36
38
|
* @param {(val: O[keyof O], key: keyof O) => (boolean | void)} iterator
|
|
37
39
|
*/
|
|
38
|
-
|
|
40
|
+
function objectEach(obj, iterator) {
|
|
39
41
|
for (const key in obj) {
|
|
40
42
|
if (!objectHas(obj, key))
|
|
41
43
|
continue;
|
|
42
44
|
if (iterator(obj[key], key) === false)
|
|
43
45
|
break;
|
|
44
46
|
}
|
|
45
|
-
}
|
|
47
|
+
}
|
|
46
48
|
/**
|
|
47
49
|
* 异步遍历对象,返回 false 中断遍历
|
|
48
50
|
* @param {O} obj
|
|
@@ -148,7 +150,7 @@ const merge = (map, source, target) => {
|
|
|
148
150
|
* @param {ObjectAssignItem | undefined} targets
|
|
149
151
|
* @returns {R}
|
|
150
152
|
*/
|
|
151
|
-
|
|
153
|
+
function objectAssign(source, ...targets) {
|
|
152
154
|
const map = new Map();
|
|
153
155
|
for (let i = 0; i < targets.length; i++) {
|
|
154
156
|
const target = targets[i];
|
|
@@ -158,7 +160,7 @@ const objectAssign = (source, ...targets) => {
|
|
|
158
160
|
}
|
|
159
161
|
map.clear();
|
|
160
162
|
return source;
|
|
161
|
-
}
|
|
163
|
+
}
|
|
162
164
|
/**
|
|
163
165
|
* 对象填充
|
|
164
166
|
* @param {Partial<R>} source
|
|
@@ -166,7 +168,7 @@ const objectAssign = (source, ...targets) => {
|
|
|
166
168
|
* @param {(s: Partial<R>, t: Partial<R>, key: keyof R) => boolean} fillable
|
|
167
169
|
* @returns {R}
|
|
168
170
|
*/
|
|
169
|
-
|
|
171
|
+
function objectFill(source, target, fillable) {
|
|
170
172
|
const _fillable = fillable || ((source, target, key) => source[key] === undefined);
|
|
171
173
|
objectEach(target, (val, key) => {
|
|
172
174
|
if (_fillable(source, target, key)) {
|
|
@@ -174,7 +176,7 @@ const objectFill = (source, target, fillable) => {
|
|
|
174
176
|
}
|
|
175
177
|
});
|
|
176
178
|
return source;
|
|
177
|
-
}
|
|
179
|
+
}
|
|
178
180
|
function objectGet(obj, path, strict = false) {
|
|
179
181
|
path = path.replace(/\[(\w+)\]/g, '.$1');
|
|
180
182
|
path = path.replace(/^\./, '');
|
|
@@ -207,7 +209,7 @@ function objectGet(obj, path, strict = false) {
|
|
|
207
209
|
* 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
|
|
208
210
|
* @param {AnyObject | AnyArray} obj
|
|
209
211
|
* @param {WeakMap} map
|
|
210
|
-
* @
|
|
212
|
+
* @returns {AnyObject | AnyArray}
|
|
211
213
|
*/
|
|
212
214
|
function cloneDeep(obj, map = new WeakMap()) {
|
|
213
215
|
if (obj instanceof Date)
|
package/lib/cjs/path.js
CHANGED
package/lib/cjs/qs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -14,7 +14,7 @@ var type = require('./type.js');
|
|
|
14
14
|
* @param {string} queryString
|
|
15
15
|
* @returns {Params}
|
|
16
16
|
*/
|
|
17
|
-
|
|
17
|
+
function qsParse(queryString) {
|
|
18
18
|
const params = new URLSearchParams(queryString);
|
|
19
19
|
const result = {};
|
|
20
20
|
for (const [key, val] of params.entries()) {
|
|
@@ -28,7 +28,7 @@ const qsParse = (queryString) => {
|
|
|
28
28
|
result[key] = params.getAll(key);
|
|
29
29
|
}
|
|
30
30
|
return result;
|
|
31
|
-
}
|
|
31
|
+
}
|
|
32
32
|
const defaultReplacer = (val) => {
|
|
33
33
|
if (type.isString(val))
|
|
34
34
|
return val;
|
|
@@ -46,7 +46,7 @@ const defaultReplacer = (val) => {
|
|
|
46
46
|
* @param {Replacer} replacer
|
|
47
47
|
* @returns {string}
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
function qsStringify(query, replacer = defaultReplacer) {
|
|
50
50
|
const params = new URLSearchParams();
|
|
51
51
|
object.objectEach(query, (val, key) => {
|
|
52
52
|
if (type.isArray(val)) {
|
|
@@ -65,7 +65,7 @@ const qsStringify = (query, replacer = defaultReplacer) => {
|
|
|
65
65
|
}
|
|
66
66
|
});
|
|
67
67
|
return params.toString();
|
|
68
|
-
}
|
|
68
|
+
}
|
|
69
69
|
|
|
70
70
|
exports.qsParse = qsParse;
|
|
71
71
|
exports.qsStringify = qsStringify;
|
package/lib/cjs/random.js
CHANGED
package/lib/cjs/string.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -14,24 +14,24 @@ var type = require('./type.js');
|
|
|
14
14
|
* @param {boolean} [bigger] 是否大写第一个字母
|
|
15
15
|
* @returns {string}
|
|
16
16
|
*/
|
|
17
|
-
|
|
17
|
+
function stringCamelCase(string, bigger) {
|
|
18
18
|
let string2 = string;
|
|
19
19
|
if (bigger) {
|
|
20
20
|
string2 = string.replace(/^./, origin => origin.toUpperCase());
|
|
21
21
|
}
|
|
22
22
|
const HUMP_RE = /[\s_-](.)/g;
|
|
23
23
|
return string2.replace(HUMP_RE, (orign, char) => char.toUpperCase());
|
|
24
|
-
}
|
|
24
|
+
}
|
|
25
25
|
/**
|
|
26
26
|
* 将字符串转换为连字格式
|
|
27
27
|
* @param {string} string
|
|
28
28
|
* @param {string} [separator] 分隔符,默认是"-"(短横线)
|
|
29
29
|
* @returns {string}
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
function stringKebabCase(string, separator = '-') {
|
|
32
32
|
const string2 = string.replace(/^./, origin => origin.toLowerCase());
|
|
33
33
|
return string2.replace(/[A-Z]/g, origin => `${separator}${origin.toLowerCase()}`);
|
|
34
|
-
}
|
|
34
|
+
}
|
|
35
35
|
const STRING_ARABIC_NUMERALS = '0123456789';
|
|
36
36
|
const STRING_LOWERCASE_ALPHA = 'abcdefghijklmnopqrstuvwxyz';
|
|
37
37
|
const STRING_UPPERCASE_ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
@@ -47,7 +47,7 @@ const placeholderRE = /%[%sdo]/g;
|
|
|
47
47
|
* @param args
|
|
48
48
|
* @returns {string}
|
|
49
49
|
*/
|
|
50
|
-
|
|
50
|
+
function stringFormat(string, ...args) {
|
|
51
51
|
let index = 0;
|
|
52
52
|
const result = string.replace(placeholderRE, (origin) => {
|
|
53
53
|
const arg = args[index++];
|
|
@@ -65,7 +65,7 @@ const stringFormat = (string, ...args) => {
|
|
|
65
65
|
}
|
|
66
66
|
});
|
|
67
67
|
return [result, ...args.splice(index).map(String)].join(' ');
|
|
68
|
-
}
|
|
68
|
+
}
|
|
69
69
|
const ev = (expression, data) => {
|
|
70
70
|
try {
|
|
71
71
|
// eslint-disable-next-line @typescript-eslint/no-implied-eval,@typescript-eslint/no-unsafe-return
|
|
@@ -129,7 +129,7 @@ const stringFill = (length, value = ' ') => new Array(length).fill(value).join('
|
|
|
129
129
|
* @param {string} str 目标字符串
|
|
130
130
|
* @param {number} fontSize 字符串字体大小
|
|
131
131
|
* @param {boolean} isRemoveDom 计算后是否移除中间dom元素
|
|
132
|
-
* @
|
|
132
|
+
* @returns {*}
|
|
133
133
|
*/
|
|
134
134
|
function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
|
|
135
135
|
let strWidth = 0;
|
package/lib/cjs/type.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
|
|
9
9
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* 判断任意值的数据类型
|
|
13
|
+
* @param {unknown} any
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
11
16
|
const typeIs = (any) => Object.prototype.toString.call(any).slice(8, -1);
|
|
12
17
|
// 基本数据类型判断
|
|
13
18
|
const isString = (any) => typeof any === 'string';
|
|
@@ -21,7 +26,11 @@ const isPrimitive = (any) => any === null || typeof any !== 'object';
|
|
|
21
26
|
// 复合数据类型判断
|
|
22
27
|
const isObject = (any) => typeIs(any) === 'Object';
|
|
23
28
|
const isArray = (any) => Array.isArray(any);
|
|
24
|
-
|
|
29
|
+
/**
|
|
30
|
+
* 判断是否为函数
|
|
31
|
+
* @param {unknown} any
|
|
32
|
+
* @returns {boolean}
|
|
33
|
+
*/
|
|
25
34
|
const isFunction = (any) => typeof any === 'function';
|
|
26
35
|
// 对象类型判断
|
|
27
36
|
const isNaN = (any) => Number.isNaN(any);
|
package/lib/cjs/unique.js
CHANGED
package/lib/cjs/url.js
CHANGED
package/lib/cjs/watermark.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -13,17 +13,18 @@
|
|
|
13
13
|
* @desc 网页加水印的工具类
|
|
14
14
|
*/
|
|
15
15
|
/**
|
|
16
|
-
* canvas 实现
|
|
16
|
+
* canvas 实现 水印, 具备防删除功能
|
|
17
17
|
* @param {ICanvasWM} canvasWM
|
|
18
|
+
* @example genCanvasWM({ content: 'QQMusicFE' })
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
|
+
function genCanvasWM(canvasWM) {
|
|
20
21
|
const { container = document.body, width = '300px', height = '200px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
|
|
21
22
|
// fontWeight = 500,
|
|
22
23
|
fillStyle = 'rgba(189, 177, 167, .3)', content = '请勿外传', rotate = 30, zIndex = 2147483647 } = canvasWM;
|
|
23
24
|
// 仅限主页面添加水印
|
|
24
|
-
if (!location.pathname.includes('/home')) {
|
|
25
|
-
|
|
26
|
-
}
|
|
25
|
+
// if (!location.pathname.includes('/home')) {
|
|
26
|
+
// return;
|
|
27
|
+
// }
|
|
27
28
|
const args = canvasWM;
|
|
28
29
|
const canvas = document.createElement('canvas');
|
|
29
30
|
canvas.setAttribute('width', width);
|
|
@@ -84,8 +85,6 @@ const genCanvasWM = (canvasWM) => {
|
|
|
84
85
|
});
|
|
85
86
|
mo.observe(container, { attributes: true, subtree: true, childList: true });
|
|
86
87
|
}
|
|
87
|
-
}
|
|
88
|
-
// 调用
|
|
89
|
-
// __canvasWM({ content: 'QQMusicFE' })
|
|
88
|
+
}
|
|
90
89
|
|
|
91
90
|
exports.genCanvasWM = genCanvasWM;
|
package/lib/es/array.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -9,10 +9,11 @@ import { isArray, isString, isObject } from './type.js';
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* 判断一个对象是否为类数组
|
|
12
|
+
*
|
|
12
13
|
* @param any
|
|
13
14
|
* @returns {boolean}
|
|
14
15
|
*/
|
|
15
|
-
|
|
16
|
+
function arrayLike(any) {
|
|
16
17
|
if (isArray(any))
|
|
17
18
|
return true;
|
|
18
19
|
if (isString(any))
|
|
@@ -20,14 +21,16 @@ const arrayLike = (any) => {
|
|
|
20
21
|
if (!isObject(any))
|
|
21
22
|
return false;
|
|
22
23
|
return objectHas(any, 'length');
|
|
23
|
-
}
|
|
24
|
+
}
|
|
24
25
|
/**
|
|
25
26
|
* 遍历数组,返回 false 中断遍历
|
|
27
|
+
*
|
|
26
28
|
* @param {ArrayLike<V>} array
|
|
27
29
|
* @param {(val: V, idx: number) => any} iterator
|
|
28
30
|
* @param reverse {boolean} 是否倒序
|
|
31
|
+
* @returns {*}
|
|
29
32
|
*/
|
|
30
|
-
|
|
33
|
+
function arrayEach(array, iterator, reverse = false) {
|
|
31
34
|
if (reverse) {
|
|
32
35
|
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
33
36
|
const val = array[idx];
|
|
@@ -42,12 +45,12 @@ const arrayEach = (array, iterator, reverse = false) => {
|
|
|
42
45
|
break;
|
|
43
46
|
}
|
|
44
47
|
}
|
|
45
|
-
}
|
|
48
|
+
}
|
|
46
49
|
/**
|
|
47
50
|
* 异步遍历数组,返回 false 中断遍历
|
|
48
|
-
* @param {ArrayLike<V>} array
|
|
49
|
-
* @param {(val: V, idx: number) => Promise<any>} iterator
|
|
50
|
-
* @param {boolean} reverse
|
|
51
|
+
* @param {ArrayLike<V>} array 数组
|
|
52
|
+
* @param {(val: V, idx: number) => Promise<any>} iterator 支持Promise类型的回调函数
|
|
53
|
+
* @param {boolean} reverse 是否反向遍历
|
|
51
54
|
*/
|
|
52
55
|
async function arrayEachAsync(array, iterator, reverse = false) {
|
|
53
56
|
if (reverse) {
|
|
@@ -70,15 +73,16 @@ async function arrayEachAsync(array, iterator, reverse = false) {
|
|
|
70
73
|
* @param {AnyArray} array
|
|
71
74
|
* @param {number} start
|
|
72
75
|
* @param {number} to
|
|
76
|
+
* @returns {*}
|
|
73
77
|
*/
|
|
74
|
-
|
|
78
|
+
function arrayInsertBefore(array, start, to) {
|
|
75
79
|
if (start === to || start + 1 === to)
|
|
76
80
|
return;
|
|
77
81
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
78
82
|
const [source] = array.splice(start, 1);
|
|
79
83
|
const insertIndex = to < start ? to : to - 1;
|
|
80
84
|
array.splice(insertIndex, 0, source);
|
|
81
|
-
}
|
|
85
|
+
}
|
|
82
86
|
/**
|
|
83
87
|
* 数组删除指定项目
|
|
84
88
|
* @param {V[]} array
|
|
@@ -99,17 +103,18 @@ function arrayRemove(array, expect) {
|
|
|
99
103
|
}
|
|
100
104
|
/**
|
|
101
105
|
* 自定义深度优先遍历函数(支持continue和break操作)
|
|
102
|
-
* @param {
|
|
103
|
-
* @param {
|
|
104
|
-
* @param {
|
|
105
|
-
* @param {boolean} isReverse
|
|
106
|
+
* @param {ArrayLike<V>} tree 树形数据
|
|
107
|
+
* @param {Function} iterator 迭代函数
|
|
108
|
+
* @param {string} children 定制子元素的key
|
|
109
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
110
|
+
* @returns {*}
|
|
106
111
|
*/
|
|
107
|
-
|
|
112
|
+
function deepTraversal(tree, iterator, children = 'children', isReverse = false) {
|
|
108
113
|
let level = 0;
|
|
109
114
|
const walk = (arr, parent) => {
|
|
110
115
|
if (isReverse) {
|
|
111
116
|
for (let i = arr.length - 1; i >= 0; i--) {
|
|
112
|
-
const re = iterator(arr[i], i,
|
|
117
|
+
const re = iterator(arr[i], i, tree, parent, level);
|
|
113
118
|
if (re === 'break') {
|
|
114
119
|
break;
|
|
115
120
|
}
|
|
@@ -126,7 +131,7 @@ const deepTraversal = (deepList, iterator, children = 'children', isReverse = fa
|
|
|
126
131
|
}
|
|
127
132
|
else {
|
|
128
133
|
for (let i = 0; i < arr.length; i++) {
|
|
129
|
-
const re = iterator(arr[i], i,
|
|
134
|
+
const re = iterator(arr[i], i, tree, parent, level);
|
|
130
135
|
if (re === 'break') {
|
|
131
136
|
break;
|
|
132
137
|
}
|
|
@@ -142,14 +147,15 @@ const deepTraversal = (deepList, iterator, children = 'children', isReverse = fa
|
|
|
142
147
|
}
|
|
143
148
|
}
|
|
144
149
|
};
|
|
145
|
-
walk(
|
|
146
|
-
}
|
|
150
|
+
walk(tree, null);
|
|
151
|
+
}
|
|
147
152
|
/**
|
|
148
153
|
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
149
|
-
*
|
|
150
|
-
* @param {
|
|
151
|
-
* @param {
|
|
152
|
-
* @
|
|
154
|
+
*
|
|
155
|
+
* @param {ArrayLike<T>} tree - 树形数据
|
|
156
|
+
* @param {IdLike} nodeId - 元素ID
|
|
157
|
+
* @param {ITreeConf} config - 迭代配置项
|
|
158
|
+
* @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
|
|
153
159
|
*/
|
|
154
160
|
function getTreeIds(tree, nodeId, config) {
|
|
155
161
|
const { children = 'children', id = 'id' } = config || {};
|
package/lib/es/async.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param {number} timeout 等待时间,单位毫秒
|
|
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
|
/**
|
|
@@ -21,7 +21,7 @@ async function wait(timeout = 1) {
|
|
|
21
21
|
* @param {number} concurrency 并发数量,默认无限
|
|
22
22
|
* @returns {Promise<R[]>}
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
function asyncMap(list, mapper, concurrency = Infinity) {
|
|
25
25
|
return new Promise((resolve, reject) => {
|
|
26
26
|
const iterator = list[Symbol.iterator]();
|
|
27
27
|
const limit = Math.min(list.length, concurrency);
|
package/lib/es/clipboard.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -18,7 +18,7 @@ document.body.appendChild(textEl);
|
|
|
18
18
|
* 复制文本
|
|
19
19
|
* @param {string} text
|
|
20
20
|
*/
|
|
21
|
-
|
|
21
|
+
function copyText(text) {
|
|
22
22
|
textEl.value = text;
|
|
23
23
|
textEl.focus({ preventScroll: true });
|
|
24
24
|
textEl.select();
|
|
@@ -29,6 +29,6 @@ const copyText = (text) => {
|
|
|
29
29
|
catch (err) {
|
|
30
30
|
// ignore
|
|
31
31
|
}
|
|
32
|
-
}
|
|
32
|
+
}
|
|
33
33
|
|
|
34
34
|
export { copyText };
|
package/lib/es/cookie.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -11,7 +11,7 @@ import { isNumber, isDate } from './type.js';
|
|
|
11
11
|
* @param {string} name
|
|
12
12
|
* @returns {string}
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
function cookieGet(name) {
|
|
15
15
|
const { cookie } = document;
|
|
16
16
|
if (!cookie)
|
|
17
17
|
return '';
|
|
@@ -23,14 +23,14 @@ const cookieGet = (name) => {
|
|
|
23
23
|
return decodeURIComponent(val);
|
|
24
24
|
}
|
|
25
25
|
return '';
|
|
26
|
-
}
|
|
26
|
+
}
|
|
27
27
|
/**
|
|
28
28
|
* 设置 cookie
|
|
29
29
|
* @param {string} name
|
|
30
30
|
* @param {string} value
|
|
31
31
|
* @param {number | Date} [maxAge]
|
|
32
32
|
*/
|
|
33
|
-
|
|
33
|
+
function cookieSet(name, value, maxAge) {
|
|
34
34
|
const metas = [];
|
|
35
35
|
const EXPIRES = 'expires';
|
|
36
36
|
metas.push([name, encodeURIComponent(value)]);
|
|
@@ -49,7 +49,7 @@ const cookieSet = (name, value, maxAge) => {
|
|
|
49
49
|
return `${key}=${val}`;
|
|
50
50
|
})
|
|
51
51
|
.join(';');
|
|
52
|
-
}
|
|
52
|
+
}
|
|
53
53
|
/**
|
|
54
54
|
* 删除单个 cookie
|
|
55
55
|
* @param name cookie 名称
|
package/lib/es/date.js
CHANGED
|
@@ -1,9 +1,64 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
3
|
* (c) 2023-2023 chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { isDate, isNaN, isString } from './type.js';
|
|
8
|
+
|
|
9
|
+
const isValidDate = (any) => isDate(any) && !isNaN(any.getTime());
|
|
10
|
+
/* istanbul ignore next */
|
|
11
|
+
const guessDateSeparator = (value) => {
|
|
12
|
+
if (!isString(value))
|
|
13
|
+
return;
|
|
14
|
+
const value2 = value.replace(/-/g, '/');
|
|
15
|
+
return new Date(value2);
|
|
16
|
+
};
|
|
17
|
+
/* istanbul ignore next */
|
|
18
|
+
const guessDateTimezone = (value) => {
|
|
19
|
+
if (!isString(value))
|
|
20
|
+
return;
|
|
21
|
+
const re = /([+-])(\d\d)(\d\d)$/;
|
|
22
|
+
const matches = re.exec(value);
|
|
23
|
+
if (!matches)
|
|
24
|
+
return;
|
|
25
|
+
const value2 = value.replace(re, 'Z');
|
|
26
|
+
const d = new Date(value2);
|
|
27
|
+
if (!isValidDate(d))
|
|
28
|
+
return;
|
|
29
|
+
const [, flag, hours, minutes] = matches;
|
|
30
|
+
const hours2 = parseInt(hours, 10);
|
|
31
|
+
const minutes2 = parseInt(minutes, 10);
|
|
32
|
+
const offset = (a, b) => (flag === '+' ? a - b : a + b);
|
|
33
|
+
d.setHours(offset(d.getHours(), hours2));
|
|
34
|
+
d.setMinutes(offset(d.getMinutes(), minutes2));
|
|
35
|
+
return d;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* 解析为Date对象
|
|
39
|
+
* @param {DateValue} value - 可以是数值、字符串或 Date 对象
|
|
40
|
+
* @returns {Date} - 转换后的目标Date
|
|
41
|
+
*/
|
|
42
|
+
function dateParse(value) {
|
|
43
|
+
const d1 = new Date(value);
|
|
44
|
+
if (isValidDate(d1))
|
|
45
|
+
return d1;
|
|
46
|
+
// safari 浏览器的日期解析有问题
|
|
47
|
+
// new Date('2020-06-26 18:06:15') 返回值是一个非法日期对象
|
|
48
|
+
/* istanbul ignore next */
|
|
49
|
+
const d2 = guessDateSeparator(value);
|
|
50
|
+
/* istanbul ignore next */
|
|
51
|
+
if (isValidDate(d2))
|
|
52
|
+
return d2;
|
|
53
|
+
// safari 浏览器的日期解析有问题
|
|
54
|
+
// new Date('2020-06-26T18:06:15.000+0800') 返回值是一个非法日期对象
|
|
55
|
+
/* istanbul ignore next */
|
|
56
|
+
const d3 = guessDateTimezone(value);
|
|
57
|
+
/* istanbul ignore next */
|
|
58
|
+
if (isValidDate(d3))
|
|
59
|
+
return d3;
|
|
60
|
+
throw new SyntaxError(`${value.toString()} 不是一个合法的日期描述`);
|
|
61
|
+
}
|
|
7
62
|
/**
|
|
8
63
|
* 格式化为日期对象(带自定义格式化模板)
|
|
9
64
|
* @param {DateValue} value 可以是数值、字符串或 Date 对象
|
|
@@ -18,10 +73,69 @@
|
|
|
18
73
|
* - mm:分
|
|
19
74
|
* - ss:秒
|
|
20
75
|
* - SSS:毫秒
|
|
21
|
-
* - ww: 周
|
|
22
76
|
* @returns {string}
|
|
23
77
|
*/
|
|
24
|
-
const
|
|
78
|
+
// export const dateStringify = (value: DateValue, format = 'YYYY-MM-DD HH:mm:ss'): string => {
|
|
79
|
+
// const date = dateParse(value);
|
|
80
|
+
// let fmt = format;
|
|
81
|
+
// let ret;
|
|
82
|
+
// const opt: DateObj = {
|
|
83
|
+
// 'Y+': `${date.getFullYear()}`, // 年
|
|
84
|
+
// 'y+': `${date.getFullYear()}`, // 年
|
|
85
|
+
// 'M+': `${date.getMonth() + 1}`, // 月
|
|
86
|
+
// 'D+': `${date.getDate()}`, // 日
|
|
87
|
+
// 'd+': `${date.getDate()}`, // 日
|
|
88
|
+
// 'H+': `${date.getHours()}`, // 时
|
|
89
|
+
// 'm+': `${date.getMinutes()}`, // 分
|
|
90
|
+
// 's+': `${date.getSeconds()}`, // 秒
|
|
91
|
+
// 'S+': `${date.getMilliseconds()}` // 豪秒
|
|
92
|
+
// };
|
|
93
|
+
// for (const k in opt) {
|
|
94
|
+
// ret = new RegExp(`(${k})`).exec(fmt);
|
|
95
|
+
// if (ret) {
|
|
96
|
+
// fmt = fmt.replace(ret[1], ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'));
|
|
97
|
+
// }
|
|
98
|
+
// }
|
|
99
|
+
// return fmt;
|
|
100
|
+
// };
|
|
101
|
+
/**
|
|
102
|
+
* 将日期转换为一天的开始时间,即0点0分0秒0毫秒
|
|
103
|
+
* @param {DateValue} value
|
|
104
|
+
* @returns {Date}
|
|
105
|
+
*/
|
|
106
|
+
function dateToStart(value) {
|
|
107
|
+
const d = dateParse(value);
|
|
108
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 将日期转换为一天的结束时间,即23点59分59秒999毫秒
|
|
112
|
+
* @param {DateValue} value
|
|
113
|
+
* @returns {Date}
|
|
114
|
+
*/
|
|
115
|
+
function dateToEnd(value) {
|
|
116
|
+
const d = dateToStart(value);
|
|
117
|
+
d.setDate(d.getDate() + 1);
|
|
118
|
+
return dateParse(d.getTime() - 1);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 格式化为日期对象(带自定义格式化模板)
|
|
122
|
+
* @param {Date} value - 可以是数值、字符串或 Date 对象
|
|
123
|
+
* @param {string} [format] - 模板,默认是 YYYY-MM-DD HH:mm:ss,模板字符:
|
|
124
|
+
* - YYYY:年
|
|
125
|
+
* - yyyy: 年
|
|
126
|
+
* - MM:月
|
|
127
|
+
* - DD:日
|
|
128
|
+
* - dd: 日
|
|
129
|
+
* - HH:时(24 小时制)
|
|
130
|
+
* - hh:时(12 小时制)
|
|
131
|
+
* - mm:分
|
|
132
|
+
* - ss:秒
|
|
133
|
+
* - SSS:毫秒
|
|
134
|
+
* - ww: 周
|
|
135
|
+
* @returns {string} 格式化后的日期字符串
|
|
136
|
+
*/
|
|
137
|
+
function formatDate(value, format = 'YYYY-MM-DD HH:mm:ss') {
|
|
138
|
+
const date = dateParse(value);
|
|
25
139
|
let fmt = format;
|
|
26
140
|
let ret;
|
|
27
141
|
const opt = {
|
|
@@ -44,13 +158,13 @@ const formatDate = (date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') => {
|
|
|
44
158
|
}
|
|
45
159
|
}
|
|
46
160
|
return fmt;
|
|
47
|
-
}
|
|
161
|
+
}
|
|
48
162
|
/**
|
|
49
163
|
* 计算向前或向后N天的具体日期
|
|
50
|
-
* @param {string} strDate 参考日期
|
|
51
|
-
* @param {number} n 正数:向后推算;负数:向前推算
|
|
52
|
-
* @param {string} sep 日期格式的分隔符
|
|
53
|
-
* @
|
|
164
|
+
* @param {string} strDate - 参考日期
|
|
165
|
+
* @param {number} n - 正数:向后推算;负数:向前推算
|
|
166
|
+
* @param {string} sep - 日期格式的分隔符
|
|
167
|
+
* @returns {string} 计算后的目标日期
|
|
54
168
|
*/
|
|
55
169
|
function calculateDate(strDate, n, sep = '-') {
|
|
56
170
|
//strDate 为字符串日期 如:'2019-01-01' n为你要传入的参数,当前为0,前一天为-1,后一天为1
|
|
@@ -68,10 +182,10 @@ function calculateDate(strDate, n, sep = '-') {
|
|
|
68
182
|
}
|
|
69
183
|
/**
|
|
70
184
|
* 计算向前或向后N天的具体时间日期
|
|
71
|
-
* @param {number} n 正数:向后推算;负数:向前推算
|
|
72
|
-
* @param {string} dateSep 日期分隔符
|
|
73
|
-
* @param {string} timeSep 时间分隔符
|
|
74
|
-
* @
|
|
185
|
+
* @param {number} n - 正数:向后推算;负数:向前推算
|
|
186
|
+
* @param {string} dateSep - 日期分隔符
|
|
187
|
+
* @param {string} timeSep - 时间分隔符
|
|
188
|
+
* @returns {string} 转换后的目标日期时间
|
|
75
189
|
*/
|
|
76
190
|
function calculateDateTime(n, dateSep = '-', timeSep = ':') {
|
|
77
191
|
const date = new Date();
|
|
@@ -107,4 +221,4 @@ function calculateDateTime(n, dateSep = '-', timeSep = ':') {
|
|
|
107
221
|
date.getSeconds());
|
|
108
222
|
}
|
|
109
223
|
|
|
110
|
-
export { calculateDate, calculateDateTime, formatDate };
|
|
224
|
+
export { calculateDate, calculateDateTime, dateParse, dateToEnd, dateToStart, formatDate, isValidDate };
|