a-js-tools 1.0.4 → 2.0.0-alpha.0

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.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { CreateConstructor, createConstructor } from 'src/object/createConstructor';
2
2
  export { toLowerCamelCase, toSplitCase, getRandomFloat, getRandomInt, getRandomString, } from './src/index';
3
3
  export { throttle, debounce } from './src/performance';
4
- export type { DebounceAndThrottleReturnType } from './src/performance';
4
+ export type { DebounceAndThrottleReturnType, debounce_throttle_options, } from './src/performance';
5
5
  export { escapeRegExp, autoEscapedRegExp } from './src/regexp';
6
6
  export { isBrowser, isNode } from './src/isNode';
7
7
  export { intersection, enArr, union, difference, symmetricDifference, } from './src/array/';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "1.0.4",
3
+ "version": "2.0.0-alpha.0",
4
4
  "name": "a-js-tools",
5
5
  "description": "一点点 🤏 js 函数",
6
6
  "license": "MIT",
@@ -34,12 +34,10 @@ var aTypeOfJs = require('a-type-of-js');
34
34
  *
35
35
  */
36
36
  function difference(a, b) {
37
- if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
37
+ if ([a, b].some(e => !aTypeOfJs.isArray(e)))
38
38
  throw new TypeError('参数需为数组');
39
- }
40
- if (a.length === 0 || b.length === 0) {
39
+ if ([a, b].some(e => aTypeOfJs.isEmptyArray(e)))
41
40
  return a;
42
- }
43
41
  /** 获取两个数组中长度较小的 */
44
42
  // 参数有顺序要求
45
43
  // const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
@@ -1,4 +1,4 @@
1
- import { isArray } from 'a-type-of-js';
1
+ import { isArray, isEmptyArray } from 'a-type-of-js';
2
2
 
3
3
  /**
4
4
  * 求给出的两个数组的差值(A - B)
@@ -32,12 +32,10 @@ import { isArray } from 'a-type-of-js';
32
32
  *
33
33
  */
34
34
  function difference(a, b) {
35
- if (!isArray(a) || !isArray(b)) {
35
+ if ([a, b].some(e => !isArray(e)))
36
36
  throw new TypeError('参数需为数组');
37
- }
38
- if (a.length === 0 || b.length === 0) {
37
+ if ([a, b].some(e => isEmptyArray(e)))
39
38
  return a;
40
- }
41
39
  /** 获取两个数组中长度较小的 */
42
40
  // 参数有顺序要求
43
41
  // const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
@@ -24,13 +24,11 @@ var aTypeOfJs = require('a-type-of-js');
24
24
  *
25
25
  */
26
26
  function intersection(a, b) {
27
- if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
27
+ if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b))
28
28
  throw new TypeError('参数必须是数组类型数据');
29
- }
30
29
  // 任意数组为空,则返回空数组
31
- if (a.length === 0 || b.length === 0) {
30
+ if ([a, b].some(e => aTypeOfJs.isEmptyArray(e)))
32
31
  return [];
33
- }
34
32
  /**
35
33
  * 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
36
34
  *
@@ -1,4 +1,4 @@
1
- import { isArray } from 'a-type-of-js';
1
+ import { isArray, isEmptyArray } from 'a-type-of-js';
2
2
 
3
3
  /**
4
4
  *
@@ -22,13 +22,11 @@ import { isArray } from 'a-type-of-js';
22
22
  *
23
23
  */
