sculp-js 0.1.1 → 1.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/README.md +3 -0
- package/lib/cjs/array.js +56 -36
- 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 +9 -3
- 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/tooltip.js +118 -0
- 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 +55 -35
- 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 +4 -3
- 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/tooltip.js +116 -0
- 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 +155 -66
- package/lib/tsdoc-metadata.json +11 -0
- package/lib/umd/index.js +365 -108
- package/package.json +3 -2
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 };
|
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,
|
|
@@ -144,7 +146,7 @@ function onDomReady(callback) {
|
|
|
144
146
|
* @param {HTMLElement} el
|
|
145
147
|
* @param {string} property
|
|
146
148
|
* @param {boolean} reNumber
|
|
147
|
-
* @
|
|
149
|
+
* @returns {string|number}
|
|
148
150
|
*/
|
|
149
151
|
function getComputedCssVal(el, property, reNumber = true) {
|
|
150
152
|
const originVal = getComputedStyle(el).getPropertyValue(property) ?? '';
|
package/lib/es/download.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,15 +11,15 @@ import { urlSetParams } from './url.js';
|
|
|
11
11
|
* @param {string} url
|
|
12
12
|
* @param {LooseParams} params
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
function downloadURL(url, params) {
|
|
15
15
|
window.open(params ? urlSetParams(url, params) : url);
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
/**
|
|
18
18
|
* 通过 A 链接的方式下载
|
|
19
19
|
* @param {string} href
|
|
20
20
|
* @param {string} filename
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
function downloadHref(href, filename) {
|
|
23
23
|
const eleLink = document.createElement('a');
|
|
24
24
|
eleLink.download = filename;
|
|
25
25
|
eleLink.style.display = 'none';
|
|
@@ -27,17 +27,17 @@ const downloadHref = (href, filename) => {
|
|
|
27
27
|
document.body.appendChild(eleLink);
|
|
28
28
|
eleLink.click();
|
|
29
29
|
setTimeout(() => document.body.removeChild(eleLink));
|
|
30
|
-
}
|
|
30
|
+
}
|
|
31
31
|
/**
|
|
32
32
|
* 将大文件对象通过 A 链接的方式下载
|
|
33
33
|
* @param {Blob} blob
|
|
34
34
|
* @param {string} filename
|
|
35
35
|
*/
|
|
36
|
-
|
|
36
|
+
function downloadBlob(blob, filename) {
|
|
37
37
|
const objURL = URL.createObjectURL(blob);
|
|
38
38
|
downloadHref(objURL, filename);
|
|
39
39
|
setTimeout(() => URL.revokeObjectURL(objURL));
|
|
40
|
-
}
|
|
40
|
+
}
|
|
41
41
|
/**
|
|
42
42
|
* 将指定数据格式通过 A 链接的方式下载
|
|
43
43
|
* @param {AnyObject | AnyObject[]} data
|
|
@@ -45,7 +45,7 @@ const downloadBlob = (blob, filename) => {
|
|
|
45
45
|
* @param {string} filename
|
|
46
46
|
* @param {string[]} [headers]
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
function downloadData(data, fileType, filename, headers) {
|
|
49
49
|
filename = filename.replace(`.${fileType}`, '') + `.${fileType}`;
|
|
50
50
|
if (fileType === 'json') {
|
|
51
51
|
const blob = new Blob([JSON.stringify(data, null, 4)]);
|
|
@@ -72,6 +72,6 @@ const downloadData = (data, fileType, filename, headers) => {
|
|
|
72
72
|
const href = 'data:' + MIMETypes[fileType] + ';charset=utf-8,\ufeff' + encodeURIComponent(headerStr + bodyStr);
|
|
73
73
|
downloadHref(href, filename);
|
|
74
74
|
}
|
|
75
|
-
}
|
|
75
|
+
}
|
|
76
76
|
|
|
77
77
|
export { downloadBlob, downloadData, downloadHref, downloadURL };
|
package/lib/es/easing.js
CHANGED
package/lib/es/file.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
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
7
|
/**
|
|
8
8
|
* 选择本地文件
|
|
9
|
-
* @param {
|
|
10
|
-
* @
|
|
9
|
+
* @param {string} accept 上传的文件类型,用于过滤
|
|
10
|
+
* @param {Function} changeCb 选择文件回调
|
|
11
|
+
* @returns {HTMLInputElement}
|
|
11
12
|
*/
|
|
12
|
-
function chooseLocalFile(
|
|
13
|
+
function chooseLocalFile(accept, changeCb) {
|
|
13
14
|
const inputObj = document.createElement('input');
|
|
14
15
|
inputObj.setAttribute('id', String(Date.now()));
|
|
15
16
|
inputObj.setAttribute('type', 'file');
|
package/lib/es/func.js
CHANGED
package/lib/es/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
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
|
-
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove,
|
|
7
|
+
export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayLike, arrayRemove, forEachDeep, searchTreeById } from './array.js';
|
|
8
8
|
export { copyText } from './clipboard.js';
|
|
9
9
|
export { cookieDel, cookieGet, cookieSet } from './cookie.js';
|
|
10
|
-
export { calculateDate, calculateDateTime, formatDate } from './date.js';
|
|
10
|
+
export { calculateDate, calculateDateTime, dateParse, dateToEnd, dateToStart, formatDate, isValidDate } from './date.js';
|
|
11
11
|
export { addClass, getComputedCssVal, getStyle, hasClass, isDomReady, onDomReady, removeClass, setStyle, smoothScroll } from './dom.js';
|
|
12
12
|
export { downloadBlob, downloadData, downloadHref, downloadURL } from './download.js';
|
|
13
13
|
export { cloneDeep, isPlainObject, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectHas, objectMap, objectAssign as objectMerge, objectOmit, objectPick } from './object.js';
|
|
@@ -23,3 +23,4 @@ export { debounce, getGlobal, once, setGlobal, throttle } from './func.js';
|
|
|
23
23
|
export { STRING_POOL, randomNumber, randomString, randomUuid } from './random.js';
|
|
24
24
|
export { HEX_POOL, formatNumber, numberAbbr, numberToHex } from './number.js';
|
|
25
25
|
export { UNIQUE_NUMBER_SAFE_LENGTH, uniqueNumber, uniqueString } from './unique.js';
|
|
26
|
+
export { tooltipEvent } from './tooltip.js';
|
package/lib/es/number.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
|
*/
|
|
@@ -19,7 +19,7 @@ const remainder = (x, y) => (supportBigInt ? x % y : jsbi().remainder(x, y));
|
|
|
19
19
|
* @param {string} [hexPool] 进制池,默认 62 进制
|
|
20
20
|
* @returns {string}
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
function numberToHex(decimal, hexPool = HEX_POOL) {
|
|
23
23
|
if (hexPool.length < 2)
|
|
24
24
|
throw new Error('进制池长度不能少于 2');
|
|
25
25
|
if (!supportBigInt) {
|
|
@@ -41,7 +41,7 @@ const numberToHex = (decimal, hexPool = HEX_POOL) => {
|
|
|
41
41
|
};
|
|
42
42
|
execute();
|
|
43
43
|
return ret.join('');
|
|
44
|
-
}
|
|
44
|
+
}
|
|
45
45
|
/**
|
|
46
46
|
* 缩写
|
|
47
47
|
* @param {number | string} num
|
|
@@ -68,7 +68,7 @@ const numberAbbr = (num, units, ratio = 1000, exponent) => {
|
|
|
68
68
|
* 将数字格式化成千位分隔符显示的字符串
|
|
69
69
|
* @param {number} val 数字
|
|
70
70
|
* @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
|
|
71
|
-
* @
|
|
71
|
+
* @returns {string}
|
|
72
72
|
*/
|
|
73
73
|
function formatNumber(val, type = 'int') {
|
|
74
74
|
return type === 'int' ? parseInt(String(val)).toLocaleString() : Number(val).toLocaleString('en-US');
|
package/lib/es/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
|
*/
|
|
@@ -27,20 +27,22 @@ const isPlainObject = (obj) => {
|
|
|
27
27
|
* @param {string} key
|
|
28
28
|
* @returns {boolean}
|
|
29
29
|
*/
|
|
30
|
-
|
|
30
|
+
function objectHas(obj, key) {
|
|
31
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
32
|
+
}
|
|
31
33
|
/**
|
|
32
34
|
* 遍历对象,返回 false 中断遍历
|
|
33
35
|
* @param {O} obj
|
|
34
36
|
* @param {(val: O[keyof O], key: keyof O) => (boolean | void)} iterator
|
|
35
37
|
*/
|
|
36
|
-
|
|
38
|
+
function objectEach(obj, iterator) {
|
|
37
39
|
for (const key in obj) {
|
|
38
40
|
if (!objectHas(obj, key))
|
|
39
41
|
continue;
|
|
40
42
|
if (iterator(obj[key], key) === false)
|
|
41
43
|
break;
|
|
42
44
|
}
|
|
43
|
-
}
|
|
45
|
+
}
|
|
44
46
|
/**
|
|
45
47
|
* 异步遍历对象,返回 false 中断遍历
|
|
46
48
|
* @param {O} obj
|
|
@@ -146,7 +148,7 @@ const merge = (map, source, target) => {
|
|
|
146
148
|
* @param {ObjectAssignItem | undefined} targets
|
|
147
149
|
* @returns {R}
|
|
148
150
|
*/
|
|
149
|
-
|
|
151
|
+
function objectAssign(source, ...targets) {
|
|
150
152
|
const map = new Map();
|
|
151
153
|
for (let i = 0; i < targets.length; i++) {
|
|
152
154
|
const target = targets[i];
|
|
@@ -156,7 +158,7 @@ const objectAssign = (source, ...targets) => {
|
|
|
156
158
|
}
|
|
157
159
|
map.clear();
|
|
158
160
|
return source;
|
|
159
|
-
}
|
|
161
|
+
}
|
|
160
162
|
/**
|
|
161
163
|
* 对象填充
|
|
162
164
|
* @param {Partial<R>} source
|
|
@@ -164,7 +166,7 @@ const objectAssign = (source, ...targets) => {
|
|
|
164
166
|
* @param {(s: Partial<R>, t: Partial<R>, key: keyof R) => boolean} fillable
|
|
165
167
|
* @returns {R}
|
|
166
168
|
*/
|
|
167
|
-
|
|
169
|
+
function objectFill(source, target, fillable) {
|
|
168
170
|
const _fillable = fillable || ((source, target, key) => source[key] === undefined);
|
|
169
171
|
objectEach(target, (val, key) => {
|
|
170
172
|
if (_fillable(source, target, key)) {
|
|
@@ -172,7 +174,7 @@ const objectFill = (source, target, fillable) => {
|
|
|
172
174
|
}
|
|
173
175
|
});
|
|
174
176
|
return source;
|
|
175
|
-
}
|
|
177
|
+
}
|
|
176
178
|
function objectGet(obj, path, strict = false) {
|
|
177
179
|
path = path.replace(/\[(\w+)\]/g, '.$1');
|
|
178
180
|
path = path.replace(/^\./, '');
|
|
@@ -205,7 +207,7 @@ function objectGet(obj, path, strict = false) {
|
|
|
205
207
|
* 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
|
|
206
208
|
* @param {AnyObject | AnyArray} obj
|
|
207
209
|
* @param {WeakMap} map
|
|
208
|
-
* @
|
|
210
|
+
* @returns {AnyObject | AnyArray}
|
|
209
211
|
*/
|
|
210
212
|
function cloneDeep(obj, map = new WeakMap()) {
|
|
211
213
|
if (obj instanceof Date)
|
package/lib/es/path.js
CHANGED
package/lib/es/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
|
*/
|
|
@@ -12,7 +12,7 @@ import { isUndefined, isArray, isString, isNumber, isBoolean, isDate } from './t
|
|
|
12
12
|
* @param {string} queryString
|
|
13
13
|
* @returns {Params}
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
function qsParse(queryString) {
|
|
16
16
|
const params = new URLSearchParams(queryString);
|
|
17
17
|
const result = {};
|
|
18
18
|
for (const [key, val] of params.entries()) {
|
|
@@ -26,7 +26,7 @@ const qsParse = (queryString) => {
|
|
|
26
26
|
result[key] = params.getAll(key);
|
|
27
27
|
}
|
|
28
28
|
return result;
|
|
29
|
-
}
|
|
29
|
+
}
|
|
30
30
|
const defaultReplacer = (val) => {
|
|
31
31
|
if (isString(val))
|
|
32
32
|
return val;
|
|
@@ -44,7 +44,7 @@ const defaultReplacer = (val) => {
|
|
|
44
44
|
* @param {Replacer} replacer
|
|
45
45
|
* @returns {string}
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
function qsStringify(query, replacer = defaultReplacer) {
|
|
48
48
|
const params = new URLSearchParams();
|
|
49
49
|
objectEach(query, (val, key) => {
|
|
50
50
|
if (isArray(val)) {
|
|
@@ -63,6 +63,6 @@ const qsStringify = (query, replacer = defaultReplacer) => {
|
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
65
|
return params.toString();
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
67
|
|
|
68
68
|
export { qsParse, qsStringify };
|
package/lib/es/random.js
CHANGED
package/lib/es/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
|
*/
|
|
@@ -12,24 +12,24 @@ import { isString } from './type.js';
|
|
|
12
12
|
* @param {boolean} [bigger] 是否大写第一个字母
|
|
13
13
|
* @returns {string}
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
function stringCamelCase(string, bigger) {
|
|
16
16
|
let string2 = string;
|
|
17
17
|
if (bigger) {
|
|
18
18
|
string2 = string.replace(/^./, origin => origin.toUpperCase());
|
|
19
19
|
}
|
|
20
20
|
const HUMP_RE = /[\s_-](.)/g;
|
|
21
21
|
return string2.replace(HUMP_RE, (orign, char) => char.toUpperCase());
|
|
22
|
-
}
|
|
22
|
+
}
|
|
23
23
|
/**
|
|
24
24
|
* 将字符串转换为连字格式
|
|
25
25
|
* @param {string} string
|
|
26
26
|
* @param {string} [separator] 分隔符,默认是"-"(短横线)
|
|
27
27
|
* @returns {string}
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
function stringKebabCase(string, separator = '-') {
|
|
30
30
|
const string2 = string.replace(/^./, origin => origin.toLowerCase());
|
|
31
31
|
return string2.replace(/[A-Z]/g, origin => `${separator}${origin.toLowerCase()}`);
|
|
32
|
-
}
|
|
32
|
+
}
|
|
33
33
|
const STRING_ARABIC_NUMERALS = '0123456789';
|
|
34
34
|
const STRING_LOWERCASE_ALPHA = 'abcdefghijklmnopqrstuvwxyz';
|
|
35
35
|
const STRING_UPPERCASE_ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
@@ -45,7 +45,7 @@ const placeholderRE = /%[%sdo]/g;
|
|
|
45
45
|
* @param args
|
|
46
46
|
* @returns {string}
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
function stringFormat(string, ...args) {
|
|
49
49
|
let index = 0;
|
|
50
50
|
const result = string.replace(placeholderRE, (origin) => {
|
|
51
51
|
const arg = args[index++];
|
|
@@ -63,7 +63,7 @@ const stringFormat = (string, ...args) => {
|
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
65
|
return [result, ...args.splice(index).map(String)].join(' ');
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
67
|
const ev = (expression, data) => {
|
|
68
68
|
try {
|
|
69
69
|
// eslint-disable-next-line @typescript-eslint/no-implied-eval,@typescript-eslint/no-unsafe-return
|
|
@@ -127,7 +127,7 @@ const stringFill = (length, value = ' ') => new Array(length).fill(value).join('
|
|
|
127
127
|
* @param {string} str 目标字符串
|
|
128
128
|
* @param {number} fontSize 字符串字体大小
|
|
129
129
|
* @param {boolean} isRemoveDom 计算后是否移除中间dom元素
|
|
130
|
-
* @
|
|
130
|
+
* @returns {*}
|
|
131
131
|
*/
|
|
132
132
|
function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
|
|
133
133
|
let strWidth = 0;
|