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/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-present, [chandq](https://github.com/chandq)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/lib/cjs/array.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
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 object = require('./object.js');
|
|
10
|
+
var type = require('./type.js');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 判断一个对象是否为类数组
|
|
14
|
+
* @param any
|
|
15
|
+
* @returns {boolean}
|
|
16
|
+
*/
|
|
17
|
+
const arrayLike = (any) => {
|
|
18
|
+
if (type.isArray(any))
|
|
19
|
+
return true;
|
|
20
|
+
if (type.isString(any))
|
|
21
|
+
return true;
|
|
22
|
+
if (!type.isObject(any))
|
|
23
|
+
return false;
|
|
24
|
+
return object.objectHas(any, 'length');
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* 遍历数组,返回 false 中断遍历
|
|
28
|
+
* @param {ArrayLike<V>} array
|
|
29
|
+
* @param {(val: V, idx: number) => any} iterator
|
|
30
|
+
* @param reverse {boolean} 是否倒序
|
|
31
|
+
*/
|
|
32
|
+
const arrayEach = (array, iterator, reverse = false) => {
|
|
33
|
+
if (reverse) {
|
|
34
|
+
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
35
|
+
const val = array[idx];
|
|
36
|
+
if (iterator(val, idx) === false)
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
for (let idx = 0; idx < array.length; idx++) {
|
|
42
|
+
const val = array[idx];
|
|
43
|
+
if (iterator(val, idx) === false)
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* 异步遍历数组,返回 false 中断遍历
|
|
50
|
+
* @param {ArrayLike<V>} array
|
|
51
|
+
* @param {(val: V, idx: number) => Promise<any>} iterator
|
|
52
|
+
* @param {boolean} reverse
|
|
53
|
+
*/
|
|
54
|
+
async function arrayEachAsync(array, iterator, reverse = false) {
|
|
55
|
+
if (reverse) {
|
|
56
|
+
for (let idx = array.length - 1; idx >= 0; idx--) {
|
|
57
|
+
const val = array[idx];
|
|
58
|
+
if ((await iterator(val, idx)) === false)
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
for (let idx = 0; idx < array.length; idx++) {
|
|
64
|
+
const val = array[idx];
|
|
65
|
+
if ((await iterator(val, idx)) === false)
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 插入到目标位置之前
|
|
72
|
+
* @param {AnyArray} array
|
|
73
|
+
* @param {number} start
|
|
74
|
+
* @param {number} to
|
|
75
|
+
*/
|
|
76
|
+
const arrayInsertBefore = (array, start, to) => {
|
|
77
|
+
if (start === to || start + 1 === to)
|
|
78
|
+
return;
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
80
|
+
const [source] = array.splice(start, 1);
|
|
81
|
+
const insertIndex = to < start ? to : to - 1;
|
|
82
|
+
array.splice(insertIndex, 0, source);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* 数组删除指定项目
|
|
86
|
+
* @param {V[]} array
|
|
87
|
+
* @param {(val: V, idx: number) => boolean} expect
|
|
88
|
+
* @returns {V[]}
|
|
89
|
+
*/
|
|
90
|
+
function arrayRemove(array, expect) {
|
|
91
|
+
const indexes = [];
|
|
92
|
+
// 这里重命名一下:是为了杜绝 jest 里的 expect 与之产生检查错误
|
|
93
|
+
// eslint 的 jest 语法检查是遇到 expect 函数就以为是单元测试
|
|
94
|
+
const _expect = expect;
|
|
95
|
+
arrayEach(array, (val, idx) => {
|
|
96
|
+
if (_expect(val, idx))
|
|
97
|
+
indexes.push(idx);
|
|
98
|
+
});
|
|
99
|
+
indexes.forEach((val, idx) => array.splice(val - idx, 1));
|
|
100
|
+
return array;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 自定义深度优先遍历函数(支持continue和break操作)
|
|
104
|
+
* @param {array} deepList
|
|
105
|
+
* @param {function} iterator
|
|
106
|
+
* @param {array} children
|
|
107
|
+
* @param {boolean} isReverse 是否反向遍历
|
|
108
|
+
*/
|
|
109
|
+
const deepTraversal = (deepList, iterator, children = 'children', isReverse = false) => {
|
|
110
|
+
let level = 0;
|
|
111
|
+
const walk = (arr, parent) => {
|
|
112
|
+
if (isReverse) {
|
|
113
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
114
|
+
const re = iterator(arr[i], i, deepList, parent, level);
|
|
115
|
+
if (re === 'break') {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
else if (re === 'continue') {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
if (Array.isArray(arr[i][children])) {
|
|
123
|
+
++level;
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
walk(arr[i][children], arr[i]);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
for (let i = 0; i < arr.length; i++) {
|
|
131
|
+
const re = iterator(arr[i], i, deepList, parent, level);
|
|
132
|
+
if (re === 'break') {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
else if (re === 'continue') {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
// @ts-ignore
|
|
139
|
+
if (Array.isArray(arr[i][children])) {
|
|
140
|
+
++level;
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
walk(arr[i][children], arr[i]);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
walk(deepList, null);
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
151
|
+
* @param {ArrayLike<T>} tree
|
|
152
|
+
* @param {IdLike} nodeId
|
|
153
|
+
* @param {ITreeConf} config
|
|
154
|
+
* @return {[IdLike[], ITreeItem<V>[]]}
|
|
155
|
+
*/
|
|
156
|
+
function getTreeIds(tree, nodeId, config) {
|
|
157
|
+
const { children = 'children', id = 'id' } = config || {};
|
|
158
|
+
const toFlatArray = (tree, parentId, parent) => {
|
|
159
|
+
return tree.reduce((t, _) => {
|
|
160
|
+
const child = _[children];
|
|
161
|
+
return [
|
|
162
|
+
...t,
|
|
163
|
+
parentId ? { ..._, parentId, parent } : _,
|
|
164
|
+
...(child && child.length ? toFlatArray(child, _[id], _) : [])
|
|
165
|
+
];
|
|
166
|
+
}, []);
|
|
167
|
+
};
|
|
168
|
+
const getIds = (flatArray) => {
|
|
169
|
+
let child = flatArray.find(_ => _[id] === nodeId);
|
|
170
|
+
const { parent, parentId, ...other } = child;
|
|
171
|
+
let ids = [nodeId], nodes = [other];
|
|
172
|
+
while (child && child.parentId) {
|
|
173
|
+
ids = [child.parentId, ...ids];
|
|
174
|
+
nodes = [child.parent, ...nodes];
|
|
175
|
+
child = flatArray.find(_ => _[id] === child.parentId);
|
|
176
|
+
}
|
|
177
|
+
return [ids, nodes];
|
|
178
|
+
};
|
|
179
|
+
return getIds(toFlatArray(tree));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 异步ForEach函数
|
|
183
|
+
* @param {array} array
|
|
184
|
+
* @param {asyncFuntion} callback
|
|
185
|
+
* // asyncForEach 使用范例如下
|
|
186
|
+
// const start = async () => {
|
|
187
|
+
// await asyncForEach(result, async (item) => {
|
|
188
|
+
// await request(item);
|
|
189
|
+
// count++;
|
|
190
|
+
// });
|
|
191
|
+
|
|
192
|
+
// console.log('发送次数', count);
|
|
193
|
+
// }
|
|
194
|
+
|
|
195
|
+
// for await...of 使用范例如下
|
|
196
|
+
// const loadImages = async (images) => {
|
|
197
|
+
// for await (const item of images) {
|
|
198
|
+
// await request(item);
|
|
199
|
+
// count++;
|
|
200
|
+
// }
|
|
201
|
+
// }
|
|
202
|
+
* @return {*}
|
|
203
|
+
*/
|
|
204
|
+
async function asyncForEach(array, callback) {
|
|
205
|
+
for (let index = 0, len = array.length; index < len; index++) {
|
|
206
|
+
await callback(array[index], index, array);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
exports.arrayEach = arrayEach;
|
|
211
|
+
exports.arrayEachAsync = arrayEachAsync;
|
|
212
|
+
exports.arrayInsertBefore = arrayInsertBefore;
|
|
213
|
+
exports.arrayLike = arrayLike;
|
|
214
|
+
exports.arrayRemove = arrayRemove;
|
|
215
|
+
exports.asyncForEach = asyncForEach;
|
|
216
|
+
exports.deepTraversal = deepTraversal;
|
|
217
|
+
exports.getTreeIds = getTreeIds;
|
package/lib/cjs/async.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
* 等待一段时间
|
|
11
|
+
* @param {number} timeout 等待时间,单位毫秒
|
|
12
|
+
* @returns {Promise<void>}
|
|
13
|
+
*/
|
|
14
|
+
async function wait(timeout = 1) {
|
|
15
|
+
return new Promise(resolve => setTimeout(resolve, timeout));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 异步遍历
|
|
19
|
+
* @ref https://github.com/Kevnz/async-tools/blob/master/src/mapper.js
|
|
20
|
+
* @ref https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator
|
|
21
|
+
* @param {Array<T>} list
|
|
22
|
+
* @param {(val: T, idx: number, list: ArrayLike<T>) => Promise<R>} mapper
|
|
23
|
+
* @param {number} concurrency 并发数量,默认无限
|
|
24
|
+
* @returns {Promise<R[]>}
|
|
25
|
+
*/
|
|
26
|
+
async function asyncMap(list, mapper, concurrency = Infinity) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const iterator = list[Symbol.iterator]();
|
|
29
|
+
const limit = Math.min(list.length, concurrency);
|
|
30
|
+
const resolves = [];
|
|
31
|
+
let resolvedLength = 0;
|
|
32
|
+
let rejected;
|
|
33
|
+
let index = 0;
|
|
34
|
+
const next = () => {
|
|
35
|
+
if (rejected)
|
|
36
|
+
return reject(rejected);
|
|
37
|
+
const it = iterator.next();
|
|
38
|
+
if (it.done) {
|
|
39
|
+
if (resolvedLength === list.length)
|
|
40
|
+
resolve(resolves);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const current = index++;
|
|
44
|
+
mapper(it.value, current, list)
|
|
45
|
+
.then(value => {
|
|
46
|
+
resolvedLength++;
|
|
47
|
+
resolves[current] = value;
|
|
48
|
+
next();
|
|
49
|
+
})
|
|
50
|
+
.catch(err => {
|
|
51
|
+
rejected = err;
|
|
52
|
+
next();
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
// 开始
|
|
56
|
+
for (let i = 0; i < limit; i++) {
|
|
57
|
+
next();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
exports.asyncMap = asyncMap;
|
|
63
|
+
exports.wait = wait;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 dom = require('./dom.js');
|
|
10
|
+
|
|
11
|
+
const textEl = document.createElement('textarea');
|
|
12
|
+
dom.setStyle(textEl, {
|
|
13
|
+
position: 'absolute',
|
|
14
|
+
top: '-9999px',
|
|
15
|
+
left: '-9999px',
|
|
16
|
+
opacity: 0
|
|
17
|
+
});
|
|
18
|
+
document.body.appendChild(textEl);
|
|
19
|
+
/**
|
|
20
|
+
* 复制文本
|
|
21
|
+
* @param {string} text
|
|
22
|
+
*/
|
|
23
|
+
const copyText = (text) => {
|
|
24
|
+
textEl.value = text;
|
|
25
|
+
textEl.focus({ preventScroll: true });
|
|
26
|
+
textEl.select();
|
|
27
|
+
try {
|
|
28
|
+
document.execCommand('copy');
|
|
29
|
+
textEl.blur();
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
// ignore
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.copyText = copyText;
|
|
@@ -0,0 +1,63 @@
|
|
|
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 type = require('./type.js');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 获取cookie
|
|
13
|
+
* @param {string} name
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
const cookieGet = (name) => {
|
|
17
|
+
const { cookie } = document;
|
|
18
|
+
if (!cookie)
|
|
19
|
+
return '';
|
|
20
|
+
const result = cookie.split(';');
|
|
21
|
+
for (let i = 0; i < result.length; i++) {
|
|
22
|
+
const item = result[i];
|
|
23
|
+
const [key, val = ''] = item.split('=');
|
|
24
|
+
if (key === name)
|
|
25
|
+
return decodeURIComponent(val);
|
|
26
|
+
}
|
|
27
|
+
return '';
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* 设置 cookie
|
|
31
|
+
* @param {string} name
|
|
32
|
+
* @param {string} value
|
|
33
|
+
* @param {number | Date} [maxAge]
|
|
34
|
+
*/
|
|
35
|
+
const cookieSet = (name, value, maxAge) => {
|
|
36
|
+
const metas = [];
|
|
37
|
+
const EXPIRES = 'expires';
|
|
38
|
+
metas.push([name, encodeURIComponent(value)]);
|
|
39
|
+
if (type.isNumber(maxAge)) {
|
|
40
|
+
const d = new Date();
|
|
41
|
+
d.setTime(d.getTime() + maxAge);
|
|
42
|
+
metas.push([EXPIRES, d.toUTCString()]);
|
|
43
|
+
}
|
|
44
|
+
else if (type.isDate(maxAge)) {
|
|
45
|
+
metas.push([EXPIRES, maxAge.toUTCString()]);
|
|
46
|
+
}
|
|
47
|
+
metas.push(['path', '/']);
|
|
48
|
+
document.cookie = metas
|
|
49
|
+
.map(item => {
|
|
50
|
+
const [key, val] = item;
|
|
51
|
+
return `${key}=${val}`;
|
|
52
|
+
})
|
|
53
|
+
.join(';');
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* 删除单个 cookie
|
|
57
|
+
* @param name cookie 名称
|
|
58
|
+
*/
|
|
59
|
+
const cookieDel = (name) => cookieSet(name, '', -1);
|
|
60
|
+
|
|
61
|
+
exports.cookieDel = cookieDel;
|
|
62
|
+
exports.cookieGet = cookieGet;
|
|
63
|
+
exports.cookieSet = cookieSet;
|
package/lib/cjs/date.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
* 格式化为日期对象(带自定义格式化模板)
|
|
11
|
+
* @param {DateValue} value 可以是数值、字符串或 Date 对象
|
|
12
|
+
* @param {string} [format] 模板,默认是 YYYY-MM-DD HH:mm:ss,模板字符:
|
|
13
|
+
* - YYYY:年
|
|
14
|
+
* - yyyy: 年
|
|
15
|
+
* - MM:月
|
|
16
|
+
* - DD:日
|
|
17
|
+
* - dd: 日
|
|
18
|
+
* - HH:时(24 小时制)
|
|
19
|
+
* - hh:时(12 小时制)
|
|
20
|
+
* - mm:分
|
|
21
|
+
* - ss:秒
|
|
22
|
+
* - SSS:毫秒
|
|
23
|
+
* - ww: 周
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
const formatDate = (date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') => {
|
|
27
|
+
let fmt = format;
|
|
28
|
+
let ret;
|
|
29
|
+
const opt = {
|
|
30
|
+
'Y+': `${date.getFullYear()}`,
|
|
31
|
+
'y+': `${date.getFullYear()}`,
|
|
32
|
+
'M+': `${date.getMonth() + 1}`,
|
|
33
|
+
'D+': `${date.getDate()}`,
|
|
34
|
+
'd+': `${date.getDate()}`,
|
|
35
|
+
'H+': `${date.getHours()}`,
|
|
36
|
+
'm+': `${date.getMinutes()}`,
|
|
37
|
+
's+': `${date.getSeconds()}`,
|
|
38
|
+
'S+': `${date.getMilliseconds()}`,
|
|
39
|
+
'w+': ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()] // 周
|
|
40
|
+
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
|
41
|
+
};
|
|
42
|
+
for (const k in opt) {
|
|
43
|
+
ret = new RegExp('(' + k + ')').exec(fmt);
|
|
44
|
+
if (ret) {
|
|
45
|
+
fmt = fmt.replace(ret[1], ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return fmt;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* 计算向前或向后N天的具体日期
|
|
52
|
+
* @param {string} strDate 参考日期
|
|
53
|
+
* @param {number} n 正数:向后推算;负数:向前推算
|
|
54
|
+
* @param {string} sep 日期格式的分隔符
|
|
55
|
+
* @return {*} 目标日期
|
|
56
|
+
*/
|
|
57
|
+
function calculateDate(strDate, n, sep = '-') {
|
|
58
|
+
//strDate 为字符串日期 如:'2019-01-01' n为你要传入的参数,当前为0,前一天为-1,后一天为1
|
|
59
|
+
let dateArr = strDate.split(sep); //这边给定一个特定时间
|
|
60
|
+
let newDate = new Date(+dateArr[0], +dateArr[1] - 1, +dateArr[2]);
|
|
61
|
+
let befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(String(n)); //计算前几天用减,计算后几天用加,最后一个就是多少天的数量
|
|
62
|
+
let beforeDat = new Date();
|
|
63
|
+
beforeDat.setTime(befminuts);
|
|
64
|
+
let befMonth = beforeDat.getMonth() + 1;
|
|
65
|
+
let mon = befMonth >= 10 ? befMonth : '0' + befMonth;
|
|
66
|
+
let befDate = beforeDat.getDate();
|
|
67
|
+
let da = befDate >= 10 ? befDate : '0' + befDate;
|
|
68
|
+
let finalNewDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
|
|
69
|
+
return finalNewDate;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 计算向前或向后N天的具体时间日期
|
|
73
|
+
* @param {number} n 正数:向后推算;负数:向前推算
|
|
74
|
+
* @param {string} dateSep 日期分隔符
|
|
75
|
+
* @param {string} timeSep 时间分隔符
|
|
76
|
+
* @return {*}
|
|
77
|
+
*/
|
|
78
|
+
function calculateDateTime(n, dateSep = '-', timeSep = ':') {
|
|
79
|
+
let date = new Date();
|
|
80
|
+
let separator1 = '-';
|
|
81
|
+
let separator2 = ':';
|
|
82
|
+
let year = date.getFullYear();
|
|
83
|
+
let month = date.getMonth() + 1;
|
|
84
|
+
let strDate = date.getDate() + n;
|
|
85
|
+
if (strDate > new Date(year, month, 0).getDate()) {
|
|
86
|
+
month += 1;
|
|
87
|
+
strDate -= new Date(year, month, 0).getDate();
|
|
88
|
+
if (month > 12) {
|
|
89
|
+
year += 1;
|
|
90
|
+
month = 1;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (month >= 1 && month <= 9) {
|
|
94
|
+
month = '0' + month;
|
|
95
|
+
}
|
|
96
|
+
if (strDate >= 0 && strDate <= 9) {
|
|
97
|
+
strDate = '0' + strDate;
|
|
98
|
+
}
|
|
99
|
+
return (year +
|
|
100
|
+
separator1 +
|
|
101
|
+
month +
|
|
102
|
+
separator1 +
|
|
103
|
+
strDate +
|
|
104
|
+
' ' +
|
|
105
|
+
date.getHours() +
|
|
106
|
+
separator2 +
|
|
107
|
+
date.getMinutes() +
|
|
108
|
+
separator2 +
|
|
109
|
+
date.getSeconds());
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
exports.calculateDate = calculateDate;
|
|
113
|
+
exports.calculateDateTime = calculateDateTime;
|
|
114
|
+
exports.formatDate = formatDate;
|
package/lib/cjs/dom.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
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 array = require('./array.js');
|
|
10
|
+
var easing = require('./easing.js');
|
|
11
|
+
var object = require('./object.js');
|
|
12
|
+
var string = require('./string.js');
|
|
13
|
+
var type = require('./type.js');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 判断元素是否包含某个样式名
|
|
17
|
+
* @param {HTMLElement} el
|
|
18
|
+
* @param {string} className
|
|
19
|
+
* @returns {boolean}
|
|
20
|
+
*/
|
|
21
|
+
const hasClass = (el, className) => {
|
|
22
|
+
if (className.indexOf(' ') !== -1)
|
|
23
|
+
throw new Error('className should not contain space.');
|
|
24
|
+
return el.classList.contains(className);
|
|
25
|
+
};
|
|
26
|
+
const eachClassName = (classNames, func) => {
|
|
27
|
+
const classNameList = classNames.split(/\s+/g);
|
|
28
|
+
classNameList.forEach(func);
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* 给元素增加样式名
|
|
32
|
+
* @param {HTMLElement} el
|
|
33
|
+
* @param {string} classNames
|
|
34
|
+
*/
|
|
35
|
+
const addClass = (el, classNames) => {
|
|
36
|
+
eachClassName(classNames, className => el.classList.add(className));
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* 给元素移除样式名
|
|
40
|
+
* @param {HTMLElement} el
|
|
41
|
+
* @param {string} classNames
|
|
42
|
+
*/
|
|
43
|
+
const removeClass = (el, classNames) => {
|
|
44
|
+
eachClassName(classNames, className => el.classList.remove(className));
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 设置元素样式
|
|
48
|
+
* @param {HTMLElement} el
|
|
49
|
+
* @param {string | Style} key
|
|
50
|
+
* @param {string} val
|
|
51
|
+
*/
|
|
52
|
+
const setStyle = (el, key, val) => {
|
|
53
|
+
if (type.isObject(key)) {
|
|
54
|
+
object.objectEach(key, (val1, key1) => {
|
|
55
|
+
setStyle(el, key1, val1);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
el.style.setProperty(string.stringKebabCase(key), val);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* 获取元素样式
|
|
64
|
+
* @param {HTMLElement} el
|
|
65
|
+
* @param {string} key
|
|
66
|
+
* @returns {string}
|
|
67
|
+
*/
|
|
68
|
+
const getStyle = (el, key) => getComputedStyle(el).getPropertyValue(key);
|
|
69
|
+
async function smoothScroll(options) {
|
|
70
|
+
return new Promise(resolve => {
|
|
71
|
+
const defaults = {
|
|
72
|
+
el: document,
|
|
73
|
+
to: 0,
|
|
74
|
+
duration: 567,
|
|
75
|
+
easing: 'ease'
|
|
76
|
+
};
|
|
77
|
+
const { el, to, duration, easing: easing$1 } = object.objectAssign(defaults, options);
|
|
78
|
+
const htmlEl = document.documentElement;
|
|
79
|
+
const bodyEl = document.body;
|
|
80
|
+
const globalMode = el === window || el === document || el === htmlEl || el === bodyEl;
|
|
81
|
+
const els = globalMode ? [htmlEl, bodyEl] : [el];
|
|
82
|
+
const query = () => {
|
|
83
|
+
let value = 0;
|
|
84
|
+
array.arrayEach(els, el => {
|
|
85
|
+
if ('scrollTop' in el) {
|
|
86
|
+
value = el.scrollTop;
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return value;
|
|
91
|
+
};
|
|
92
|
+
const update = (val) => {
|
|
93
|
+
els.forEach(el => {
|
|
94
|
+
if ('scrollTop' in el) {
|
|
95
|
+
el.scrollTop = val;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
let startTime;
|
|
100
|
+
const startValue = query();
|
|
101
|
+
const length = to - startValue;
|
|
102
|
+
const easingFn = easing.easingFunctional(easing$1);
|
|
103
|
+
const render = () => {
|
|
104
|
+
const now = performance.now();
|
|
105
|
+
const passingTime = startTime ? now - startTime : 0;
|
|
106
|
+
const t = passingTime / duration;
|
|
107
|
+
const p = easingFn(t);
|
|
108
|
+
if (!startTime)
|
|
109
|
+
startTime = now;
|
|
110
|
+
update(startValue + length * p);
|
|
111
|
+
if (t >= 1)
|
|
112
|
+
resolve();
|
|
113
|
+
else
|
|
114
|
+
requestAnimationFrame(render);
|
|
115
|
+
};
|
|
116
|
+
render();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const domReadyCallbacks = [];
|
|
120
|
+
const eventType = 'DOMContentLoaded';
|
|
121
|
+
const listener = () => {
|
|
122
|
+
domReadyCallbacks.forEach(callback => callback());
|
|
123
|
+
domReadyCallbacks.length = 0;
|
|
124
|
+
document.removeEventListener(eventType, listener);
|
|
125
|
+
};
|
|
126
|
+
document.addEventListener(eventType, listener);
|
|
127
|
+
let readied = false;
|
|
128
|
+
function isDomReady() {
|
|
129
|
+
if (readied)
|
|
130
|
+
return true;
|
|
131
|
+
readied = ['complete', 'loaded', 'interactive'].indexOf(document.readyState) !== -1;
|
|
132
|
+
return readied;
|
|
133
|
+
}
|
|
134
|
+
function onDomReady(callback) {
|
|
135
|
+
// document readied
|
|
136
|
+
if (isDomReady()) {
|
|
137
|
+
setTimeout(callback, 0);
|
|
138
|
+
}
|
|
139
|
+
// listen document to ready
|
|
140
|
+
else {
|
|
141
|
+
domReadyCallbacks.push(callback);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
exports.addClass = addClass;
|
|
146
|
+
exports.getStyle = getStyle;
|
|
147
|
+
exports.hasClass = hasClass;
|
|
148
|
+
exports.isDomReady = isDomReady;
|
|
149
|
+
exports.onDomReady = onDomReady;
|
|
150
|
+
exports.removeClass = removeClass;
|
|
151
|
+
exports.setStyle = setStyle;
|
|
152
|
+
exports.smoothScroll = smoothScroll;
|