a-js-tools 1.0.13-beta.1 → 1.0.13-beta.2

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 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
- Object.assign(initOptions, { length: options });
235
+ ObjectAssign(initOptions, { length: options });
230
236
  if (aTypeOfJs.isPlainObject(options)) {
231
- Object.assign(initOptions, options);
237
+ ObjectAssign(initOptions, options);
232
238
  initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
233
239
  }
234
240
  /** 生成随机字符串 */
@@ -322,16 +328,19 @@ function debounce(callback, options = 200) {
322
328
  timeoutId = undefined;
323
329
  }
324
330
  };
331
+ const delay = options && options.delay ? options.delay : 5;
325
332
  const result = (...args) => {
326
333
  clear();
327
334
  timeoutId = setTimeout(() => {
328
335
  try {
329
- Reflect.apply(callback, options?.this ?? null, args);
336
+ const _this = options && options.this ? options.this : null;
337
+ callback.apply(_this, args);
338
+ // Reflect.apply(callback, options?.this ?? null, args);
330
339
  }
331
340
  catch (error) {
332
341
  console.log('Debounce callback throw an error', error);
333
342
  }
334
- }, Math.max(options?.delay ?? 5, 5));
343
+ }, Math.max(delay, 5));
335
344
  };
336
345
  result.cancel = () => clear();
337
346
  return result;
@@ -360,11 +369,13 @@ function throttle(callback, options = 200) {
360
369
  let inThrottle = false;
361
370
  /** 延迟控制 */
362
371
  let timeoutId = null;
372
+ const delay = options && options.delay ? options.delay : 5;
363
373
  const throttled = (...args) => {
364
374
  if (inThrottle)
365
375
  return;
366
376
  try {
367
- Reflect.apply(callback, options?.this ?? null, args);
377
+ const _this = options && options.this ? options.this : null;
378
+ callback.apply(_this, args);
368
379
  }
369
380
  catch (error) {
370
381
  console.error('Throttle 执行回调抛出问题', error);
@@ -375,7 +386,7 @@ function throttle(callback, options = 200) {
375
386
  timeoutId = setTimeout(() => {
376
387
  inThrottle = false;
377
388
  timeoutId = null;
378
- }, Math.max(options?.delay ?? 5, 5));
389
+ }, Math.max(delay, 5));
379
390
  };
380
391
  throttled.cancel = () => {
381
392
  if (!aTypeOfJs.isNull(timeoutId))
@@ -481,7 +492,11 @@ function autoEscapedRegExp(pattern, options) {
481
492
  *
482
493
  */
483
494
  function isNode() {
484
- return !aTypeOfJs.isUndefined(globalThis?.process?.versions?.node);
495
+ return !aTypeOfJs.isUndefined((globalThis &&
496
+ globalThis.process &&
497
+ globalThis.process.versions &&
498
+ globalThis.process.versions.node) ||
499
+ undefined);
485
500
  }
486
501
  /**
487
502
  *
@@ -847,6 +862,7 @@ async function sleep(delay = 1000) {
847
862
  return new Promise(resolve => setTimeout(resolve, delay));
848
863
  }
849
864
 
865
+ exports.ObjectAssign = ObjectAssign;
850
866
  exports.autoEscapedRegExp = autoEscapedRegExp;
851
867
  exports.createConstructor = createConstructor;
852
868
  exports.debounce = debounce;
package/index.d.ts CHANGED
@@ -1,10 +1,9 @@
1
- import { CreateConstructor, createConstructor } from './src/object/createConstructor';
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "1.0.13-beta.1",
3
+ "version": "1.0.13-beta.2",
4
4
  "name": "a-js-tools",
5
5
  "description": "一点点 🤏 js 函数",
6
6
  "license": "MIT",
@@ -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>;