sculp-js 0.0.2 → 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/LICENSE.md +1 -1
- package/README.md +22 -1
- package/lib/cjs/array.js +32 -55
- 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 +142 -24
- package/lib/cjs/dom.js +24 -10
- 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 +160 -0
- package/lib/cjs/index.js +28 -2
- package/lib/cjs/number.js +82 -0
- package/lib/cjs/object.js +13 -11
- package/lib/cjs/path.js +1 -1
- package/lib/cjs/qs.js +5 -5
- package/lib/cjs/random.js +72 -0
- package/lib/cjs/string.js +40 -7
- package/lib/cjs/type.js +12 -2
- package/lib/cjs/unique.js +83 -0
- package/lib/cjs/url.js +1 -1
- package/lib/cjs/watermark.js +8 -9
- package/lib/es/array.js +33 -55
- 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 +139 -25
- package/lib/es/dom.js +24 -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 +154 -0
- package/lib/es/index.js +10 -6
- package/lib/es/number.js +77 -0
- package/lib/es/object.js +12 -10
- package/lib/es/path.js +1 -1
- package/lib/es/qs.js +5 -5
- package/lib/es/random.js +67 -0
- package/lib/es/string.js +40 -8
- package/lib/es/type.js +12 -3
- package/lib/es/unique.js +79 -0
- package/lib/es/url.js +1 -1
- package/lib/es/watermark.js +8 -9
- package/lib/index.d.ts +254 -80
- package/lib/umd/index.js +637 -132
- package/package.json +36 -12
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v1.0.0
|
|
3
|
+
* (c) 2023-2023 chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var number = require('./number.js');
|
|
10
|
+
var random = require('./random.js');
|
|
11
|
+
var type = require('./type.js');
|
|
12
|
+
|
|
13
|
+
const padStartWithZero = (str, maxLength = 2) => String(str).padStart(maxLength, '0');
|
|
14
|
+
let safeNo = 0;
|
|
15
|
+
let lastTimestamp = 0;
|
|
16
|
+
// 安全后缀长度,按 1 毫秒运算 99999 次 JS 代码来进行估算
|
|
17
|
+
// 那么,补足长度为 5
|
|
18
|
+
// 时间戳模式长度为 13
|
|
19
|
+
// 取最长 5 + 13 = 18
|
|
20
|
+
const UNIQUE_NUMBER_SAFE_LENGTH = 18;
|
|
21
|
+
const FIX_SAFE_LENGTH = 5;
|
|
22
|
+
const TIMESTAMP_LENGTH = 13;
|
|
23
|
+
/**
|
|
24
|
+
* 生成唯一不重复数值
|
|
25
|
+
* @param {number} length
|
|
26
|
+
* @returns {string}
|
|
27
|
+
*/
|
|
28
|
+
const uniqueNumber = (length = UNIQUE_NUMBER_SAFE_LENGTH) => {
|
|
29
|
+
const now = Date.now();
|
|
30
|
+
length = Math.max(length, UNIQUE_NUMBER_SAFE_LENGTH);
|
|
31
|
+
if (now !== lastTimestamp) {
|
|
32
|
+
lastTimestamp = now;
|
|
33
|
+
safeNo = 0;
|
|
34
|
+
}
|
|
35
|
+
const timestamp = `${now}`;
|
|
36
|
+
let random$1 = '';
|
|
37
|
+
const rndLength = length - FIX_SAFE_LENGTH - TIMESTAMP_LENGTH;
|
|
38
|
+
if (rndLength > 0) {
|
|
39
|
+
const rndMin = 10 ** (rndLength - 1);
|
|
40
|
+
const rndMax = 10 ** rndLength - 1;
|
|
41
|
+
const rnd = random.randomNumber(rndMin, rndMax);
|
|
42
|
+
random$1 = `${rnd}`;
|
|
43
|
+
}
|
|
44
|
+
const safe = padStartWithZero(safeNo, FIX_SAFE_LENGTH);
|
|
45
|
+
safeNo++;
|
|
46
|
+
return `${timestamp}${random$1}${safe}`;
|
|
47
|
+
};
|
|
48
|
+
const randomFromPool = (pool) => {
|
|
49
|
+
const poolIndex = random.randomNumber(0, pool.length - 1);
|
|
50
|
+
return pool[poolIndex];
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* 生成唯一不重复字符串
|
|
54
|
+
* @param {number | string} length
|
|
55
|
+
* @param {string} pool
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
const uniqueString = (length, pool) => {
|
|
59
|
+
let _length = 0;
|
|
60
|
+
let _pool = number.HEX_POOL;
|
|
61
|
+
if (type.isString(pool)) {
|
|
62
|
+
_length = length;
|
|
63
|
+
_pool = pool;
|
|
64
|
+
}
|
|
65
|
+
else if (type.isNumber(length)) {
|
|
66
|
+
_length = length;
|
|
67
|
+
}
|
|
68
|
+
else if (type.isString(length)) {
|
|
69
|
+
_pool = length;
|
|
70
|
+
}
|
|
71
|
+
let uniqueString = number.numberToHex(uniqueNumber(), _pool);
|
|
72
|
+
let insertLength = _length - uniqueString.length;
|
|
73
|
+
if (insertLength <= 0)
|
|
74
|
+
return uniqueString;
|
|
75
|
+
while (insertLength--) {
|
|
76
|
+
uniqueString += randomFromPool(_pool);
|
|
77
|
+
}
|
|
78
|
+
return uniqueString;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.UNIQUE_NUMBER_SAFE_LENGTH = UNIQUE_NUMBER_SAFE_LENGTH;
|
|
82
|
+
exports.uniqueNumber = uniqueNumber;
|
|
83
|
+
exports.uniqueString = uniqueString;
|
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,34 +21,36 @@ 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];
|
|
34
|
-
if (iterator(val, idx) === false)
|
|
37
|
+
if (iterator(val, idx, array) === false)
|
|
35
38
|
break;
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
else {
|
|
39
42
|
for (let idx = 0; idx < array.length; idx++) {
|
|
40
43
|
const val = array[idx];
|
|
41
|
-
if (iterator(val, idx) === false)
|
|
44
|
+
if (iterator(val, idx, array) === 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 || {};
|
|
@@ -170,39 +176,11 @@ function getTreeIds(tree, nodeId, config) {
|
|
|
170
176
|
while (child && child.parentId) {
|
|
171
177
|
ids = [child.parentId, ...ids];
|
|
172
178
|
nodes = [child.parent, ...nodes];
|
|
173
|
-
child = flatArray.find(_ => _[id] === child.parentId);
|
|
179
|
+
child = flatArray.find(_ => _[id] === child.parentId); // eslint-disable-line
|
|
174
180
|
}
|
|
175
181
|
return [ids, nodes];
|
|
176
182
|
};
|
|
177
183
|
return getIds(toFlatArray(tree));
|
|
178
184
|
}
|
|
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
185
|
|
|
208
|
-
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove,
|
|
186
|
+
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, deepTraversal, getTreeIds };
|
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,39 +158,39 @@ 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
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
171
|
+
const dateArr = strDate.split(sep); //这边给定一个特定时间
|
|
172
|
+
const newDate = new Date(+dateArr[0], +dateArr[1] - 1, +dateArr[2]);
|
|
173
|
+
const befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(String(n)); //计算前几天用减,计算后几天用加,最后一个就是多少天的数量
|
|
174
|
+
const beforeDat = new Date();
|
|
61
175
|
beforeDat.setTime(befminuts);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
176
|
+
const befMonth = beforeDat.getMonth() + 1;
|
|
177
|
+
const mon = befMonth >= 10 ? befMonth : '0' + befMonth;
|
|
178
|
+
const befDate = beforeDat.getDate();
|
|
179
|
+
const da = befDate >= 10 ? befDate : '0' + befDate;
|
|
180
|
+
const finalNewDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
|
|
67
181
|
return finalNewDate;
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
191
|
+
const date = new Date();
|
|
192
|
+
const separator1 = '-';
|
|
193
|
+
const separator2 = ':';
|
|
80
194
|
let year = date.getFullYear();
|
|
81
195
|
let month = date.getMonth() + 1;
|
|
82
196
|
let strDate = date.getDate() + n;
|
|
@@ -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 };
|
package/lib/es/dom.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
|
*/
|
|
@@ -16,11 +16,11 @@ import { isObject } from './type.js';
|
|
|
16
16
|
* @param {string} className
|
|
17
17
|
* @returns {boolean}
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
function hasClass(el, className) {
|
|
20
20
|
if (className.indexOf(' ') !== -1)
|
|
21
21
|
throw new Error('className should not contain space.');
|
|
22
22
|
return el.classList.contains(className);
|
|
23
|
-
}
|
|
23
|
+
}
|
|
24
24
|
const eachClassName = (classNames, func) => {
|
|
25
25
|
const classNameList = classNames.split(/\s+/g);
|
|
26
26
|
classNameList.forEach(func);
|
|
@@ -30,17 +30,17 @@ const eachClassName = (classNames, func) => {
|
|
|
30
30
|
* @param {HTMLElement} el
|
|
31
31
|
* @param {string} classNames
|
|
32
32
|
*/
|
|
33
|
-
|
|
33
|
+
function addClass(el, classNames) {
|
|
34
34
|
eachClassName(classNames, className => el.classList.add(className));
|
|
35
|
-
}
|
|
35
|
+
}
|
|
36
36
|
/**
|
|
37
37
|
* 给元素移除样式名
|
|
38
38
|
* @param {HTMLElement} el
|
|
39
39
|
* @param {string} classNames
|
|
40
40
|
*/
|
|
41
|
-
|
|
41
|
+
function removeClass(el, classNames) {
|
|
42
42
|
eachClassName(classNames, className => el.classList.remove(className));
|
|
43
|
-
}
|
|
43
|
+
}
|
|
44
44
|
/**
|
|
45
45
|
* 设置元素样式
|
|
46
46
|
* @param {HTMLElement} el
|
|
@@ -59,12 +59,14 @@ const setStyle = (el, key, val) => {
|
|
|
59
59
|
};
|
|
60
60
|
/**
|
|
61
61
|
* 获取元素样式
|
|
62
|
-
* @param {HTMLElement} el
|
|
62
|
+
* @param {HTMLElement} el 元素
|
|
63
63
|
* @param {string} key
|
|
64
64
|
* @returns {string}
|
|
65
65
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
function getStyle(el, key) {
|
|
67
|
+
return getComputedStyle(el).getPropertyValue(key);
|
|
68
|
+
}
|
|
69
|
+
function smoothScroll(options) {
|
|
68
70
|
return new Promise(resolve => {
|
|
69
71
|
const defaults = {
|
|
70
72
|
el: document,
|
|
@@ -139,5 +141,16 @@ function onDomReady(callback) {
|
|
|
139
141
|
domReadyCallbacks.push(callback);
|
|
140
142
|
}
|
|
141
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* 获取元素样式属性的计算值
|
|
146
|
+
* @param {HTMLElement} el
|
|
147
|
+
* @param {string} property
|
|
148
|
+
* @param {boolean} reNumber
|
|
149
|
+
* @returns {string|number}
|
|
150
|
+
*/
|
|
151
|
+
function getComputedCssVal(el, property, reNumber = true) {
|
|
152
|
+
const originVal = getComputedStyle(el).getPropertyValue(property) ?? '';
|
|
153
|
+
return reNumber ? Number(originVal.replace(/([0-9]*)(.*)/g, '$1')) : originVal;
|
|
154
|
+
}
|
|
142
155
|
|
|
143
|
-
export { addClass, getStyle, hasClass, isDomReady, onDomReady, removeClass, setStyle, smoothScroll };
|
|
156
|
+
export { addClass, getComputedCssVal, getStyle, hasClass, isDomReady, onDomReady, removeClass, setStyle, smoothScroll };
|