a-js-tools 0.6.0 → 0.6.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/LICENSE CHANGED
@@ -1,5 +1,7 @@
1
- Copyright (c) <2024> <earthnut.dev>
1
+ # MIT License
2
2
 
3
+ Copyright (c) <2024> <earthnut.dev>
4
+
3
5
  Permission to use, copy, modify, and/or distribute this software for any
4
6
  purpose with or without fee is hereby granted, provided that the above
5
7
  copyright notice and this permission notice appear in all copies.
@@ -11,3 +13,13 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
13
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
14
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
15
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+
17
+ # MIT 许可证
18
+
19
+ 版权所有 (c) [2024] [花生亻]
20
+
21
+ 特此免费授予任何获得本软件及相关文档文件(以下简称“软件”)副本的人不受限制地处置该软件的权利,包括不受限制地使用、复制、修改、合并、发布、分发、再许可和/或出售该软件副本的权利,并允许向其提供该软件的人这样做,但须遵守以下条件:
22
+
23
+ 上述版权声明和本许可声明应包含在所有副本或软件的重要部分中。
24
+
25
+ 软件按“原样”提供,不附带任何形式的明示或暗示的保证,包括但不限于适销性、特定用途适用性和不侵权的保证。在任何情况下,作者或版权持有人均不对因合同、侵权或其他方式引起的任何索赔、损害或其他责任负责,无论是在与软件或软件的使用或其他交易有关的任何诉讼中。
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # a-js-tools
1
+ # 一点点 🤏 js 函数
2
2
 
