a-js-tools 1.0.13-beta.1 → 1.0.13-beta.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/index.cjs.js +35 -13
- package/index.d.ts +2 -3
- package/package.json +1 -1
- package/src/object/createConstructor.d.ts +2 -0
package/index.cjs.js
CHANGED
|
@@ -49,6 +49,12 @@ function createConstructor(constructor) {
|
|
|
49
49
|
constructor.prototype.toString = Function.toString;
|
|
50
50
|
return constructor;
|
|
51
51
|
}
|
|
52
|
+
/** 对象的 assign 用法 */
|
|
53
|
+
function ObjectAssign(target, bar) {
|
|
54
|
+
const keys = Object.keys(bar);
|
|
55
|
+
keys.forEach(key => (target[key] = bar[key]));
|
|
56
|
+
return target;
|
|
57
|
+
}
|
|
52
58
|
|
|
53
59
|
/**
|
|
54
60
|
* 过去随机数
|
|
@@ -226,9 +232,9 @@ function getRandomString(options) {
|
|
|
226
232
|
return crypto.randomUUID();
|
|
227
233
|
// 验证输入参数
|
|
228
234
|
if (aTypeOfJs.isNumber(options) && Number.isInteger(options) && options > 0)
|
|
229
|
-
|
|
235
|
+
ObjectAssign(initOptions, { length: options });
|
|
230
236
|
if (aTypeOfJs.isPlainObject(options)) {
|
|
231
|
-
|
|
237
|
+
ObjectAssign(initOptions, options);
|
|
232
238
|
initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
|
|
233
239
|
}
|
|
234
240
|
/** 生成随机字符串 */
|
|
@@ -242,13 +248,19 @@ function getRandomString(options) {
|
|
|
242
248
|
// 添加特殊字符
|
|
243
249
|
if (initOptions.includeSpecial)
|
|
244
250
|
interleaveString(templateCharsArr, initOptions.chars3);
|
|
245
|
-
// 使用密码学安全的随机数生成器
|
|
246
|
-
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(initOptions.length));
|
|
247
251
|
let result = '';
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
+
const templateCharsArrLength = templateCharsArr.length;
|
|
253
|
+
if (globalThis && globalThis.crypto && globalThis.crypto.getRandomValues) {
|
|
254
|
+
// 使用密码学安全的随机数生成器
|
|
255
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(initOptions.length));
|
|
256
|
+
/** 获取最后的 chars 数据 */
|
|
257
|
+
// 循环遍历
|
|
258
|
+
bytes.forEach(byte => (result += templateCharsArr[byte % templateCharsArrLength]));
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
for (let i = 0; i < initOptions.length; i++)
|
|
262
|
+
result += templateCharsArr[getRandomInt(templateCharsArrLength - 1)];
|
|
263
|
+
}
|
|
252
264
|
/**
|
|
253
265
|
*
|
|
254
266
|
* 字符串交叉函数
|
|
@@ -322,16 +334,19 @@ function debounce(callback, options = 200) {
|
|
|
322
334
|
timeoutId = undefined;
|
|
323
335
|
}
|
|
324
336
|
};
|
|
337
|
+
const delay = options && options.delay ? options.delay : 5;
|
|
325
338
|
const result = (...args) => {
|
|
326
339
|
clear();
|
|
327
340
|
timeoutId = setTimeout(() => {
|
|
328
341
|
try {
|
|
329
|
-
|
|
342
|
+
const _this = options && options.this ? options.this : null;
|
|
343
|
+
callback.apply(_this, args);
|
|
344
|
+
// Reflect.apply(callback, options?.this ?? null, args);
|
|
330
345
|
}
|
|
331
346
|
catch (error) {
|
|
332
347
|
console.log('Debounce callback throw an error', error);
|
|
333
348
|
}
|
|
334
|
-
}, Math.max(
|
|
349
|
+
}, Math.max(delay, 5));
|
|
335
350
|
};
|
|
336
351
|
result.cancel = () => clear();
|
|
337
352
|
return result;
|
|
@@ -360,11 +375,13 @@ function throttle(callback, options = 200) {
|
|
|
360
375
|
let inThrottle = false;
|
|
361
376
|
/** 延迟控制 */
|
|
362
377
|
let timeoutId = null;
|
|
378
|
+
const delay = options && options.delay ? options.delay : 5;
|
|
363
379
|
const throttled = (...args) => {
|
|
364
380
|
if (inThrottle)
|
|
365
381
|
return;
|
|
366
382
|
try {
|
|
367
|
-
|
|
383
|
+
const _this = options && options.this ? options.this : null;
|
|
384
|
+
callback.apply(_this, args);
|
|
368
385
|
}
|
|
369
386
|
catch (error) {
|
|
370
387
|
console.error('Throttle 执行回调抛出问题', error);
|
|
@@ -375,7 +392,7 @@ function throttle(callback, options = 200) {
|
|
|
375
392
|
timeoutId = setTimeout(() => {
|
|
376
393
|
inThrottle = false;
|
|
377
394
|
timeoutId = null;
|
|
378
|
-
}, Math.max(
|
|
395
|
+
}, Math.max(delay, 5));
|
|
379
396
|
};
|
|
380
397
|
throttled.cancel = () => {
|
|
381
398
|
if (!aTypeOfJs.isNull(timeoutId))
|
|
@@ -481,7 +498,11 @@ function autoEscapedRegExp(pattern, options) {
|
|
|
481
498
|
*
|
|
482
499
|
*/
|
|
483
500
|
function isNode() {
|
|
484
|
-
return !aTypeOfJs.isUndefined(globalThis
|
|
501
|
+
return !aTypeOfJs.isUndefined((globalThis &&
|
|
502
|
+
globalThis.process &&
|
|
503
|
+
globalThis.process.versions &&
|
|
504
|
+
globalThis.process.versions.node) ||
|
|
505
|
+
undefined);
|
|
485
506
|
}
|
|
486
507
|
/**
|
|
487
508
|
*
|
|
@@ -847,6 +868,7 @@ async function sleep(delay = 1000) {
|
|
|
847
868
|
return new Promise(resolve => setTimeout(resolve, delay));
|
|
848
869
|
}
|
|
849
870
|
|
|
871
|
+
exports.ObjectAssign = ObjectAssign;
|
|
850
872
|
exports.autoEscapedRegExp = autoEscapedRegExp;
|
|
851
873
|
exports.createConstructor = createConstructor;
|
|
852
874
|
exports.debounce = debounce;
|
package/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
export type { CreateConstructor } from './src/object/createConstructor';
|
|
2
|
+
export { createConstructor, ObjectAssign, } from './src/object/createConstructor';
|
|
2
3
|
export { toLowerCamelCase, toSplitCase, getRandomFloat, getRandomInt, getRandomString, } from './src/index';
|
|
3
4
|
export { throttle, debounce } from './src/performance';
|
|
4
5
|
export type { DebounceAndThrottleReturnType } from './src/performance';
|
|
5
6
|
export { escapeRegExp, autoEscapedRegExp } from './src/regexp';
|
|
6
7
|
export { isBrowser, isNode } from './src/isNode';
|
|
7
8
|
export { intersection, enArr, union, difference, symmetricDifference, } from './src/array/';
|
|
8
|
-
export { createConstructor };
|
|
9
|
-
export type { CreateConstructor };
|
|
10
9
|
export { sleep } from './src/sleep';
|
package/package.json
CHANGED
|
@@ -45,3 +45,5 @@ export interface CreateConstructor<T, Args extends unknown[] = unknown[]> {
|
|
|
45
45
|
*
|
|
46
46
|
*/
|
|
47
47
|
export declare function createConstructor<T, Args extends unknown[] = unknown[]>(constructor: (...argumentList: Args) => T): CreateConstructor<T, Args>;
|
|
48
|
+
/** 对象的 assign 用法 */
|
|
49
|
+
export declare function ObjectAssign(target: Record<string, unknown>, bar: Record<string, unknown>): Record<string, unknown>;
|