devix 0.1.2 → 1.0.3
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 +2 -2
- package/dist/index.cjs +314 -0
- package/dist/index.d.cts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +300 -0
- package/package.json +32 -63
- package/dist/index.cjs.js +0 -309
- package/dist/index.esm.js +0 -295
- package/index.d.ts +0 -53
package/dist/index.cjs.js
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function getDataType(target) {
|
|
4
|
-
const type = typeof target;
|
|
5
|
-
return type !== "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
|
|
6
|
-
}
|
|
7
|
-
const typeCheckers = {
|
|
8
|
-
// DataType
|
|
9
|
-
undefined: (target) => typeof target === "undefined",
|
|
10
|
-
null: (target) => target === null,
|
|
11
|
-
number: (target) => typeof target === "number",
|
|
12
|
-
string: (target) => typeof target === "string",
|
|
13
|
-
boolean: (target) => typeof target === "boolean",
|
|
14
|
-
symbol: (target) => typeof target === "symbol",
|
|
15
|
-
bigint: (target) => typeof target === "bigint",
|
|
16
|
-
// ObjectType
|
|
17
|
-
object: (target) => target !== null && typeof target === "object",
|
|
18
|
-
array: (target) => Array.isArray(target),
|
|
19
|
-
date: (target) => target instanceof Date,
|
|
20
|
-
function: (target) => typeof target === "function",
|
|
21
|
-
set: (target) => target instanceof Set,
|
|
22
|
-
map: (target) => target instanceof Map,
|
|
23
|
-
regexp: (target) => target instanceof RegExp,
|
|
24
|
-
promise: (target) => target instanceof Promise,
|
|
25
|
-
// ExpandType
|
|
26
|
-
empty: (target) => !(target !== null && target !== "" && typeof target !== "undefined")
|
|
27
|
-
};
|
|
28
|
-
const isType = (type, target) => {
|
|
29
|
-
const dataType = typeCheckers[type] ? typeCheckers[type](target) : getDataType(target) === type;
|
|
30
|
-
return dataType;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function currying(fn) {
|
|
34
|
-
function curried(...args) {
|
|
35
|
-
if (args.length >= fn.length) return fn.apply(this, args);
|
|
36
|
-
return function subCurried(...args2) {
|
|
37
|
-
return curried.apply(this, args.concat(args2));
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
return curried;
|
|
41
|
-
}
|
|
42
|
-
function compose(...fns) {
|
|
43
|
-
const {
|
|
44
|
-
length
|
|
45
|
-
} = fns;
|
|
46
|
-
if (length <= 0) return null;
|
|
47
|
-
for (let i = 0; i < length; i++) {
|
|
48
|
-
const fn = fns[i];
|
|
49
|
-
if (typeof fn !== "function") {
|
|
50
|
-
throw new Error(`argument with index ${i} is not a function`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function executeFn(...args) {
|
|
54
|
-
let index = 0;
|
|
55
|
-
let result = fns[index].apply(this, args);
|
|
56
|
-
while (++index < length) {
|
|
57
|
-
result = fns[index].call(this, result);
|
|
58
|
-
}
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
return executeFn;
|
|
62
|
-
}
|
|
63
|
-
function insertStr(soure, start, newStr) {
|
|
64
|
-
return soure.slice(0, start) + newStr + soure.slice(start);
|
|
65
|
-
}
|
|
66
|
-
function setCaseType(soure, caseType) {
|
|
67
|
-
const newStr = soure.slice(0, 1)[caseType === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
|
|
68
|
-
return newStr;
|
|
69
|
-
}
|
|
70
|
-
function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
|
|
71
|
-
const [separator, separate] = separa;
|
|
72
|
-
const [firstCase, argsCase] = cases;
|
|
73
|
-
const newStr = soure.split(separator);
|
|
74
|
-
for (let i = 0; i < newStr.length; i++) {
|
|
75
|
-
newStr[i] = setCaseType(newStr[i], i === 0 ? firstCase : argsCase);
|
|
76
|
-
}
|
|
77
|
-
return newStr.join(separate);
|
|
78
|
-
}
|
|
79
|
-
const transformGetParams = (params) => {
|
|
80
|
-
let result = "";
|
|
81
|
-
for (const propName of Object.keys(params)) {
|
|
82
|
-
const value = params[propName];
|
|
83
|
-
const part = `${encodeURIComponent(propName)}=`;
|
|
84
|
-
if (isType("empty", value)) continue;
|
|
85
|
-
if (!isType("object", value)) {
|
|
86
|
-
result += `${part + encodeURIComponent(value)}&`;
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
for (const key of Object.keys(value)) {
|
|
90
|
-
if (!isType("empty", value)) continue;
|
|
91
|
-
const paramsStr = `${propName}[${key}]`;
|
|
92
|
-
const subPart = `${encodeURIComponent(paramsStr)}=`;
|
|
93
|
-
result += `${subPart + encodeURIComponent(value[key])}&`;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return result.slice(0, -1);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
function debounce(callback, delay = 0, immediate = false) {
|
|
100
|
-
let timer = null;
|
|
101
|
-
let isInvoke = false;
|
|
102
|
-
function subDebounce(...args) {
|
|
103
|
-
return new Promise((resolve, reject) => {
|
|
104
|
-
if (timer !== null) clearTimeout(timer);
|
|
105
|
-
if (immediate && !isInvoke) {
|
|
106
|
-
try {
|
|
107
|
-
const result = callback.apply(this, args);
|
|
108
|
-
resolve(result);
|
|
109
|
-
} catch (error) {
|
|
110
|
-
reject(error);
|
|
111
|
-
}
|
|
112
|
-
isInvoke = true;
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
timer = setTimeout(() => {
|
|
116
|
-
try {
|
|
117
|
-
const result = callback.apply(this, args);
|
|
118
|
-
resolve(result);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
reject(error);
|
|
121
|
-
} finally {
|
|
122
|
-
timer = null;
|
|
123
|
-
isInvoke = false;
|
|
124
|
-
}
|
|
125
|
-
}, delay);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
subDebounce.cancel = () => {
|
|
129
|
-
if (timer !== null) clearTimeout(timer);
|
|
130
|
-
timer = null;
|
|
131
|
-
isInvoke = false;
|
|
132
|
-
};
|
|
133
|
-
return subDebounce;
|
|
134
|
-
}
|
|
135
|
-
function throttle(callback, interval, options = {}) {
|
|
136
|
-
const {
|
|
137
|
-
leading = true,
|
|
138
|
-
trailing = false
|
|
139
|
-
} = options;
|
|
140
|
-
let startTime = 0;
|
|
141
|
-
let timer = null;
|
|
142
|
-
function subThrottle(...args) {
|
|
143
|
-
return new Promise((resolve, reject) => {
|
|
144
|
-
try {
|
|
145
|
-
const nowTime = Date.now();
|
|
146
|
-
let result;
|
|
147
|
-
if (!leading && startTime === 0) startTime = nowTime;
|
|
148
|
-
const waitTime = interval - (nowTime - startTime);
|
|
149
|
-
if (waitTime <= 0) {
|
|
150
|
-
if (timer) clearTimeout(timer);
|
|
151
|
-
result = callback.apply(this, args);
|
|
152
|
-
resolve(result);
|
|
153
|
-
startTime = nowTime;
|
|
154
|
-
timer = null;
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (trailing && !timer) {
|
|
158
|
-
timer = setTimeout(() => {
|
|
159
|
-
result = callback.apply(this, args);
|
|
160
|
-
resolve(result);
|
|
161
|
-
startTime = Date.now();
|
|
162
|
-
timer = null;
|
|
163
|
-
}, waitTime);
|
|
164
|
-
}
|
|
165
|
-
} catch (error) {
|
|
166
|
-
reject(error);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
subThrottle.cancel = () => {
|
|
171
|
-
if (timer) clearTimeout(timer);
|
|
172
|
-
startTime = 0;
|
|
173
|
-
timer = null;
|
|
174
|
-
};
|
|
175
|
-
return subThrottle;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
//! Function Shallow Copy
|
|
179
|
-
function shallowClone(source) {
|
|
180
|
-
if (isType("array", source)) return source.slice();
|
|
181
|
-
if (isType("object", source)) return {
|
|
182
|
-
...source
|
|
183
|
-
};
|
|
184
|
-
return source;
|
|
185
|
-
}
|
|
186
|
-
//! Function Deep Copy
|
|
187
|
-
const isFormat = (target) => isType("object", target) || isType("function", target);
|
|
188
|
-
function handleSpeciBoundar(source, dpClone, hash) {
|
|
189
|
-
if (isType("symbol", source)) return Symbol(source.description);
|
|
190
|
-
if (!isFormat(source)) return source;
|
|
191
|
-
if (isType("set", source)) {
|
|
192
|
-
const newSet = /* @__PURE__ */ new Set();
|
|
193
|
-
source.forEach((value) => newSet.add(dpClone(value, hash)));
|
|
194
|
-
return newSet;
|
|
195
|
-
}
|
|
196
|
-
if (isType("map", source)) {
|
|
197
|
-
const newMap = /* @__PURE__ */ new Map();
|
|
198
|
-
source.forEach((value, key) => newMap.set(key, dpClone(value, hash)));
|
|
199
|
-
return newMap;
|
|
200
|
-
}
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
function deepClone(source, hash = /* @__PURE__ */ new WeakMap()) {
|
|
204
|
-
if (hash.get(source)) return hash.get(source);
|
|
205
|
-
const result = handleSpeciBoundar(source, deepClone, hash);
|
|
206
|
-
if (result) return result;
|
|
207
|
-
const isArray = isType("array", source);
|
|
208
|
-
const cloneObject = isArray ? [] : {};
|
|
209
|
-
hash.set(source, cloneObject);
|
|
210
|
-
if (isArray) {
|
|
211
|
-
source.forEach((item, index) => {
|
|
212
|
-
cloneObject[index] = deepClone(item, hash);
|
|
213
|
-
});
|
|
214
|
-
} else {
|
|
215
|
-
Object.keys(source).forEach((key) => {
|
|
216
|
-
cloneObject[key] = deepClone(source[key], hash);
|
|
217
|
-
});
|
|
218
|
-
Object.getOwnPropertySymbols(source).forEach((sym) => {
|
|
219
|
-
cloneObject[Symbol(sym.description)] = deepClone(source[sym], hash);
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
return cloneObject;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
exports.SortType = void 0;
|
|
226
|
-
(function(SortType2) {
|
|
227
|
-
SortType2["ASC"] = "ASC";
|
|
228
|
-
SortType2["DESC"] = "DESC";
|
|
229
|
-
})(exports.SortType || (exports.SortType = {}));
|
|
230
|
-
function swap(array, index1, index2) {
|
|
231
|
-
[array[index1], array[index2]] = [array[index2], array[index1]];
|
|
232
|
-
}
|
|
233
|
-
function compare(value1, value2, type) {
|
|
234
|
-
return type === exports.SortType.ASC ? value1 > value2 : value1 < value2;
|
|
235
|
-
}
|
|
236
|
-
function bubblingSort(array, type = "ASC", key) {
|
|
237
|
-
const {
|
|
238
|
-
length
|
|
239
|
-
} = array;
|
|
240
|
-
if (length < 2) return array;
|
|
241
|
-
for (let i = 0; i < length - 1; i++) {
|
|
242
|
-
for (let j = 0; j < length - 1 - i; j++) {
|
|
243
|
-
const value1 = key ? array[j][key] : array[j];
|
|
244
|
-
const value2 = key ? array[j + 1][key] : array[j + 1];
|
|
245
|
-
if (compare(value1, value2, type)) swap(array, j, j + 1);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return array;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const formatRules = /* @__PURE__ */ new Map([["yyyy", "year"], ["MM", "month"], ["dd", "day"], ["HH", "hours"], ["mm", "minutes"], ["ss", "seconds"], ["W", "week"]]);
|
|
252
|
-
const WeekList = /* @__PURE__ */ new Map([[1, "\u4E00"], [2, "\u4E8C"], [3, "\u4E09"], [4, "\u56DB"], [5, "\u4E94"], [6, "\u516D"], [0, "\u65E5"]]);
|
|
253
|
-
function processWeek(weekNum) {
|
|
254
|
-
return WeekList.get(weekNum) || "";
|
|
255
|
-
}
|
|
256
|
-
function formatNumber(value) {
|
|
257
|
-
return value.toString().padStart(2, "0");
|
|
258
|
-
}
|
|
259
|
-
function createTimerObj(date) {
|
|
260
|
-
const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay();
|
|
261
|
-
return {
|
|
262
|
-
year: date.getFullYear().toString(),
|
|
263
|
-
month: formatNumber(date.getMonth() + 1),
|
|
264
|
-
day: formatNumber(date.getDate()),
|
|
265
|
-
hours: formatNumber(date.getHours()),
|
|
266
|
-
minutes: formatNumber(date.getMinutes()),
|
|
267
|
-
seconds: formatNumber(date.getSeconds()),
|
|
268
|
-
week: processWeek(dayOfWeek),
|
|
269
|
-
weekNum: dayOfWeek.toString()
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
const formatTimer = (cellValue, formatType = "yyyy-MM-dd HH:mm:ss") => {
|
|
273
|
-
if (!cellValue) return (/* @__PURE__ */ new Date()).toISOString();
|
|
274
|
-
const date = new Date(cellValue);
|
|
275
|
-
const timerObj = createTimerObj(date);
|
|
276
|
-
if (isType("string", formatType) && !formatType.trim()) return timerObj;
|
|
277
|
-
const timerStr = Array.from(formatRules).reduce((currentFormat, [rule, key]) => {
|
|
278
|
-
return currentFormat.replace(new RegExp(rule, "g"), timerObj[key]);
|
|
279
|
-
}, formatType);
|
|
280
|
-
return timerStr;
|
|
281
|
-
};
|
|
282
|
-
async function setTimer(execute, delay = 0, immediate = false) {
|
|
283
|
-
let timer = null;
|
|
284
|
-
const interval = async () => {
|
|
285
|
-
await execute();
|
|
286
|
-
timer = setTimeout(interval, delay);
|
|
287
|
-
};
|
|
288
|
-
if (immediate) await execute();
|
|
289
|
-
setTimeout(interval, delay);
|
|
290
|
-
return {
|
|
291
|
-
cancel: () => {
|
|
292
|
-
if (timer !== null) clearTimeout(timer);
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
exports.bubblingSort = bubblingSort;
|
|
298
|
-
exports.compose = compose;
|
|
299
|
-
exports.currying = currying;
|
|
300
|
-
exports.debounce = debounce;
|
|
301
|
-
exports.deepClone = deepClone;
|
|
302
|
-
exports.formatTimer = formatTimer;
|
|
303
|
-
exports.insertStr = insertStr;
|
|
304
|
-
exports.isType = isType;
|
|
305
|
-
exports.setTimer = setTimer;
|
|
306
|
-
exports.shallowClone = shallowClone;
|
|
307
|
-
exports.stringCase = stringCase;
|
|
308
|
-
exports.throttle = throttle;
|
|
309
|
-
exports.transformGetParams = transformGetParams;
|
package/dist/index.esm.js
DELETED
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
function getDataType(target) {
|
|
2
|
-
const type = typeof target;
|
|
3
|
-
return type !== "object" ? type : Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
|
|
4
|
-
}
|
|
5
|
-
const typeCheckers = {
|
|
6
|
-
// DataType
|
|
7
|
-
undefined: (target) => typeof target === "undefined",
|
|
8
|
-
null: (target) => target === null,
|
|
9
|
-
number: (target) => typeof target === "number",
|
|
10
|
-
string: (target) => typeof target === "string",
|
|
11
|
-
boolean: (target) => typeof target === "boolean",
|
|
12
|
-
symbol: (target) => typeof target === "symbol",
|
|
13
|
-
bigint: (target) => typeof target === "bigint",
|
|
14
|
-
// ObjectType
|
|
15
|
-
object: (target) => target !== null && typeof target === "object",
|
|
16
|
-
array: (target) => Array.isArray(target),
|
|
17
|
-
date: (target) => target instanceof Date,
|
|
18
|
-
function: (target) => typeof target === "function",
|
|
19
|
-
set: (target) => target instanceof Set,
|
|
20
|
-
map: (target) => target instanceof Map,
|
|
21
|
-
regexp: (target) => target instanceof RegExp,
|
|
22
|
-
promise: (target) => target instanceof Promise,
|
|
23
|
-
// ExpandType
|
|
24
|
-
empty: (target) => !(target !== null && target !== "" && typeof target !== "undefined")
|
|
25
|
-
};
|
|
26
|
-
const isType = (type, target) => {
|
|
27
|
-
const dataType = typeCheckers[type] ? typeCheckers[type](target) : getDataType(target) === type;
|
|
28
|
-
return dataType;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
function currying(fn) {
|
|
32
|
-
function curried(...args) {
|
|
33
|
-
if (args.length >= fn.length) return fn.apply(this, args);
|
|
34
|
-
return function subCurried(...args2) {
|
|
35
|
-
return curried.apply(this, args.concat(args2));
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return curried;
|
|
39
|
-
}
|
|
40
|
-
function compose(...fns) {
|
|
41
|
-
const {
|
|
42
|
-
length
|
|
43
|
-
} = fns;
|
|
44
|
-
if (length <= 0) return null;
|
|
45
|
-
for (let i = 0; i < length; i++) {
|
|
46
|
-
const fn = fns[i];
|
|
47
|
-
if (typeof fn !== "function") {
|
|
48
|
-
throw new Error(`argument with index ${i} is not a function`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function executeFn(...args) {
|
|
52
|
-
let index = 0;
|
|
53
|
-
let result = fns[index].apply(this, args);
|
|
54
|
-
while (++index < length) {
|
|
55
|
-
result = fns[index].call(this, result);
|
|
56
|
-
}
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
59
|
-
return executeFn;
|
|
60
|
-
}
|
|
61
|
-
function insertStr(soure, start, newStr) {
|
|
62
|
-
return soure.slice(0, start) + newStr + soure.slice(start);
|
|
63
|
-
}
|
|
64
|
-
function setCaseType(soure, caseType) {
|
|
65
|
-
const newStr = soure.slice(0, 1)[caseType === "upper" ? "toUpperCase" : "toLowerCase"]() + soure.slice(1).toLowerCase();
|
|
66
|
-
return newStr;
|
|
67
|
-
}
|
|
68
|
-
function stringCase(soure, separa = ["", ""], cases = ["upper", "upper"]) {
|
|
69
|
-
const [separator, separate] = separa;
|
|
70
|
-
const [firstCase, argsCase] = cases;
|
|
71
|
-
const newStr = soure.split(separator);
|
|
72
|
-
for (let i = 0; i < newStr.length; i++) {
|
|
73
|
-
newStr[i] = setCaseType(newStr[i], i === 0 ? firstCase : argsCase);
|
|
74
|
-
}
|
|
75
|
-
return newStr.join(separate);
|
|
76
|
-
}
|
|
77
|
-
const transformGetParams = (params) => {
|
|
78
|
-
let result = "";
|
|
79
|
-
for (const propName of Object.keys(params)) {
|
|
80
|
-
const value = params[propName];
|
|
81
|
-
const part = `${encodeURIComponent(propName)}=`;
|
|
82
|
-
if (isType("empty", value)) continue;
|
|
83
|
-
if (!isType("object", value)) {
|
|
84
|
-
result += `${part + encodeURIComponent(value)}&`;
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
for (const key of Object.keys(value)) {
|
|
88
|
-
if (!isType("empty", value)) continue;
|
|
89
|
-
const paramsStr = `${propName}[${key}]`;
|
|
90
|
-
const subPart = `${encodeURIComponent(paramsStr)}=`;
|
|
91
|
-
result += `${subPart + encodeURIComponent(value[key])}&`;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return result.slice(0, -1);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
function debounce(callback, delay = 0, immediate = false) {
|
|
98
|
-
let timer = null;
|
|
99
|
-
let isInvoke = false;
|
|
100
|
-
function subDebounce(...args) {
|
|
101
|
-
return new Promise((resolve, reject) => {
|
|
102
|
-
if (timer !== null) clearTimeout(timer);
|
|
103
|
-
if (immediate && !isInvoke) {
|
|
104
|
-
try {
|
|
105
|
-
const result = callback.apply(this, args);
|
|
106
|
-
resolve(result);
|
|
107
|
-
} catch (error) {
|
|
108
|
-
reject(error);
|
|
109
|
-
}
|
|
110
|
-
isInvoke = true;
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
timer = setTimeout(() => {
|
|
114
|
-
try {
|
|
115
|
-
const result = callback.apply(this, args);
|
|
116
|
-
resolve(result);
|
|
117
|
-
} catch (error) {
|
|
118
|
-
reject(error);
|
|
119
|
-
} finally {
|
|
120
|
-
timer = null;
|
|
121
|
-
isInvoke = false;
|
|
122
|
-
}
|
|
123
|
-
}, delay);
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
subDebounce.cancel = () => {
|
|
127
|
-
if (timer !== null) clearTimeout(timer);
|
|
128
|
-
timer = null;
|
|
129
|
-
isInvoke = false;
|
|
130
|
-
};
|
|
131
|
-
return subDebounce;
|
|
132
|
-
}
|
|
133
|
-
function throttle(callback, interval, options = {}) {
|
|
134
|
-
const {
|
|
135
|
-
leading = true,
|
|
136
|
-
trailing = false
|
|
137
|
-
} = options;
|
|
138
|
-
let startTime = 0;
|
|
139
|
-
let timer = null;
|
|
140
|
-
function subThrottle(...args) {
|
|
141
|
-
return new Promise((resolve, reject) => {
|
|
142
|
-
try {
|
|
143
|
-
const nowTime = Date.now();
|
|
144
|
-
let result;
|
|
145
|
-
if (!leading && startTime === 0) startTime = nowTime;
|
|
146
|
-
const waitTime = interval - (nowTime - startTime);
|
|
147
|
-
if (waitTime <= 0) {
|
|
148
|
-
if (timer) clearTimeout(timer);
|
|
149
|
-
result = callback.apply(this, args);
|
|
150
|
-
resolve(result);
|
|
151
|
-
startTime = nowTime;
|
|
152
|
-
timer = null;
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
if (trailing && !timer) {
|
|
156
|
-
timer = setTimeout(() => {
|
|
157
|
-
result = callback.apply(this, args);
|
|
158
|
-
resolve(result);
|
|
159
|
-
startTime = Date.now();
|
|
160
|
-
timer = null;
|
|
161
|
-
}, waitTime);
|
|
162
|
-
}
|
|
163
|
-
} catch (error) {
|
|
164
|
-
reject(error);
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
subThrottle.cancel = () => {
|
|
169
|
-
if (timer) clearTimeout(timer);
|
|
170
|
-
startTime = 0;
|
|
171
|
-
timer = null;
|
|
172
|
-
};
|
|
173
|
-
return subThrottle;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
//! Function Shallow Copy
|
|
177
|
-
function shallowClone(source) {
|
|
178
|
-
if (isType("array", source)) return source.slice();
|
|
179
|
-
if (isType("object", source)) return {
|
|
180
|
-
...source
|
|
181
|
-
};
|
|
182
|
-
return source;
|
|
183
|
-
}
|
|
184
|
-
//! Function Deep Copy
|
|
185
|
-
const isFormat = (target) => isType("object", target) || isType("function", target);
|
|
186
|
-
function handleSpeciBoundar(source, dpClone, hash) {
|
|
187
|
-
if (isType("symbol", source)) return Symbol(source.description);
|
|
188
|
-
if (!isFormat(source)) return source;
|
|
189
|
-
if (isType("set", source)) {
|
|
190
|
-
const newSet = /* @__PURE__ */ new Set();
|
|
191
|
-
source.forEach((value) => newSet.add(dpClone(value, hash)));
|
|
192
|
-
return newSet;
|
|
193
|
-
}
|
|
194
|
-
if (isType("map", source)) {
|
|
195
|
-
const newMap = /* @__PURE__ */ new Map();
|
|
196
|
-
source.forEach((value, key) => newMap.set(key, dpClone(value, hash)));
|
|
197
|
-
return newMap;
|
|
198
|
-
}
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
function deepClone(source, hash = /* @__PURE__ */ new WeakMap()) {
|
|
202
|
-
if (hash.get(source)) return hash.get(source);
|
|
203
|
-
const result = handleSpeciBoundar(source, deepClone, hash);
|
|
204
|
-
if (result) return result;
|
|
205
|
-
const isArray = isType("array", source);
|
|
206
|
-
const cloneObject = isArray ? [] : {};
|
|
207
|
-
hash.set(source, cloneObject);
|
|
208
|
-
if (isArray) {
|
|
209
|
-
source.forEach((item, index) => {
|
|
210
|
-
cloneObject[index] = deepClone(item, hash);
|
|
211
|
-
});
|
|
212
|
-
} else {
|
|
213
|
-
Object.keys(source).forEach((key) => {
|
|
214
|
-
cloneObject[key] = deepClone(source[key], hash);
|
|
215
|
-
});
|
|
216
|
-
Object.getOwnPropertySymbols(source).forEach((sym) => {
|
|
217
|
-
cloneObject[Symbol(sym.description)] = deepClone(source[sym], hash);
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
return cloneObject;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
var SortType;
|
|
224
|
-
(function(SortType2) {
|
|
225
|
-
SortType2["ASC"] = "ASC";
|
|
226
|
-
SortType2["DESC"] = "DESC";
|
|
227
|
-
})(SortType || (SortType = {}));
|
|
228
|
-
function swap(array, index1, index2) {
|
|
229
|
-
[array[index1], array[index2]] = [array[index2], array[index1]];
|
|
230
|
-
}
|
|
231
|
-
function compare(value1, value2, type) {
|
|
232
|
-
return type === SortType.ASC ? value1 > value2 : value1 < value2;
|
|
233
|
-
}
|
|
234
|
-
function bubblingSort(array, type = "ASC", key) {
|
|
235
|
-
const {
|
|
236
|
-
length
|
|
237
|
-
} = array;
|
|
238
|
-
if (length < 2) return array;
|
|
239
|
-
for (let i = 0; i < length - 1; i++) {
|
|
240
|
-
for (let j = 0; j < length - 1 - i; j++) {
|
|
241
|
-
const value1 = key ? array[j][key] : array[j];
|
|
242
|
-
const value2 = key ? array[j + 1][key] : array[j + 1];
|
|
243
|
-
if (compare(value1, value2, type)) swap(array, j, j + 1);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return array;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const formatRules = /* @__PURE__ */ new Map([["yyyy", "year"], ["MM", "month"], ["dd", "day"], ["HH", "hours"], ["mm", "minutes"], ["ss", "seconds"], ["W", "week"]]);
|
|
250
|
-
const WeekList = /* @__PURE__ */ new Map([[1, "\u4E00"], [2, "\u4E8C"], [3, "\u4E09"], [4, "\u56DB"], [5, "\u4E94"], [6, "\u516D"], [0, "\u65E5"]]);
|
|
251
|
-
function processWeek(weekNum) {
|
|
252
|
-
return WeekList.get(weekNum) || "";
|
|
253
|
-
}
|
|
254
|
-
function formatNumber(value) {
|
|
255
|
-
return value.toString().padStart(2, "0");
|
|
256
|
-
}
|
|
257
|
-
function createTimerObj(date) {
|
|
258
|
-
const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay();
|
|
259
|
-
return {
|
|
260
|
-
year: date.getFullYear().toString(),
|
|
261
|
-
month: formatNumber(date.getMonth() + 1),
|
|
262
|
-
day: formatNumber(date.getDate()),
|
|
263
|
-
hours: formatNumber(date.getHours()),
|
|
264
|
-
minutes: formatNumber(date.getMinutes()),
|
|
265
|
-
seconds: formatNumber(date.getSeconds()),
|
|
266
|
-
week: processWeek(dayOfWeek),
|
|
267
|
-
weekNum: dayOfWeek.toString()
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
const formatTimer = (cellValue, formatType = "yyyy-MM-dd HH:mm:ss") => {
|
|
271
|
-
if (!cellValue) return (/* @__PURE__ */ new Date()).toISOString();
|
|
272
|
-
const date = new Date(cellValue);
|
|
273
|
-
const timerObj = createTimerObj(date);
|
|
274
|
-
if (isType("string", formatType) && !formatType.trim()) return timerObj;
|
|
275
|
-
const timerStr = Array.from(formatRules).reduce((currentFormat, [rule, key]) => {
|
|
276
|
-
return currentFormat.replace(new RegExp(rule, "g"), timerObj[key]);
|
|
277
|
-
}, formatType);
|
|
278
|
-
return timerStr;
|
|
279
|
-
};
|
|
280
|
-
async function setTimer(execute, delay = 0, immediate = false) {
|
|
281
|
-
let timer = null;
|
|
282
|
-
const interval = async () => {
|
|
283
|
-
await execute();
|
|
284
|
-
timer = setTimeout(interval, delay);
|
|
285
|
-
};
|
|
286
|
-
if (immediate) await execute();
|
|
287
|
-
setTimeout(interval, delay);
|
|
288
|
-
return {
|
|
289
|
-
cancel: () => {
|
|
290
|
-
if (timer !== null) clearTimeout(timer);
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export { SortType, bubblingSort, compose, currying, debounce, deepClone, formatTimer, insertStr, isType, setTimer, shallowClone, stringCase, throttle, transformGetParams };
|
package/index.d.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
interface ITimerObj {
|
|
2
|
-
year: string;
|
|
3
|
-
month: string;
|
|
4
|
-
day: string;
|
|
5
|
-
hours: string;
|
|
6
|
-
minutes: string;
|
|
7
|
-
seconds: string;
|
|
8
|
-
week: string;
|
|
9
|
-
weekNum: string;
|
|
10
|
-
}
|
|
11
|
-
type TFormatTimer = (cellValue: string | number | Date, formatType?: string) => string | ITimerObj;
|
|
12
|
-
type ThrottleOptions = {
|
|
13
|
-
leading?: boolean;
|
|
14
|
-
trailing?: boolean;
|
|
15
|
-
};
|
|
16
|
-
type TIsType = (type: string, target: any) => boolean;
|
|
17
|
-
type Tcase = 'upper' | 'lower';
|
|
18
|
-
type TCases = [Tcase, Tcase];
|
|
19
|
-
type TTransGetParams = (params: IDataObject) => string;
|
|
20
|
-
interface IDataObject {
|
|
21
|
-
[key: string]: any;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
declare function currying(fn: (...args: any[]) => any): (this: any, ...args: any[]) => any;
|
|
25
|
-
declare function compose(...fns: ((...args: any[]) => any)[]): ((this: any, ...args: any[]) => any) | null;
|
|
26
|
-
declare function insertStr(soure: string, start: number, newStr: string): string;
|
|
27
|
-
declare function stringCase(soure: string, separa?: string[], cases?: TCases): string;
|
|
28
|
-
declare const transformGetParams: TTransGetParams;
|
|
29
|
-
|
|
30
|
-
declare const isType: TIsType;
|
|
31
|
-
|
|
32
|
-
declare function debounce<T extends (...args: any[]) => any>(callback: T, delay?: number, immediate?: boolean): ((...args: Parameters<T>) => Promise<ReturnType<T>>) & {
|
|
33
|
-
cancel: () => void;
|
|
34
|
-
};
|
|
35
|
-
declare function throttle<T extends (...args: any[]) => any>(callback: T, interval: number, options?: ThrottleOptions): ((...args: Parameters<T>) => Promise<ReturnType<T>>) & {
|
|
36
|
-
cancel: () => void;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
declare function shallowClone<T = any>(source: T): T;
|
|
40
|
-
declare function deepClone(source: any, hash?: WeakMap<object, any>): any;
|
|
41
|
-
|
|
42
|
-
declare enum SortType {
|
|
43
|
-
ASC = "ASC",
|
|
44
|
-
DESC = "DESC"
|
|
45
|
-
}
|
|
46
|
-
declare function bubblingSort<T>(array: T[], type?: string, key?: keyof T): T[];
|
|
47
|
-
|
|
48
|
-
declare const formatTimer: TFormatTimer;
|
|
49
|
-
declare function setTimer(execute: (...args: any[]) => any, delay?: number, immediate?: boolean): Promise<{
|
|
50
|
-
cancel: () => void;
|
|
51
|
-
}>;
|
|
52
|
-
|
|
53
|
-
export { SortType, bubblingSort, compose, currying, debounce, deepClone, formatTimer, insertStr, isType, setTimer, shallowClone, stringCase, throttle, transformGetParams };
|