24
24
  function intersection(a, b) {
25
- if (!isArray(a) || !isArray(b)) {
25
+ if (!isArray(a) || !isArray(b))
26
26
  throw new TypeError('参数必须是数组类型数据');
27
- }
28
27
  // 任意数组为空,则返回空数组
29
- if (a.length === 0 || b.length === 0) {
28
+ if ([a, b].some(e => isEmptyArray(e)))
30
29
  return [];
31
- }
32
30
  /**
33
31
  * 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
34
32
  *
@@ -39,10 +39,10 @@ function symmetricDifference(a, b) {
39
39
  if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
40
40
  throw new TypeError('参数必须是数组');
41
41
  }
42
- if (a.length === 0) {
42
+ if (aTypeOfJs.isEmptyArray(a)) {
43
43
  return [...b];
44
44
  }
45
- if (b.length === 0) {
45
+ if (aTypeOfJs.isEmptyArray(b)) {
46
46
  return [...a];
47
47
  }
48
48
  return [...difference.difference(a, b), ...difference.difference(b, a)];
@@ -1,4 +1,4 @@
1
- import { isArray } from 'a-type-of-js';
1
+ import { isArray, isEmptyArray } from 'a-type-of-js';
2
2
  import { difference } from './difference.mjs';
3
3
 
4
4
  /**
@@ -37,10 +37,10 @@ function symmetricDifference(a, b) {
37
37
  if (!isArray(a) || !isArray(b)) {
38
38
  throw new TypeError('参数必须是数组');
39
39
  }
40
- if (a.length === 0) {
40
+ if (isEmptyArray(a)) {
41
41
  return [...b];
42
42
  }
43
- if (b.length === 0) {
43
+ if (isEmptyArray(b)) {
44
44
  return [...a];
45
45
  }
46
46
  return [...difference(a, b), ...difference(b, a)];
@@ -44,7 +44,7 @@ var aTypeOfJs = require('a-type-of-js');
44
44
  *
45
45
  */
46
46
  function union(...arrays) {
47
- if (arrays.length === 0) {
47
+ if (aTypeOfJs.isEmptyArray(arrays)) {
48
48
  return [];
49
49
  }
50
50
  if (arrays.some(i => !aTypeOfJs.isArray(i))) {
@@ -1,4 +1,4 @@
1
- import { isArray } from 'a-type-of-js';
1
+ import { isEmptyArray, isArray } from 'a-type-of-js';
2
2
 
3
3
  /**
4
4
  *
@@ -42,7 +42,7 @@ import { isArray } from 'a-type-of-js';
42
42
  *
43
43
  */
44
44
  function union(...arrays) {
45
- if (arrays.length === 0) {
45
+ if (isEmptyArray(arrays)) {
46
46
  return [];
47
47
  }
48
48
  if (arrays.some(i => !isArray(i))) {
@@ -104,7 +104,7 @@ function getRandomString(options) {
104
104
  const str1Length = str1.length, str2Length = str2.length;
105
105
  const maxLength = Math.max(str1Length, str2Length);
106
106
  for (let i = 0; i < maxLength; i++) {
107
- if (i < str1Length && str2[i] !== undefined) {
107
+ if (i < str1Length && !aTypeOfJs.isUndefined(str2[i])) {
108
108
  str1[i] += str2[i];
109
109
  }
110
110
  else if (i < str2Length) {
@@ -1,4 +1,4 @@
1
- import { isPlainObject, isNumber, isNaN } from 'a-type-of-js';
1
+ import { isPlainObject, isNumber, isNaN, isUndefined } from 'a-type-of-js';
2
2
  import { isBrowser } from './isNode.mjs';
3
3
 
4
4
  /**
@@ -102,7 +102,7 @@ function getRandomString(options) {
102
102
  const str1Length = str1.length, str2Length = str2.length;
103
103
  const maxLength = Math.max(str1Length, str2Length);
104
104
  for (let i = 0; i < maxLength; i++) {
105
- if (i < str1Length && str2[i] !== undefined) {
105
+ if (i < str1Length && !isUndefined(str2[i])) {
106
106
  str1[i] += str2[i];
107
107
  }
108
108
  else if (i < str2Length) {
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var aTypeOfJs = require('a-type-of-js');
4
+
3
5
  /**
4
6
  * 防抖和节流
5
7
  *
@@ -11,8 +13,7 @@
11
13
  *
12
14
  * 防抖
13
15
  *
14
- * @param callback 回调函数
15
- * @param delay 延迟时间(毫秒),默认 200 (ms)
16
+ * @param options 使用参数
16
17
  * @returns 返回的闭包函数
17
18
  * @example
18
19
  *
@@ -25,13 +26,21 @@
25
26
  * }
26
27
  *
27
28
  */
28
- function debounce(callback, delay = 200) {
29
- if (typeof callback !== 'function') {
30
- throw new TypeError('callback must be a function');
29
+ function debounce(options) {
30
+ if (aTypeOfJs.isFunction(options)) {
31
+ options = {
32
+ callback: options,
33
+ delay: 200,
34
+ this: null,
35
+ };
31
36
  }
32
- if (!isFinite(delay) || delay < 0)
37
+ if (!aTypeOfJs.isPlainObject(options) || !aTypeOfJs.isFunction(options.callback))
38
+ throw new TypeError('参数类型有误');
39
+ if (aTypeOfJs.isUndefined(options.delay))
40
+ options.delay = 200;
41
+ if (!isFinite(options.delay) || options.delay < 0)
33
42
  // 强制转换非数值
34
- delay = 200;
43
+ options.delay = 200;
35
44
  /** 定时器返回的 id */
36
45
  let timeoutId;
37
46
  const clear = () => {
@@ -44,12 +53,12 @@ function debounce(callback, delay = 200) {
44
53
  clear();
45
54
  timeoutId = setTimeout(() => {
46
55
  try {
47
- Reflect.apply(callback, null, args);
56
+ Reflect.apply(options.callback, options.this ?? null, args);
48
57
  }
49
58
  catch (error) {
50
59
  console.log('Debounce callback throw an error', error);
51
60
  }
52
- }, Math.max(delay, 5));
61
+ }, Math.max(options.delay ?? 5, 5));
53
62
  };
54
63
  result.cancel = () => {
55
64
  clear();
@@ -59,14 +68,24 @@ function debounce(callback, delay = 200) {
59
68
  /**
60
69
  * 节流
61
70
  *
62
- * @param callback 回调函数
63
- * @param delay 延迟时间(毫秒),默认 200 (ms)
71
+ * @param options 使用的参数值
64
72
  * @returns 返回的闭包函数
65
73
  */
66
- function throttle(callback, delay = 200) {
67
- // 强制转换非数值
68
- if (!isFinite(delay) || (isFinite(delay) && delay < 4))
69
- delay = 200;
74
+ function throttle(options) {
75
+ if (aTypeOfJs.isFunction(options)) {
76
+ options = {
77
+ callback: options,
78
+ delay: 200,
79
+ this: null,
80
+ };
81
+ }
82
+ if (!aTypeOfJs.isPlainObject(options) || !aTypeOfJs.isFunction(options.callback))
83
+ throw new TypeError('参数类型有误');
84
+ if (aTypeOfJs.isUndefined(options.delay))
85
+ options.delay = 200;
86
+ if (!isFinite(options.delay) || options.delay < 0)
87
+ // 强制转换非数值
88
+ options.delay = 200;
70
89
  /** 延迟控制插销 */
71
90
  let inThrottle = false;
72
91
  /** 延迟控制 */
@@ -75,22 +94,22 @@ function throttle(callback, delay = 200) {
75
94
  if (inThrottle)
76
95
  return;
77
96
  try {
78
- Reflect.apply(callback, null, args);
97
+ Reflect.apply(options.callback, options.this ?? null, args);
79
98
  }
80
99
  catch (error) {
81
100
  console.error('Throttle callback throw an error', error);
82
101
  }
83
102
  inThrottle = true;
84
- if (timeoutId !== null) {
103
+ if (!aTypeOfJs.isNull(timeoutId)) {
85
104
  clearTimeout(timeoutId);
86
105
  }
87
106
  timeoutId = setTimeout(() => {
88
107
  inThrottle = false;
89
108
  timeoutId = null;
90
- }, delay);
109
+ }, Math.max(options.delay ?? 5, 5));
91
110
  };
92
111
  throttled.cancel = () => {
93
- if (timeoutId !== null) {
112
+ if (!aTypeOfJs.isNull(timeoutId)) {
94
113
  clearTimeout(timeoutId);
95
114
  }
96
115
  inThrottle = false;
@@ -15,12 +15,20 @@ export interface DebounceAndThrottleReturnType<F extends Callback> {
15
15
  (...args: Parameters<F>): void;
16
16
  cancel(): void;
17
17
  }
18
+ /** 参数类型 */
19
+ export type debounce_throttle_options<T extends (...args: unknown[]) => void> = T | {
20
+ /** 回调函数 */
21
+ callback: T;
22
+ /** 使用的延迟时间,缺省值为 200ms */
23
+ delay?: number;
24
+ /** 使用的 this */
25
+ this?: null | unknown;
26
+ };
18
27
  /**
19
28
  *
20
29
  * 防抖
21
30
  *
22
- * @param callback 回调函数
23
- * @param delay 延迟时间(毫秒),默认 200 (ms)
31
+ * @param options 使用参数
24
32
  * @returns 返回的闭包函数
25
33
  * @example
26
34
  *
@@ -33,13 +41,12 @@ export interface DebounceAndThrottleReturnType<F extends Callback> {
33
41
  * }
34
42
  *
35
43
  */
36
- export declare function debounce<F extends (...args: unknown[]) => void>(callback: F, delay?: number): DebounceAndThrottleReturnType<F>;
44
+ export declare function debounce<F extends (...args: unknown[]) => void>(options: debounce_throttle_options<F>): DebounceAndThrottleReturnType<F>;
37
45
  /**
38
46
  * 节流
39
47
  *
40
- * @param callback 回调函数
41
- * @param delay 延迟时间(毫秒),默认 200 (ms)
48
+ * @param options 使用的参数值
42
49
  * @returns 返回的闭包函数
43
50
  */
44
- export declare function throttle<F extends (...args: unknown[]) => void>(callback: F, delay?: number): DebounceAndThrottleReturnType<F>;
51
+ export declare function throttle<F extends (...args: unknown[]) => void>(options: debounce_throttle_options<F>): DebounceAndThrottleReturnType<F>;
45
52
  export {};
@@ -1,3 +1,5 @@
1
+ import { isFunction, isPlainObject, isUndefined, isNull } from 'a-type-of-js';
2
+
1
3
  /**
2
4
  * 防抖和节流
3
5
  *
@@ -9,8 +11,7 @@
9
11
  *
10
12
  * 防抖
11
13
  *
12
- * @param callback 回调函数
13
- * @param delay 延迟时间(毫秒),默认 200 (ms)
14
+ * @param options 使用参数
14
15
  * @returns 返回的闭包函数
15
16
  * @example
16
17
  *
@@ -23,13 +24,21 @@
23
24
  * }
24
25
  *
25
26
  */
26
- function debounce(callback, delay = 200) {
27
- if (typeof callback !== 'function') {
28
- throw new TypeError('callback must be a function');
27
+ function debounce(options) {
28
+ if (isFunction(options)) {
29
+ options = {
30
+ callback: options,
31
+ delay: 200,
32
+ this: null,
33
+ };
29
34
  }
30
- if (!isFinite(delay) || delay < 0)
35
+ if (!isPlainObject(options) || !isFunction(options.callback))
36
+ throw new TypeError('参数类型有误');
37
+ if (isUndefined(options.delay))
38
+ options.delay = 200;
39
+ if (!isFinite(options.delay) || options.delay < 0)
31
40
  // 强制转换非数值
32
- delay = 200;
41
+ options.delay = 200;
33
42
  /** 定时器返回的 id */
34
43
  let timeoutId;
35
44
  const clear = () => {
@@ -42,12 +51,12 @@ function debounce(callback, delay = 200) {
42
51
  clear();
43
52
  timeoutId = setTimeout(() => {
44
53
  try {
45
- Reflect.apply(callback, null, args);
54
+ Reflect.apply(options.callback, options.this ?? null, args);
46
55
  }
47
56
  catch (error) {
48
57
  console.log('Debounce callback throw an error', error);
49
58
  }
50
- }, Math.max(delay, 5));
59
+ }, Math.max(options.delay ?? 5, 5));
51
60
  };
52
61
  result.cancel = () => {
53
62
  clear();
@@ -57,14 +66,24 @@ function debounce(callback, delay = 200) {
57
66
  /**
58
67
  * 节流
59
68
  *
60
- * @param callback 回调函数
61
- * @param delay 延迟时间(毫秒),默认 200 (ms)
69
+ * @param options 使用的参数值
62
70
  * @returns 返回的闭包函数
63
71
  */
64
- function throttle(callback, delay = 200) {
65
- // 强制转换非数值
66
- if (!isFinite(delay) || (isFinite(delay) && delay < 4))
67
- delay = 200;
72
+ function throttle(options) {
73
+ if (isFunction(options)) {
74
+ options = {
75
+ callback: options,
76
+ delay: 200,
77
+ this: null,
78
+ };
79
+ }
80
+ if (!isPlainObject(options) || !isFunction(options.callback))
81
+ throw new TypeError('参数类型有误');
82
+ if (isUndefined(options.delay))
83
+ options.delay = 200;
84
+ if (!isFinite(options.delay) || options.delay < 0)
85
+ // 强制转换非数值
86
+ options.delay = 200;
68
87
  /** 延迟控制插销 */
69
88
  let inThrottle = false;
70
89
  /** 延迟控制 */
@@ -73,22 +92,22 @@ function throttle(callback, delay = 200) {
73
92
  if (inThrottle)
74
93
  return;
75
94
  try {
76
- Reflect.apply(callback, null, args);
95
+ Reflect.apply(options.callback, options.this ?? null, args);
77
96
  }
78
97
  catch (error) {
79
98
  console.error('Throttle callback throw an error', error);
80
99
  }
81
100
  inThrottle = true;
82
- if (timeoutId !== null) {
101
+ if (!isNull(timeoutId)) {
83
102
  clearTimeout(timeoutId);
84
103
  }
85
104
  timeoutId = setTimeout(() => {
86
105
  inThrottle = false;
87
106
  timeoutId = null;
88
- }, delay);
107
+ }, Math.max(options.delay ?? 5, 5));
89
108
  };
90
109
  throttled.cancel = () => {
91
- if (timeoutId !== null) {
110
+ if (!isNull(timeoutId)) {
92
111
  clearTimeout(timeoutId);
93
112
  }
94
113
  inThrottle = false;
package/src/sleep.cjs CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var aTypeOfJs = require('a-type-of-js');
4
+
3
5
  /**
4
6
  *
5
7
  * ## 线程休息
@@ -28,7 +30,7 @@
28
30
  async function sleep(delay = 1000) {
29
31
  if (!isFinite(delay) || delay < 0)
30
32
  throw new TypeError('delay 应该是一个正常的数值');
31
- if (delay === 0)
33
+ if (aTypeOfJs.isZero(delay))
32
34
  return Promise.resolve();
33
35
  return new Promise(resolve => setTimeout(resolve, delay));
34
36
  }
package/src/sleep.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { isZero } from 'a-type-of-js';
2
+
1
3
  /**
2
4
  *
3
5
  * ## 线程休息
@@ -26,7 +28,7 @@
26
28
  async function sleep(delay = 1000) {
27
29
  if (!isFinite(delay) || delay < 0)
28
30
  throw new TypeError('delay 应该是一个正常的数值');
29
- if (delay === 0)
31
+ if (isZero(delay))
30
32
  return Promise.resolve();
31
33
  return new Promise(resolve => setTimeout(resolve, delay));
32
34
  }