3
3
  [![version](<https://img.shields.io/npm/v/a-js-tools.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [![Coverage Status](<https://img.shields.io/coverallsCoverage/github/earthnutDev/a-js-tools?logo=coveralls&label=coveralls&labelColor=rgb(12, 244, 39)&color=rgb(0,0,0)>)](https://coveralls.io/github/earthnutDev/a-js-tools?branch=main) [![codecov](<https://img.shields.io/codecov/c/github/earthnutDev/a-js-tools/main?logo=codecov&label=codecov&labelColor=rgb(7, 245, 245)&color=rgb(0,0,0)>)](https://codecov.io/gh/earthnutDev/a-js-tools) [![issues 提交](<https://img.shields.io/badge/issues-提交-rgb(255,0,63)?logo=github>)](https://github.com/earthnutDev/a-js-tools/issues)
4
4
  一个纯函数的工具
@@ -6,7 +6,7 @@
6
6
  ## 安装
7
7
 
8
8
  ```sh
9
- npm install a-js-tools --save
9
+ npm install --save a-js-tools
10
10
  ```
11
11
 
12
12
  ## 纯函数
@@ -5,10 +5,145 @@ var union = require('./union.cjs');
5
5
  var difference = require('./difference.cjs');
6
6
  var symmetricDifference = require('./symmetricDifference.cjs');
7
7
 
8
+ /**
9
+ *
10
+ * 数组的一些方法
11
+ *
12
+ * - union 两个数组的并集(排除共有项)
13
+ * - intersection 两个数组的交集(共有项)
14
+ * - difference 两个数组的差集 (A - B)
15
+ * - symmetricDifference 对称差集 ( A △ B)
16
+ *
17
+ */
8
18
  const enArr = {
19
+ /**
20
+ *
21
+ * 数组的并集
22
+ *
23
+ * <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
24
+ * @param arrays - 多个数组
25
+ * @returns 联合后的数组
26
+ *
27
+ *
28
+ * @example
29
+ *
30
+ * ```ts
31
+ * import { union } from 'a-js-tools';
32
+ *
33
+ * const log = console.log;
34
+ *
35
+ * // []
36
+ * log(union());
37
+ *
38
+ * // [1, 2, 3]
39
+ * log(union([1, 2, 3]));
40
+ *
41
+ * // TypeError
42
+ * log(union([1, 2, 3], 'i'));
43
+ * log(union([1, 2, 3], undefined));
44
+ * log(union([1, 2, 3], null));
45
+ * log(union([1, 2, 3], 4));
46
+ * log(union([1, 2, 3], {}));
47
+ * log(union([1, 2, 3], false));
48
+ *
49
+ * // [1, 2, 3, 4, 6]
50
+ * log(union([1, 2, 3], [2, 4, 6]));
51
+ *
52
+ * // [1, 2, 3, 4, 6]
53
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
54
+ *
55
+ * // [1, 2, 3, 4, 6]
56
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
57
+ * ```
58
+ *
59
+ */
9
60
  union: union.union,
61
+ /**
62
+ *
63
+ * 两个数组的交集
64
+ *
65
+ * @param a 数组 1️⃣
66
+ * @param b 数组 2️⃣
67
+ * @returns 返回两个数组的交集
68
+ * @example
69
+ *
70
+ * ```ts
71
+ * import { intersection } from 'a-js-tools';
72
+ *
73
+ * const log = console.log;
74
+ *
75
+ *
76
+ * log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
77
+ * log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
78
+ *
79
+ * ```
80
+ *
81
+ */
10
82
  intersection: intersection.intersection,
83
+ /**
84
+ * 求给出的两个数组的差值(A - B)
85
+ *
86
+ * @param a - 第一个数组
87
+ * @param b - 第二个数组
88
+ * @throws {TypeError} 当两个参数有一个不是
89
+ * @description 当两个参数有一个不是数组时将抛出
90
+ * @returns 返回第一个参数相对第二个参数的差值
91
+ * @example
92
+ *
93
+ * ```ts
94
+ * import { difference} from 'a-js-tools';
95
+ *
96
+ * const log = console.log;
97
+ *
98
+ *
99
+ * log(difference([], [1, 2, 3])); // []
100
+ *
101
+ * log([1, 2, 3], []); //[1, 2, 3]
102
+ *
103
+ * log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
104
+ *
105
+ * // 将抛出 TypeError
106
+ * log(difference([1, 2, 3], 'a'));
107
+ * log(difference([1, 2, 3], 1));
108
+ * log(difference([1, 2, 3], undefined));
109
+ * log(difference([1, 2, 3], null));
110
+ * log(difference([1, 2, 3], true));
111
+ * ```
112
+ *
113
+ */
11
114
  difference: difference.difference,
115
+ /**
116
+ *
117
+ * 对称差集 ( A △ B)
118
+ *
119
+ * @param a - 数组 a
120
+ * @param b - 数组 b
121
+ * @returns - 返回一个全新的数组
122
+ *
123
+ * @example
124
+ *
125
+ * ```ts
126
+ * import { symmetricDifference } from 'a-js-tools';
127
+ *
128
+ * const log = console.log;
129
+ *
130
+ * log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
131
+ *
132
+ * log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
133
+ *
134
+ * log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
135
+ *
136
+ *
137
+ * /// TypeError
138
+ * log(symmetricDifference(1, []));
139
+ * log(symmetricDifference(undefined, []));
140
+ * log(symmetricDifference(null, []));
141
+ * log(symmetricDifference('a', []));
142
+ * log(symmetricDifference(true, []));
143
+ *
144
+ * ```
145
+ *
146
+ */
12
147
  symmetricDifference: symmetricDifference.symmetricDifference,
13
148
  };
14
149
 
@@ -4,6 +4,38 @@
4
4
  *
5
5
  * 构建一个 Constructor 构造函数
6
6
  *
7
+ * 接收一个构造函数,然后返回 TS 能识别的构造函数自身
8
+ *
9
+ * 函数本身并没有执行任何逻辑,仅是简单的返回了实参自身
10
+ *
11
+ * 而经过该函数的包装,构造函数成了能够被 TS 识别为可用 new 实例的构造函数
12
+ *
13
+ * @param constructor - 传入一个构造函数
14
+ * @returns 返回传入的构造函数
15
+ *
16
+ * ```ts
17
+ * import { createConstructor } from "a-js-tools";
18
+ *
19
+ * type Tom = {
20
+ * a: number
21
+ * }
22
+ *
23
+ * function _Tom (this: TomType): Tom {
24
+ * this.a = 1;
25
+ *
26
+ * return this;
27
+ * }
28
+ *
29
+ * // 逻辑上没有错,但是会造成 ts 显示
30
+ * // 其目标缺少构造签名的 "new" 表达式隐式具有 "any" 类型。ts(7009)
31
+ * const a = new _Tom();
32
+ *
33
+ * const tomConstructor = createConstructor(_tom);
34
+ *
35
+ * const b = new tomConstructor(); // 这时就不会显示错误
36
+ *
37
+ * ```
38
+ *
7
39
  */
8
40
  function createConstructor(constructor) {
9
41
  return constructor;
@@ -3,10 +3,145 @@ import { union } from './union.mjs';
3
3
  import { difference } from './difference.mjs';
4
4
  import { symmetricDifference } from './symmetricDifference.mjs';
5
5
 
6
+ /**
7
+ *
8
+ * 数组的一些方法
9
+ *
10
+ * - union 两个数组的并集(排除共有项)
11
+ * - intersection 两个数组的交集(共有项)
12
+ * - difference 两个数组的差集 (A - B)
13
+ * - symmetricDifference 对称差集 ( A △ B)
14
+ *
15
+ */
6
16
  const enArr = {
17
+ /**
18
+ *
19
+ * 数组的并集
20
+ *
21
+ * <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
22
+ * @param arrays - 多个数组
23
+ * @returns 联合后的数组
24
+ *
25
+ *
26
+ * @example
27
+ *
28
+ * ```ts
29
+ * import { union } from 'a-js-tools';
30
+ *
31
+ * const log = console.log;
32
+ *
33
+ * // []
34
+ * log(union());
35
+ *
36
+ * // [1, 2, 3]
37
+ * log(union([1, 2, 3]));
38
+ *
39
+ * // TypeError
40
+ * log(union([1, 2, 3], 'i'));
41
+ * log(union([1, 2, 3], undefined));
42
+ * log(union([1, 2, 3], null));
43
+ * log(union([1, 2, 3], 4));
44
+ * log(union([1, 2, 3], {}));
45
+ * log(union([1, 2, 3], false));
46
+ *
47
+ * // [1, 2, 3, 4, 6]
48
+ * log(union([1, 2, 3], [2, 4, 6]));
49
+ *
50
+ * // [1, 2, 3, 4, 6]
51
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
52
+ *
53
+ * // [1, 2, 3, 4, 6]
54
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
55
+ * ```
56
+ *
57
+ */
7
58
  union,
59
+ /**
60
+ *
61
+ * 两个数组的交集
62
+ *
63
+ * @param a 数组 1️⃣
64
+ * @param b 数组 2️⃣
65
+ * @returns 返回两个数组的交集
66
+ * @example
67
+ *
68
+ * ```ts
69
+ * import { intersection } from 'a-js-tools';
70
+ *
71
+ * const log = console.log;
72
+ *
73
+ *
74
+ * log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
75
+ * log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
76
+ *
77
+ * ```
78
+ *
79
+ */
8
80
  intersection,
81
+ /**
82
+ * 求给出的两个数组的差值(A - B)
83
+ *
84
+ * @param a - 第一个数组
85
+ * @param b - 第二个数组
86
+ * @throws {TypeError} 当两个参数有一个不是
87
+ * @description 当两个参数有一个不是数组时将抛出
88
+ * @returns 返回第一个参数相对第二个参数的差值
89
+ * @example
90
+ *
91
+ * ```ts
92
+ * import { difference} from 'a-js-tools';
93
+ *
94
+ * const log = console.log;
95
+ *
96
+ *
97
+ * log(difference([], [1, 2, 3])); // []
98
+ *
99
+ * log([1, 2, 3], []); //[1, 2, 3]
100
+ *
101
+ * log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
102
+ *
103
+ * // 将抛出 TypeError
104
+ * log(difference([1, 2, 3], 'a'));
105
+ * log(difference([1, 2, 3], 1));
106
+ * log(difference([1, 2, 3], undefined));
107
+ * log(difference([1, 2, 3], null));
108
+ * log(difference([1, 2, 3], true));
109
+ * ```
110
+ *
111
+ */
9
112
  difference,
113
+ /**
114
+ *
115
+ * 对称差集 ( A △ B)
116
+ *
117
+ * @param a - 数组 a
118
+ * @param b - 数组 b
119
+ * @returns - 返回一个全新的数组
120
+ *
121
+ * @example
122
+ *
123
+ * ```ts
124
+ * import { symmetricDifference } from 'a-js-tools';
125
+ *
126
+ * const log = console.log;
127
+ *
128
+ * log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
129
+ *
130
+ * log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
131
+ *
132
+ * log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
133
+ *
134
+ *
135
+ * /// TypeError
136
+ * log(symmetricDifference(1, []));
137
+ * log(symmetricDifference(undefined, []));
138
+ * log(symmetricDifference(null, []));
139
+ * log(symmetricDifference('a', []));
140
+ * log(symmetricDifference(true, []));
141
+ *
142
+ * ```
143
+ *
144
+ */
10
145
  symmetricDifference,
11
146
  };
12
147
 
@@ -2,6 +2,38 @@
2
2
  *
3
3
  * 构建一个 Constructor 构造函数
4
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
+ *
5
37
  */
6
38
  function createConstructor(constructor) {
7
39
  return constructor;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "name": "a-js-tools",
5
5
  "description": "一点点 🤏 js 函数",
6
- "license": "ISC",
6
+ "license": "MIT",
7
7
  "dependencies": {
8
- "a-type-of-js": ">=0.2.0 <1.0.0"
8
+ "a-type-of-js": ">=0.3.0 <1.0.0"
9
9
  },
10
10
  "main": "cjs/index.cjs",
11
11
  "module": "mjs/index.mjs",
@@ -42,7 +42,7 @@
42
42
  "registry": "https://registry.npmjs.org/"
43
43
  },
44
44
  "engines": {
45
- "node": ">=12.0.0"
45
+ "node": ">=15.0.0"
46
46
  },
47
47
  "browserslist": [
48
48
  "last 2 versions not ie <= 11"
@@ -3,9 +3,144 @@ import { union } from './union';
3
3
  import { difference } from './difference';
4
4
  import { symmetricDifference } from './symmetricDifference';
5
5
  export { union, intersection, difference, symmetricDifference };
6
+ /**
7
+ *
8
+ * 数组的一些方法
9
+ *
10
+ * - union 两个数组的并集(排除共有项)
11
+ * - intersection 两个数组的交集(共有项)
12
+ * - difference 两个数组的差集 (A - B)
13
+ * - symmetricDifference 对称差集 ( A △ B)
14
+ *
15
+ */
6
16
  export declare const enArr: {
17
+ /**
18
+ *
19
+ * 数组的并集
20
+ *
21
+ * <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
22
+ * @param arrays - 多个数组
23
+ * @returns 联合后的数组
24
+ *
25
+ *
26
+ * @example
27
+ *
28
+ * ```ts
29
+ * import { union } from 'a-js-tools';
30
+ *
31
+ * const log = console.log;
32
+ *
33
+ * // []
34
+ * log(union());
35
+ *
36
+ * // [1, 2, 3]
37
+ * log(union([1, 2, 3]));
38
+ *
39
+ * // TypeError
40
+ * log(union([1, 2, 3], 'i'));
41
+ * log(union([1, 2, 3], undefined));
42
+ * log(union([1, 2, 3], null));
43
+ * log(union([1, 2, 3], 4));
44
+ * log(union([1, 2, 3], {}));
45
+ * log(union([1, 2, 3], false));
46
+ *
47
+ * // [1, 2, 3, 4, 6]
48
+ * log(union([1, 2, 3], [2, 4, 6]));
49
+ *
50
+ * // [1, 2, 3, 4, 6]
51
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
52
+ *
53
+ * // [1, 2, 3, 4, 6]
54
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
55
+ * ```
56
+ *
57
+ */
7
58
  union: typeof union;
59
+ /**
60
+ *
61
+ * 两个数组的交集
62
+ *
63
+ * @param a 数组 1️⃣
64
+ * @param b 数组 2️⃣
65
+ * @returns 返回两个数组的交集
66
+ * @example
67
+ *
68
+ * ```ts
69
+ * import { intersection } from 'a-js-tools';
70
+ *
71
+ * const log = console.log;
72
+ *
73
+ *
74
+ * log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
75
+ * log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
76
+ *
77
+ * ```
78
+ *
79
+ */
8
80
  intersection: typeof intersection;
81
+ /**
82
+ * 求给出的两个数组的差值(A - B)
83
+ *
84
+ * @param a - 第一个数组
85
+ * @param b - 第二个数组
86
+ * @throws {TypeError} 当两个参数有一个不是
87
+ * @description 当两个参数有一个不是数组时将抛出
88
+ * @returns 返回第一个参数相对第二个参数的差值
89
+ * @example
90
+ *
91
+ * ```ts
92
+ * import { difference} from 'a-js-tools';
93
+ *
94
+ * const log = console.log;
95
+ *
96
+ *
97
+ * log(difference([], [1, 2, 3])); // []
98
+ *
99
+ * log([1, 2, 3], []); //[1, 2, 3]
100
+ *
101
+ * log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
102
+ *
103
+ * // 将抛出 TypeError
104
+ * log(difference([1, 2, 3], 'a'));
105
+ * log(difference([1, 2, 3], 1));
106
+ * log(difference([1, 2, 3], undefined));
107
+ * log(difference([1, 2, 3], null));
108
+ * log(difference([1, 2, 3], true));
109
+ * ```
110
+ *
111
+ */
9
112
  difference: typeof difference;
113
+ /**
114
+ *
115
+ * 对称差集 ( A △ B)
116
+ *
117
+ * @param a - 数组 a
118
+ * @param b - 数组 b
119
+ * @returns - 返回一个全新的数组
120
+ *
121
+ * @example
122
+ *
123
+ * ```ts
124
+ * import { symmetricDifference } from 'a-js-tools';
125
+ *
126
+ * const log = console.log;
127
+ *
128
+ * log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
129
+ *
130
+ * log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
131
+ *
132
+ * log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
133
+ *
134
+ *
135
+ * /// TypeError
136
+ * log(symmetricDifference(1, []));
137
+ * log(symmetricDifference(undefined, []));
138
+ * log(symmetricDifference(null, []));
139
+ * log(symmetricDifference('a', []));
140
+ * log(symmetricDifference(true, []));
141
+ *
142
+ * ```
143
+ *
144
+ */
10
145
  symmetricDifference: typeof symmetricDifference;
11
146
  };
@@ -2,6 +2,7 @@
2
2
  *
3
3
  * 构建构建的构建函数
4
4
  *
5
+ *
5
6
  */
6
7
  export interface CreateConstructor<T, Args extends unknown[] = unknown[]> {
7
8
  new (...args: Args): T;
@@ -10,5 +11,37 @@ export interface CreateConstructor<T, Args extends unknown[] = unknown[]> {
10
11
  *
11
12
  * 构建一个 Constructor 构造函数
12
13
  *
14
+ * 接收一个构造函数,然后返回 TS 能识别的构造函数自身
15
+ *
16
+ * 函数本身并没有执行任何逻辑,仅是简单的返回了实参自身
17
+ *
18
+ * 而经过该函数的包装,构造函数成了能够被 TS 识别为可用 new 实例的构造函数
19
+ *
20
+ * @param constructor - 传入一个构造函数
21
+ * @returns 返回传入的构造函数
22
+ *
23
+ * ```ts
24
+ * import { createConstructor } from "a-js-tools";
25
+ *
26
+ * type Tom = {
27
+ * a: number
28
+ * }
29
+ *
30
+ * function _Tom (this: TomType): Tom {
31
+ * this.a = 1;
32
+ *
33
+ * return this;
34
+ * }
35
+ *
36
+ * // 逻辑上没有错,但是会造成 ts 显示
37
+ * // 其目标缺少构造签名的 "new" 表达式隐式具有 "any" 类型。ts(7009)
38
+ * const a = new _Tom();
39
+ *
40
+ * const tomConstructor = createConstructor(_tom);
41
+ *
42
+ * const b = new tomConstructor(); // 这时就不会显示错误
43
+ *
44
+ * ```
45
+ *
13
46
  */
14
47
  export declare function createConstructor<T, Args extends unknown[] = unknown[]>(constructor: (...argumentList: Args) => T): CreateConstructor<T, Args>;