a-js-tools 1.0.12 → 1.0.13-beta.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.
@@ -1,49 +0,0 @@
1
- /**
2
- *
3
- * 构建一个 Constructor 构造函数
4
- *
5
- * 接收一个构造函数,然后返回 TS 能识别的构造函数自身
6
- *
7
- * 函数本身并没有执行任何逻辑,仅是简单的返回了实参自身
8
- *
9
- * 而经过该函数的包装,构造函数成了能够被 TS 识别为可用 new 实例的构造函数
10
- *
11
- * @param constructor - 传入一个构造函数
12
- * @returns 返回传入的构造函数
13
- *
14
- * ```ts
15
- * import { createConstructor } from "a-js-tools";
16
- *
17
- * type Tom = {
18
- * a: number
19
- * }
20
- *
21
- * function _Tom (this: TomType): Tom {
22
- * this.a = 1;
23
- *
24
- * return this;
25
- * }
26
- *
27
- * // 逻辑上没有错,但是会造成 ts 显示
28
- * // 其目标缺少构造签名的 "new" 表达式隐式具有 "any" 类型。ts(7009)
29
- * const a = new _Tom();
30
- *
31
- * const tomConstructor = createConstructor(_tom);
32
- *
33
- * const b = new tomConstructor(); // 这时就不会显示错误
34
- *
35
- * ```
36
- *
37
- */
38
- function createConstructor(constructor) {
39
- constructor.prototype.apply = Function.apply;
40
- constructor.prototype.bind = Function.bind;
41
- constructor.prototype.call = Function.call;
42
- constructor.prototype.length = Function.length;
43
- // constructor.prototype.arguments = Function.arguments;
44
- constructor.prototype.name = Function.name;
45
- constructor.prototype.toString = Function.toString;
46
- return constructor;
47
- }
48
-
49
- export { createConstructor };
@@ -1,116 +0,0 @@
1
- 'use strict';
2
-
3
- var aTypeOfJs = require('a-type-of-js');
4
-
5
- /**
6
- * 防抖和节流
7
- *
8
- * @packageDocumentation
9
- * @module @a-js-tools/performance
10
- * @license MIT
11
- */
12
- /**
13
- *
14
- * 防抖
15
- *
16
- * @param callback 回调函数
17
- * @param options 延迟时间(毫秒),默认 200 (ms) 或包含 this 的配置
18
- * @returns 返回的闭包函数
19
- * @example
20
- *
21
- * ```ts
22
- * const debounce = (callback: Function, delay = 300) => {
23
- * let timer: any = null
24
- * return (...args: any[]) => {
25
- * clearTimeout(timer)
26
- * }
27
- * }
28
- *
29
- */
30
- function debounce(callback, options = 200) {
31
- if (!aTypeOfJs.isFunction(callback))
32
- throw new TypeError('callback must be a function');
33
- if (aTypeOfJs.isNumber(options))
34
- options = {
35
- delay: options,
36
- this: null,
37
- };
38
- if (aTypeOfJs.isUndefined(options.delay) ||
39
- !isFinite(options.delay) ||
40
- options.delay < 0)
41
- // 强制转换非数值
42
- options.delay = 200;
43
- /** 定时器返回的 id */
44
- let timeoutId;
45
- const clear = () => {
46
- if (timeoutId) {
47
- clearTimeout(timeoutId);
48
- timeoutId = undefined;
49
- }
50
- };
51
- const result = (...args) => {
52
- clear();
53
- timeoutId = setTimeout(() => {
54
- try {
55
- Reflect.apply(callback, options?.this ?? null, args);
56
- }
57
- catch (error) {
58
- console.log('Debounce callback throw an error', error);
59
- }
60
- }, Math.max(options?.delay ?? 5, 5));
61
- };
62
- result.cancel = () => clear();
63
- return result;
64
- }
65
- /**
66
- * 节流
67
- *
68
- * @param callback 回调函数
69
- * @param options 延迟时间(毫秒),默认 200 (ms) 或设置 this
70
- * @returns 返回的闭包函数
71
- */
72
- function throttle(callback, options = 200) {
73
- if (!aTypeOfJs.isFunction(callback))
74
- throw new TypeError('callback must be a function');
75
- if (aTypeOfJs.isNumber(options))
76
- options = {
77
- delay: options,
78
- this: null,
79
- };
80
- if (aTypeOfJs.isUndefined(options.delay) ||
81
- !isFinite(options.delay) ||
82
- options.delay < 0)
83
- // 强制转换非数值
84
- options.delay = 200;
85
- /** 延迟控制插销 */
86
- let inThrottle = false;
87
- /** 延迟控制 */
88
- let timeoutId = null;
89
- const throttled = (...args) => {
90
- if (inThrottle)
91
- return;
92
- try {
93
- Reflect.apply(callback, options?.this ?? null, args);
94
- }
95
- catch (error) {
96
- console.error('Throttle 执行回调抛出问题', error);
97
- }
98
- inThrottle = true;
99
- if (!aTypeOfJs.isNull(timeoutId))
100
- clearTimeout(timeoutId);
101
- timeoutId = setTimeout(() => {
102
- inThrottle = false;
103
- timeoutId = null;
104
- }, Math.max(options?.delay ?? 5, 5));
105
- };
106
- throttled.cancel = () => {
107
- if (!aTypeOfJs.isNull(timeoutId))
108
- clearTimeout(timeoutId);
109
- inThrottle = false;
110
- timeoutId = null;
111
- };
112
- return throttled;
113
- }
114
-
115
- exports.debounce = debounce;
116
- exports.throttle = throttle;
@@ -1,113 +0,0 @@
1
- import { isFunction, isNumber, isUndefined, isNull } from 'a-type-of-js';
2
-
3
- /**
4
- * 防抖和节流
5
- *
6
- * @packageDocumentation
7
- * @module @a-js-tools/performance
8
- * @license MIT
9
- */
10
- /**
11
- *
12
- * 防抖
13
- *
14
- * @param callback 回调函数
15
- * @param options 延迟时间(毫秒),默认 200 (ms) 或包含 this 的配置
16
- * @returns 返回的闭包函数
17
- * @example
18
- *
19
- * ```ts
20
- * const debounce = (callback: Function, delay = 300) => {
21
- * let timer: any = null
22
- * return (...args: any[]) => {
23
- * clearTimeout(timer)
24
- * }
25
- * }
26
- *
27
- */
28
- function debounce(callback, options = 200) {
29
- if (!isFunction(callback))
30
- throw new TypeError('callback must be a function');
31
- if (isNumber(options))
32
- options = {
33
- delay: options,
34
- this: null,
35
- };
36
- if (isUndefined(options.delay) ||
37
- !isFinite(options.delay) ||
38
- options.delay < 0)
39
- // 强制转换非数值
40
- options.delay = 200;
41
- /** 定时器返回的 id */
42
- let timeoutId;
43
- const clear = () => {
44
- if (timeoutId) {
45
- clearTimeout(timeoutId);
46
- timeoutId = undefined;
47
- }
48
- };
49
- const result = (...args) => {
50
- clear();
51
- timeoutId = setTimeout(() => {
52
- try {
53
- Reflect.apply(callback, options?.this ?? null, args);
54
- }
55
- catch (error) {
56
- console.log('Debounce callback throw an error', error);
57
- }
58
- }, Math.max(options?.delay ?? 5, 5));
59
- };
60
- result.cancel = () => clear();
61
- return result;
62
- }
63
- /**
64
- * 节流
65
- *
66
- * @param callback 回调函数
67
- * @param options 延迟时间(毫秒),默认 200 (ms) 或设置 this
68
- * @returns 返回的闭包函数
69
- */
70
- function throttle(callback, options = 200) {
71
- if (!isFunction(callback))
72
- throw new TypeError('callback must be a function');
73
- if (isNumber(options))
74
- options = {
75
- delay: options,
76
- this: null,
77
- };
78
- if (isUndefined(options.delay) ||
79
- !isFinite(options.delay) ||
80
- options.delay < 0)
81
- // 强制转换非数值
82
- options.delay = 200;
83
- /** 延迟控制插销 */
84
- let inThrottle = false;
85
- /** 延迟控制 */
86
- let timeoutId = null;
87
- const throttled = (...args) => {
88
- if (inThrottle)
89
- return;
90
- try {
91
- Reflect.apply(callback, options?.this ?? null, args);
92
- }
93
- catch (error) {
94
- console.error('Throttle 执行回调抛出问题', error);
95
- }
96
- inThrottle = true;
97
- if (!isNull(timeoutId))
98
- clearTimeout(timeoutId);
99
- timeoutId = setTimeout(() => {
100
- inThrottle = false;
101
- timeoutId = null;
102
- }, Math.max(options?.delay ?? 5, 5));
103
- };
104
- throttled.cancel = () => {
105
- if (!isNull(timeoutId))
106
- clearTimeout(timeoutId);
107
- inThrottle = false;
108
- timeoutId = null;
109
- };
110
- return throttled;
111
- }
112
-
113
- export { debounce, throttle };
@@ -1,48 +0,0 @@
1
- 'use strict';
2
-
3
- var aTypeOfJs = require('a-type-of-js');
4
- var escapeRegExp = require('./escapeRegExp.cjs');
5
- var parse = require('./parse.cjs');
6
-
7
- /**
8
- *
9
- * ## 适用于简单的文本字符串自动转化为简单模式正则表达式
10
- *
11
- * *若字符串包含且需保留字符类、组、反向引用、量词等时,该方法可能不适用*
12
- *
13
- * @param pattern 待转化的文本字符串
14
- * @param options 转化选项。 为文本字符串时,默认为正则表达式的标志,还可指定是否匹配开头或结尾
15
- * @returns 正则表达式
16
- * @example
17
- *
18
- * ```ts
19
- * import { autoEscapedRegExp } from 'a-regexp';
20
- *
21
- * autoEscapedRegExp('abc'); // => /abc/
22
- * autoEscapedRegExp('abc', 'gim'); // => /abc/gim
23
- * autoEscapedRegExp('abc', { flags: 'g', start: true }); // => /^abc/g
24
- * autoEscapedRegExp('abc', { flags: 'g', end: true }); // => /abc$/g
25
- * autoEscapedRegExp('abc', { start: true, end: true }); // => /^abc$/
26
- *
27
- * // 转化特殊字符类、组、反向引用、量词等
28
- * // 无法保留匹配规则
29
- * autoEscapedRegExp('a-zA-Z0-9'); // => /a-zA-Z0-9/
30
- * autoEscapedRegExp('a-zA-Z0-9', 'g'); // => /a-zA-Z0-9/g
31
- * autoEscapedRegExp('[a-zA-Z0-9]'); // => /\[a-zA-Z0-9\]/
32
- * autoEscapedRegExp('^[a-zA-Z0-9]+$'); // => /\^\[a-zA-Z0-9\]\$/
33
- *
34
- * ```
35
- *
36
- */
37
- function autoEscapedRegExp(pattern, options) {
38
- if (!aTypeOfJs.isString(pattern))
39
- throw new TypeError('pattern must be a 字符串');
40
- pattern = escapeRegExp.escapeRegExp(pattern);
41
- /** 简单转化 */
42
- if (aTypeOfJs.isUndefined(options))
43
- return new RegExp(pattern);
44
- options = parse.parse(options);
45
- return new RegExp(`${options.start ? '^' : ''}${pattern}${options.end ? '$' : ''}`, options.flags);
46
- }
47
-
48
- exports.autoEscapedRegExp = autoEscapedRegExp;
@@ -1,46 +0,0 @@
1
- import { isString, isUndefined } from 'a-type-of-js';
2
- import { escapeRegExp } from './escapeRegExp.mjs';
3
- import { parse } from './parse.mjs';
4
-
5
- /**
6
- *
7
- * ## 适用于简单的文本字符串自动转化为简单模式正则表达式
8
- *
9
- * *若字符串包含且需保留字符类、组、反向引用、量词等时,该方法可能不适用*
10
- *
11
- * @param pattern 待转化的文本字符串
12
- * @param options 转化选项。 为文本字符串时,默认为正则表达式的标志,还可指定是否匹配开头或结尾
13
- * @returns 正则表达式
14
- * @example
15
- *
16
- * ```ts
17
- * import { autoEscapedRegExp } from 'a-regexp';
18
- *
19
- * autoEscapedRegExp('abc'); // => /abc/
20
- * autoEscapedRegExp('abc', 'gim'); // => /abc/gim
21
- * autoEscapedRegExp('abc', { flags: 'g', start: true }); // => /^abc/g
22
- * autoEscapedRegExp('abc', { flags: 'g', end: true }); // => /abc$/g
23
- * autoEscapedRegExp('abc', { start: true, end: true }); // => /^abc$/
24
- *
25
- * // 转化特殊字符类、组、反向引用、量词等
26
- * // 无法保留匹配规则
27
- * autoEscapedRegExp('a-zA-Z0-9'); // => /a-zA-Z0-9/
28
- * autoEscapedRegExp('a-zA-Z0-9', 'g'); // => /a-zA-Z0-9/g
29
- * autoEscapedRegExp('[a-zA-Z0-9]'); // => /\[a-zA-Z0-9\]/
30
- * autoEscapedRegExp('^[a-zA-Z0-9]+$'); // => /\^\[a-zA-Z0-9\]\$/
31
- *
32
- * ```
33
- *
34
- */
35
- function autoEscapedRegExp(pattern, options) {
36
- if (!isString(pattern))
37
- throw new TypeError('pattern must be a 字符串');
38
- pattern = escapeRegExp(pattern);
39
- /** 简单转化 */
40
- if (isUndefined(options))
41
- return new RegExp(pattern);
42
- options = parse(options);
43
- return new RegExp(`${options.start ? '^' : ''}${pattern}${options.end ? '$' : ''}`, options.flags);
44
- }
45
-
46
- export { autoEscapedRegExp };
@@ -1,24 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- *
5
- * 将一个字符串转化为符合正则要求的字符串
6
- *
7
- * @param str 需要转义的字符串
8
- * @requires escapeRegExp 转化后字符串
9
- * @example
10
- *
11
- * ```ts
12
- * import { escapeRegExp } from 'a-js-tools';
13
- *
14
- * escapeRegExp('a.b.c'); // 'a\\.b\\.c'
15
- * escapeRegExp('a\\.b\\.c'); // 'a\\\\.b\\\\.c'
16
- * escapeRegExp('[a-z]'); // '\\[a-z\\]'
17
- * escapeRegExp('^abc$'); // '\\^abc\\$'
18
- * ```
19
- */
20
- function escapeRegExp(str) {
21
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
22
- }
23
-
24
- exports.escapeRegExp = escapeRegExp;
@@ -1,22 +0,0 @@
1
- /**
2
- *
3
- * 将一个字符串转化为符合正则要求的字符串
4
- *
5
- * @param str 需要转义的字符串
6
- * @requires escapeRegExp 转化后字符串
7
- * @example
8
- *
9
- * ```ts
10
- * import { escapeRegExp } from 'a-js-tools';
11
- *
12
- * escapeRegExp('a.b.c'); // 'a\\.b\\.c'
13
- * escapeRegExp('a\\.b\\.c'); // 'a\\\\.b\\\\.c'
14
- * escapeRegExp('[a-z]'); // '\\[a-z\\]'
15
- * escapeRegExp('^abc$'); // '\\^abc\\$'
16
- * ```
17
- */
18
- function escapeRegExp(str) {
19
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
20
- }
21
-
22
- export { escapeRegExp };
@@ -1,32 +0,0 @@
1
- 'use strict';
2
-
3
- var aTypeOfJs = require('a-type-of-js');
4
-
5
- /**
6
- *
7
- * 解析 options
8
- *
9
- */
10
- function parse(options) {
11
- // 处理 options
12
- if (aTypeOfJs.isString(options)) {
13
- options = {
14
- flags: options,
15
- };
16
- }
17
- // 处理 flags
18
- if (!aTypeOfJs.isString(options.flags))
19
- options.flags = '';
20
- else {
21
- // 需求是保留字符串中的某一部分,使用
22
- const regexp = /[migsuy]/g;
23
- const matchResult = options.flags.match(regexp);
24
- if (aTypeOfJs.isNull(matchResult))
25
- options.flags = '';
26
- else
27
- options.flags = [...new Set(matchResult)].join('');
28
- }
29
- return options;
30
- }
31
-
32
- exports.parse = parse;
@@ -1,30 +0,0 @@
1
- import { isString, isNull } from 'a-type-of-js';
2
-
3
- /**
4
- *
5
- * 解析 options
6
- *
7
- */
8
- function parse(options) {
9
- // 处理 options
10
- if (isString(options)) {
11
- options = {
12
- flags: options,
13
- };
14
- }
15
- // 处理 flags
16
- if (!isString(options.flags))
17
- options.flags = '';
18
- else {
19
- // 需求是保留字符串中的某一部分,使用
20
- const regexp = /[migsuy]/g;
21
- const matchResult = options.flags.match(regexp);
22
- if (isNull(matchResult))
23
- options.flags = '';
24
- else
25
- options.flags = [...new Set(matchResult)].join('');
26
- }
27
- return options;
28
- }
29
-
30
- export { parse };
package/src/sleep.cjs DELETED
@@ -1,38 +0,0 @@
1
- 'use strict';
2
-
3
- var aTypeOfJs = require('a-type-of-js');
4
-
5
- /**
6
- *
7
- * ## 线程休息
8
- *
9
- * 但从调用到执行完毕总是与期望的时间并不相吻合,除非执行是线型的(也不保证时间的严格性)
10
- *
11
- * - 宏任务:整体代码、setTimeout、DOM 事件回调、requestAnimationFrame、setImmediate、setInterval、I/O操作、UI渲染等
12
- * - 微任务:Promise的then/catch/finally、process.nextTick(Node.js)、MutationObserver、queueMicrotask(显示添加微任务)等
13
- *
14
- * <span style="color:#ff0;">*Node.js中的process.nextTick优先级高于其他微任务*</span>
15
- *
16
- * @param delay 睡觉时长(机器时间,毫秒为单位)
17
- * @returns 🈳
18
- * @example
19
- *
20
- * ```ts
21
- * import { sleep } from 'a-js-tools';
22
- *
23
- * console.log(Date.now()); // 1748058118471
24
- * await sleep(1000);
25
- * console.log(Date.now()); // 1748058119473
26
- *
27
- * ```
28
- *
29
- */
30
- async function sleep(delay = 1000) {
31
- if (!isFinite(delay) || delay < 0)
32
- throw new TypeError('delay 应该是一个正常的数值');
33
- if (aTypeOfJs.isZero(delay))
34
- return Promise.resolve();
35
- return new Promise(resolve => setTimeout(resolve, delay));
36
- }
37
-
38
- exports.sleep = sleep;
package/src/sleep.mjs DELETED
@@ -1,36 +0,0 @@
1
- import { isZero } from 'a-type-of-js';
2
-
3
- /**
4
- *
5
- * ## 线程休息
6
- *
7
- * 但从调用到执行完毕总是与期望的时间并不相吻合,除非执行是线型的(也不保证时间的严格性)
8
- *
9
- * - 宏任务:整体代码、setTimeout、DOM 事件回调、requestAnimationFrame、setImmediate、setInterval、I/O操作、UI渲染等
10
- * - 微任务:Promise的then/catch/finally、process.nextTick(Node.js)、MutationObserver、queueMicrotask(显示添加微任务)等
11
- *
12
- * <span style="color:#ff0;">*Node.js中的process.nextTick优先级高于其他微任务*</span>
13
- *
14
- * @param delay 睡觉时长(机器时间,毫秒为单位)
15
- * @returns 🈳
16
- * @example
17
- *
18
- * ```ts
19
- * import { sleep } from 'a-js-tools';
20
- *
21
- * console.log(Date.now()); // 1748058118471
22
- * await sleep(1000);
23
- * console.log(Date.now()); // 1748058119473
24
- *
25
- * ```
26
- *
27
- */
28
- async function sleep(delay = 1000) {
29
- if (!isFinite(delay) || delay < 0)
30
- throw new TypeError('delay 应该是一个正常的数值');
31
- if (isZero(delay))
32
- return Promise.resolve();
33
- return new Promise(resolve => setTimeout(resolve, delay));
34
- }
35
-
36
- export { sleep };