@wener/utils 1.1.47 → 1.1.49
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/README.md +9 -1
- package/lib/asyncs/Promises.js +54 -0
- package/lib/asyncs/Promises.js.map +1 -0
- package/lib/asyncs/createLazyPromise.js +52 -51
- package/lib/asyncs/createLazyPromise.js.map +1 -1
- package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
- package/lib/asyncs/isPromise.js +2 -3
- package/lib/asyncs/isPromise.js.map +1 -1
- package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
- package/lib/asyncs/timeout.js +4 -4
- package/lib/asyncs/timeout.js.map +1 -1
- package/lib/cn/division/DivisionCode.js +30 -17
- package/lib/cn/division/DivisionCode.js.map +1 -1
- package/lib/cn/id/{Mod11Checksum.js → Mod11.js} +3 -6
- package/lib/cn/id/Mod11.js.map +1 -0
- package/lib/cn/id/ResidentIdNumber.js +10 -12
- package/lib/cn/id/ResidentIdNumber.js.map +1 -1
- package/lib/cn/types.js +3 -0
- package/lib/cn/types.js.map +1 -0
- package/lib/cn/uscc/{Mod31Checksum.js → Mod31.js} +3 -6
- package/lib/cn/uscc/Mod31.js.map +1 -0
- package/lib/cn/uscc/USCC.js +6 -9
- package/lib/cn/uscc/USCC.js.map +1 -1
- package/lib/cn/uscc/isUSCC.js +1 -1
- package/lib/cn/uscc/isUSCC.js.map +1 -1
- package/lib/crypto/getNodeCrypto.js +14 -10
- package/lib/crypto/getNodeCrypto.js.map +1 -1
- package/lib/crypto/getRandomValues.js +5 -1
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/emitter/types.js +3 -0
- package/lib/emitter/types.js.map +1 -0
- package/lib/fetch/createFetchWith.js.map +1 -1
- package/lib/fetch/createFetchWithRetry.js.map +1 -1
- package/lib/fetch/http.types.js +4 -0
- package/lib/fetch/http.types.js.map +1 -0
- package/lib/index.js +6 -3
- package/lib/index.js.map +1 -1
- package/lib/io/ArrayBuffers.js.map +1 -1
- package/lib/io/ByteBuffer.js +235 -36
- package/lib/io/ByteBuffer.js.map +1 -1
- package/lib/langs/getGlobalStates.js +17 -0
- package/lib/langs/getGlobalStates.js.map +1 -0
- package/lib/langs/getObjectId.js +18 -0
- package/lib/langs/getObjectId.js.map +1 -0
- package/lib/langs/isNullish.js +5 -0
- package/lib/langs/isNullish.js.map +1 -0
- package/lib/logging/slog.js.map +1 -1
- package/lib/maths/clamp.js +7 -6
- package/lib/maths/clamp.js.map +1 -1
- package/lib/maths/random.js.map +1 -1
- package/lib/mitt/index.js +77 -0
- package/lib/mitt/index.js.map +1 -0
- package/lib/objects/computeIfAbsent.js +1 -1
- package/lib/objects/computeIfAbsent.js.map +1 -1
- package/lib/schema/typebox/typebox.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
- package/package.json +6 -14
- package/src/asyncs/Promises.ts +62 -0
- package/src/asyncs/createLazyPromise.test.ts +52 -13
- package/src/asyncs/createLazyPromise.ts +66 -61
- package/src/asyncs/firstOfAsyncIterator.ts +1 -1
- package/src/asyncs/isPromise.ts +3 -3
- package/src/asyncs/nextOfAsyncIterator.ts +1 -1
- package/src/asyncs/timeout.ts +4 -4
- package/src/cn/division/DivisionCode.ts +61 -8
- package/src/cn/division/division.test.ts +6 -0
- package/src/cn/id/{Mod11Checksum.ts → Mod11.ts} +3 -6
- package/src/cn/id/ResidentIdNumber.ts +11 -13
- package/src/cn/id/id.test.ts +5 -5
- package/src/cn/scripts/gen.test.ts +2 -1
- package/src/cn/types.ts +11 -0
- package/src/cn/uscc/{Mod31Checksum.ts → Mod31.ts} +3 -7
- package/src/cn/uscc/USCC.ts +7 -11
- package/src/cn/uscc/isUSCC.ts +1 -1
- package/src/cn/uscc/uscc.test.ts +4 -4
- package/src/crypto/getNodeCrypto.ts +16 -11
- package/src/crypto/getRandomValues.ts +6 -1
- package/src/emitter/types.ts +18 -0
- package/src/fetch/createFetchWith.ts +1 -1
- package/src/fetch/createFetchWithRetry.ts +1 -1
- package/src/fetch/http.types.ts +46 -0
- package/src/index.ts +7 -3
- package/src/io/ArrayBuffers.ts +7 -3
- package/src/io/ByteBuffer.test.ts +33 -6
- package/src/io/ByteBuffer.ts +272 -41
- package/src/langs/getGlobalStates.ts +21 -0
- package/src/langs/getObjectId.ts +20 -0
- package/src/langs/isNullish.ts +3 -0
- package/src/langs/mixin.test.ts +43 -0
- package/src/logging/slog.ts +1 -1
- package/src/maths/clamp.test.ts +5 -1
- package/src/maths/clamp.ts +7 -7
- package/src/maths/random.ts +1 -1
- package/src/mitt/README.md +1 -0
- package/src/mitt/index.ts +114 -0
- package/src/objects/computeIfAbsent.ts +3 -2
- package/src/objects/merge/merge.test.ts +1 -1
- package/src/schema/typebox/typebox.ts +1 -1
- package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
- package/tsconfig.json +4 -1
- package/lib/asyncs/sleep.js +0 -3
- package/lib/asyncs/sleep.js.map +0 -1
- package/lib/cn/id/Mod11Checksum.js.map +0 -1
- package/lib/cn/uscc/Mod31Checksum.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
- package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
- package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
- package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
- package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
- package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
- package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
- package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/index.js +0 -29
- package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
- package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
- package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
- package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/index.js +0 -3
- package/lib/schema/typebox/gen/index.js.map +0 -1
- package/src/asyncs/sleep.ts +0 -1
- package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
- package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
- package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
- package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
- package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
- package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
- package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
- package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
- package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
- package/src/schema/typebox/gen/codegen/index.ts +0 -29
- package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
- package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
- package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
- package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
- package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
- package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
- package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
- package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
- package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
- package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
- package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
- package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
- package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
- package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
- package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
- package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
- package/src/schema/typebox/gen/gen.test.ts +0 -12
- package/src/schema/typebox/gen/index.ts +0 -1
- /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
|
@@ -39,14 +39,8 @@ const DivisionCodeLevels = [
|
|
|
39
39
|
*
|
|
40
40
|
* @see https://zh.wikipedia.org/wiki/GB/T_2260 中华人民共和国行政区划代码
|
|
41
41
|
*/
|
|
42
|
-
export class
|
|
43
|
-
static
|
|
44
|
-
|
|
45
|
-
private static instance: DivisionCode;
|
|
46
|
-
|
|
47
|
-
static get() {
|
|
48
|
-
return this.instance || (this.instance = new DivisionCode());
|
|
49
|
-
}
|
|
42
|
+
export class DivisionCodeFormat {
|
|
43
|
+
static levels = DivisionCodeLevels;
|
|
50
44
|
|
|
51
45
|
regex = /^(?<province>\d{2})(?<city>\d{2})?(?<county>\d{2})?(?<town>\d{3})?(?<village>\d{3})?$/;
|
|
52
46
|
|
|
@@ -87,6 +81,63 @@ export class DivisionCode {
|
|
|
87
81
|
[82, '澳门特别行政区'],
|
|
88
82
|
// 9 国外
|
|
89
83
|
];
|
|
84
|
+
|
|
85
|
+
parse(code: string) {
|
|
86
|
+
if (!code) return;
|
|
87
|
+
const { regex } = this;
|
|
88
|
+
const match = regex.exec(code);
|
|
89
|
+
if (!match) return;
|
|
90
|
+
const { province, city, county, town, village } = match.groups ?? {};
|
|
91
|
+
if (!province) return;
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
province,
|
|
95
|
+
city,
|
|
96
|
+
county,
|
|
97
|
+
town,
|
|
98
|
+
village,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface DivisionTreeNode {
|
|
104
|
+
sub: string; // sub code
|
|
105
|
+
children?: Record<string, DivisionTreeNode>;
|
|
106
|
+
|
|
107
|
+
code: string; // full code
|
|
108
|
+
name?: string; // name of division
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export type DivisionCodeLevel = 'Village' | 'Town' | 'County' | 'City' | 'Province';
|
|
112
|
+
|
|
113
|
+
export interface ParsedDivisionCode {
|
|
114
|
+
level: DivisionCodeLevel;
|
|
115
|
+
code: string;
|
|
116
|
+
name?: string;
|
|
117
|
+
fullName?: string;
|
|
118
|
+
names: string[];
|
|
119
|
+
// 村级(村委会、居委会)
|
|
120
|
+
// 12 位
|
|
121
|
+
village?: CodeName;
|
|
122
|
+
// 乡级(乡镇、街道)
|
|
123
|
+
// 9 位
|
|
124
|
+
town?: CodeName;
|
|
125
|
+
// 县级(区县)
|
|
126
|
+
// 6 位 - 常用 - 2985 个
|
|
127
|
+
county?: CodeName;
|
|
128
|
+
// 地级(城市)
|
|
129
|
+
// 4 位 - 343 个
|
|
130
|
+
city?: CodeName;
|
|
131
|
+
// 省级(省份、直辖市、自治区)
|
|
132
|
+
// 2 位 - 32 个
|
|
133
|
+
province: CodeName;
|
|
134
|
+
|
|
135
|
+
children?: Array<{ code: string; name?: string }>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface CodeName {
|
|
139
|
+
code: string;
|
|
140
|
+
name: string;
|
|
90
141
|
}
|
|
91
142
|
|
|
92
143
|
// export function getSimpleProvinceLabel(value: string) {
|
|
@@ -100,3 +151,5 @@ export class DivisionCode {
|
|
|
100
151
|
// }
|
|
101
152
|
// return label.replace(/省|市|(回族|维吾尔|壮族)?自治区|特别行政区$/, '');
|
|
102
153
|
// }
|
|
154
|
+
|
|
155
|
+
export const DivisionCode = new DivisionCodeFormat();
|
|
@@ -3,13 +3,8 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export class Mod11Checksum {
|
|
5
5
|
weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
|
|
6
|
-
private static instance: Mod11Checksum;
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
return this.instance || (this.instance = new Mod11Checksum());
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
verify(s: string) {
|
|
7
|
+
validate(s: string) {
|
|
13
8
|
return s.at(-1) === this.generate(s.slice(0, s.length - 1));
|
|
14
9
|
}
|
|
15
10
|
|
|
@@ -27,3 +22,5 @@ export class Mod11Checksum {
|
|
|
27
22
|
}
|
|
28
23
|
}
|
|
29
24
|
}
|
|
25
|
+
|
|
26
|
+
export const Mod11 = new Mod11Checksum();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Mod11 } from './Mod11';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
checksum =
|
|
3
|
+
class ResidentIdNumberFormat {
|
|
4
|
+
checksum = Mod11;
|
|
5
5
|
|
|
6
6
|
// 9 https://zh.wikipedia.org/wiki/中华人民共和国外国人永久居留身份证
|
|
7
7
|
// 9 标识码
|
|
@@ -11,19 +11,15 @@ export class ResidentIdNumber {
|
|
|
11
11
|
regex =
|
|
12
12
|
/^(?<division>[1-9]\d{5})(?<year>18|19|20)\d{2}(?<month>0[1-9]|1[0-2])(?<day>0[1-9]|[12]\d|3[01])(?<sequence>\d{3})(?<checksum>[0-9Xx])$/;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
static get() {
|
|
17
|
-
return (this.instance ||= new ResidentIdNumber());
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
verify(s: string) {
|
|
14
|
+
validate(s: string) {
|
|
21
15
|
if (!s) return false;
|
|
22
|
-
return this.regex.test(s) && this.checksum.
|
|
16
|
+
return this.regex.test(s) && this.checksum.validate(s.toUpperCase());
|
|
23
17
|
}
|
|
24
18
|
|
|
19
|
+
generate(opts: { sex?: 'Male' | 'Female'; age?: number; birthDate?: Date; division?: string }) {}
|
|
20
|
+
|
|
25
21
|
parse(s: string): IdNumber | undefined {
|
|
26
|
-
if (!this.
|
|
22
|
+
if (!this.validate(s)) {
|
|
27
23
|
return;
|
|
28
24
|
}
|
|
29
25
|
|
|
@@ -86,7 +82,7 @@ class IdNumber {
|
|
|
86
82
|
}
|
|
87
83
|
|
|
88
84
|
get valid() {
|
|
89
|
-
return ResidentIdNumber.
|
|
85
|
+
return ResidentIdNumber.validate(this.toString());
|
|
90
86
|
}
|
|
91
87
|
|
|
92
88
|
get male() {
|
|
@@ -126,3 +122,5 @@ function formatDate(date: Date, format = 'YYYYMMDD') {
|
|
|
126
122
|
throw new Error(`Invalid format`);
|
|
127
123
|
}
|
|
128
124
|
}
|
|
125
|
+
|
|
126
|
+
export const ResidentIdNumber = new ResidentIdNumberFormat();
|
package/src/cn/id/id.test.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { assert, expect, test } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import { Mod11 } from './Mod11';
|
|
3
3
|
import { ResidentIdNumber } from './ResidentIdNumber';
|
|
4
4
|
|
|
5
5
|
test('id', (t) => {
|
|
6
6
|
const ids = ['11010519491231002X', '11010219840406970X'];
|
|
7
7
|
|
|
8
|
-
let cs =
|
|
8
|
+
let cs = Mod11;
|
|
9
9
|
for (const id of ids) {
|
|
10
10
|
assert.equal(cs.generate(id.slice(0, -1)), id.at(-1));
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
let parse = ResidentIdNumber.
|
|
14
|
-
expect(parse?.toObject()).
|
|
13
|
+
let parse = ResidentIdNumber.parse(ids[0]);
|
|
14
|
+
expect(parse?.toObject()).toMatchObject({
|
|
15
15
|
division: '110105',
|
|
16
|
-
|
|
16
|
+
birthDate: new Date('1949-12-31'),
|
|
17
17
|
sequence: 2,
|
|
18
18
|
checksum: 'X',
|
|
19
19
|
});
|
package/src/cn/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface StringFormat<GenerateOptions, ParsedObject> {
|
|
2
|
+
regex?: RegExp;
|
|
3
|
+
validate: (s: string) => boolean;
|
|
4
|
+
generate: (opts?: GenerateOptions) => ParsedObject;
|
|
5
|
+
parse: (s: string) => ParsedObject | undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface StringChecksum {
|
|
9
|
+
validate: (s: string) => boolean;
|
|
10
|
+
generate: (s: string) => string;
|
|
11
|
+
}
|
|
@@ -14,12 +14,6 @@ export class Mod31Checksum {
|
|
|
14
14
|
{} as Record<string, number>,
|
|
15
15
|
);
|
|
16
16
|
|
|
17
|
-
private static instance: Mod31Checksum;
|
|
18
|
-
|
|
19
|
-
static get() {
|
|
20
|
-
return this.instance || (this.instance = new Mod31Checksum());
|
|
21
|
-
}
|
|
22
|
-
|
|
23
17
|
toChar(n: number) {
|
|
24
18
|
return this.chars[n];
|
|
25
19
|
}
|
|
@@ -28,7 +22,7 @@ export class Mod31Checksum {
|
|
|
28
22
|
return this.numbers[c];
|
|
29
23
|
}
|
|
30
24
|
|
|
31
|
-
|
|
25
|
+
validate(s: string) {
|
|
32
26
|
return s.at(-1) === this.generate(s.slice(0, s.length - 1));
|
|
33
27
|
}
|
|
34
28
|
|
|
@@ -40,3 +34,5 @@ export class Mod31Checksum {
|
|
|
40
34
|
return this.toChar(31 - (sum % 31));
|
|
41
35
|
}
|
|
42
36
|
}
|
|
37
|
+
|
|
38
|
+
export const Mod31 = new Mod31Checksum();
|
package/src/cn/uscc/USCC.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Mod31 } from './Mod31';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* - GB 32100-2015 法人和其他组织统一社会信用代码编码规则
|
|
@@ -8,7 +8,7 @@ import { Mod31Checksum } from './Mod31Checksum';
|
|
|
8
8
|
*
|
|
9
9
|
* @see https://en.wikipedia.org/wiki/Unified_Social_Credit_Identifier
|
|
10
10
|
*/
|
|
11
|
-
export class
|
|
11
|
+
export class UnifiedSocialCreditCodeFormat {
|
|
12
12
|
registryCodeLabels: Record<string, Code> = {
|
|
13
13
|
1: {
|
|
14
14
|
label: '机构编制',
|
|
@@ -47,13 +47,7 @@ export class USCC {
|
|
|
47
47
|
|
|
48
48
|
/* 16 位 */
|
|
49
49
|
regex = /^([159][1239]|Y1)[0-9]{6}[0-9A-HJ-NP-RTUWXY]{10}$/; // 无 I O Z S V
|
|
50
|
-
checksum =
|
|
51
|
-
|
|
52
|
-
static instance: USCC;
|
|
53
|
-
|
|
54
|
-
static get() {
|
|
55
|
-
return (this.instance ||= new USCC());
|
|
56
|
-
}
|
|
50
|
+
checksum = Mod31;
|
|
57
51
|
|
|
58
52
|
parse(s: string): ParsedUSCC {
|
|
59
53
|
let registryCode = s[0];
|
|
@@ -71,8 +65,8 @@ export class USCC {
|
|
|
71
65
|
};
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
|
|
75
|
-
return this.regex.test(s) && this.checksum.
|
|
68
|
+
validate(s: string) {
|
|
69
|
+
return this.regex.test(s) && this.checksum.validate(s);
|
|
76
70
|
}
|
|
77
71
|
}
|
|
78
72
|
|
|
@@ -96,3 +90,5 @@ interface ParsedUSCC {
|
|
|
96
90
|
// 校验码
|
|
97
91
|
checksum: string;
|
|
98
92
|
}
|
|
93
|
+
|
|
94
|
+
export const USCC = new UnifiedSocialCreditCodeFormat();
|
package/src/cn/uscc/isUSCC.ts
CHANGED
package/src/cn/uscc/uscc.test.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { expect, test } from 'vitest';
|
|
2
2
|
import { isUSCC } from './isUSCC';
|
|
3
|
-
import {
|
|
3
|
+
import { Mod31 } from './Mod31';
|
|
4
4
|
import { USCC } from './USCC';
|
|
5
5
|
|
|
6
6
|
test('uscc', () => {
|
|
7
|
-
let cs =
|
|
7
|
+
let cs = Mod31;
|
|
8
8
|
|
|
9
9
|
// 阿里云计算
|
|
10
|
-
expect(cs.
|
|
10
|
+
expect(cs.validate('91330106673959654P')).toBeTruthy();
|
|
11
11
|
expect(cs.generate('91330106673959654')).toBe('P');
|
|
12
12
|
|
|
13
13
|
expect(isUSCC('91330106673959654P')).toBeTruthy();
|
|
14
14
|
|
|
15
|
-
console.log(USCC.
|
|
15
|
+
console.log(USCC.parse('91330106673959654P'));
|
|
16
16
|
});
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
// import type * as NodeCrypto from 'node:crypto';
|
|
2
|
+
import { getGlobalThis } from '../runtime/getGlobalThis';
|
|
3
|
+
|
|
4
|
+
let nodeCrypto;
|
|
2
5
|
// globalThis.process?.release?.name
|
|
3
6
|
|
|
7
|
+
// avoid import node
|
|
4
8
|
// avoid process.browser
|
|
5
|
-
if (typeof window === 'undefined') {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
9
|
+
// if (typeof window === 'undefined') {
|
|
10
|
+
// try {
|
|
11
|
+
// // avoid `node:` UnhandledSchemeError https://github.com/vercel/next.js/issues/28774
|
|
12
|
+
// if (typeof require === 'undefined') {
|
|
13
|
+
// void import('crypto').then((v) => (nodeCrypto = v.default));
|
|
14
|
+
// } else {
|
|
15
|
+
// nodeCrypto = require('crypto');
|
|
16
|
+
// }
|
|
17
|
+
// } catch (e) {}
|
|
18
|
+
// }
|
|
15
19
|
export function getNodeCrypto() {
|
|
20
|
+
nodeCrypto ||= getGlobalThis().crypto;
|
|
16
21
|
return nodeCrypto;
|
|
17
22
|
}
|
|
@@ -4,10 +4,15 @@ import { getGlobalThis } from '../runtime/getGlobalThis';
|
|
|
4
4
|
import { getNodeCrypto } from './getNodeCrypto';
|
|
5
5
|
|
|
6
6
|
const globalThis = getGlobalThis();
|
|
7
|
+
|
|
8
|
+
// chrome 11+, safari 5+, nodejs 17.4+
|
|
9
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
|
7
10
|
export let getRandomValues: <T extends Exclude<TypedArray, Float32Array | Float64Array>>(typedArray: T) => T =
|
|
8
11
|
globalThis.crypto?.getRandomValues?.bind(globalThis.crypto) ||
|
|
9
12
|
(globalThis as any).msCrypto?.getRandomValues?.bind((globalThis as any).msCrypto) ||
|
|
10
|
-
|
|
13
|
+
(() => {
|
|
14
|
+
throw new Error('[getRandomValues]: No secure random number generator available.');
|
|
15
|
+
});
|
|
11
16
|
|
|
12
17
|
function _getRandomValues<T extends Exclude<TypedArray, Float32Array | Float64Array>>(buf: T) {
|
|
13
18
|
const nodeCrypto = getNodeCrypto();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type EventType = string | symbol;
|
|
2
|
+
|
|
3
|
+
export type Handler<E extends keyof T, T = Record<string, unknown>> = (evt: { type: E; payload: T[E] }) => void;
|
|
4
|
+
export type WildcardHandler<T = Record<string, unknown>> = <E extends keyof T>(evt: { type: E; payload: T[E] }) => void;
|
|
5
|
+
|
|
6
|
+
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
7
|
+
on<Key extends keyof Events>(type: Key, handler: Handler<Key, Events>): void;
|
|
8
|
+
|
|
9
|
+
on(type: '*', handler: WildcardHandler<Events>): void;
|
|
10
|
+
|
|
11
|
+
off<Key extends keyof Events>(type: Key, handler?: Handler<Key, Events>): void;
|
|
12
|
+
|
|
13
|
+
off(type: '*', handler: WildcardHandler<Events>): void;
|
|
14
|
+
|
|
15
|
+
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
|
|
16
|
+
|
|
17
|
+
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
|
|
18
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getGlobalThis } from '../runtime/getGlobalThis';
|
|
2
|
-
import { FetchLike } from './types';
|
|
2
|
+
import type { FetchLike } from './types';
|
|
3
3
|
|
|
4
4
|
type RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;
|
|
5
5
|
type RequestRetryOnFunction = (
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// from hono
|
|
2
|
+
|
|
3
|
+
export type InfoStatusCode = 100 | 101 | 102 | 103;
|
|
4
|
+
export type SuccessStatusCode = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226;
|
|
5
|
+
export type DeprecatedStatusCode = 305 | 306;
|
|
6
|
+
export type RedirectStatusCode = 300 | 301 | 302 | 303 | 304 | DeprecatedStatusCode | 307 | 308;
|
|
7
|
+
export type ClientErrorStatusCode =
|
|
8
|
+
| 400
|
|
9
|
+
| 401
|
|
10
|
+
| 402
|
|
11
|
+
| 403
|
|
12
|
+
| 404
|
|
13
|
+
| 405
|
|
14
|
+
| 406
|
|
15
|
+
| 407
|
|
16
|
+
| 408
|
|
17
|
+
| 409
|
|
18
|
+
| 410
|
|
19
|
+
| 411
|
|
20
|
+
| 412
|
|
21
|
+
| 413
|
|
22
|
+
| 414
|
|
23
|
+
| 415
|
|
24
|
+
| 416
|
|
25
|
+
| 417
|
|
26
|
+
| 418
|
|
27
|
+
| 421
|
|
28
|
+
| 422
|
|
29
|
+
| 423
|
|
30
|
+
| 424
|
|
31
|
+
| 425
|
|
32
|
+
| 426
|
|
33
|
+
| 428
|
|
34
|
+
| 429
|
|
35
|
+
| 431
|
|
36
|
+
| 451;
|
|
37
|
+
export type ServerErrorStatusCode = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
|
|
38
|
+
|
|
39
|
+
export type UnofficialStatusCode = -1;
|
|
40
|
+
export type StatusCode =
|
|
41
|
+
| InfoStatusCode
|
|
42
|
+
| SuccessStatusCode
|
|
43
|
+
| RedirectStatusCode
|
|
44
|
+
| ClientErrorStatusCode
|
|
45
|
+
| ServerErrorStatusCode
|
|
46
|
+
| UnofficialStatusCode;
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Promises } from './asyncs/Promises';
|
|
2
|
+
|
|
1
3
|
// arrays
|
|
2
4
|
export {
|
|
3
5
|
firstOfMaybeArray,
|
|
@@ -25,10 +27,10 @@ export { firstOfAsyncIterator } from './asyncs/firstOfAsyncIterator';
|
|
|
25
27
|
export { nextOfAsyncIterator } from './asyncs/nextOfAsyncIterator';
|
|
26
28
|
export { isIterator } from './asyncs/isIterator';
|
|
27
29
|
|
|
28
|
-
export {
|
|
30
|
+
export { Promises } from './asyncs/Promises';
|
|
31
|
+
export const sleep = Promises.sleep;
|
|
32
|
+
export const isPromise = Promises.isPromise;
|
|
29
33
|
export { timeout, TimeoutError } from './asyncs/timeout';
|
|
30
|
-
export { isPromise } from './asyncs/isPromise';
|
|
31
|
-
// export * from './async/promiseOfCallback';
|
|
32
34
|
|
|
33
35
|
// langs
|
|
34
36
|
export { shallowEqual } from './langs/shallowEqual';
|
|
@@ -45,6 +47,8 @@ export { maybeFunction, type MaybeFunction } from './langs/MaybeFunction';
|
|
|
45
47
|
export { memoize } from './langs/memoize';
|
|
46
48
|
export { mixin } from './langs/mixin';
|
|
47
49
|
export type { MixinFunction, MixinInstance, MixinReturnValue } from './langs/mixin';
|
|
50
|
+
export { getObjectId } from './langs/getObjectId';
|
|
51
|
+
export { getGlobalStates, setGlobalStates } from './langs/getGlobalStates';
|
|
48
52
|
|
|
49
53
|
export { AsyncCloser } from './runtime/AsyncCloser';
|
|
50
54
|
export { Closer } from './runtime/Closer';
|
package/src/io/ArrayBuffers.ts
CHANGED
|
@@ -157,7 +157,7 @@ export class ArrayBuffers {
|
|
|
157
157
|
return this.toUint8Array(ArrayBuffers.from(v, 'hex'));
|
|
158
158
|
};
|
|
159
159
|
|
|
160
|
-
static resize = (v:
|
|
160
|
+
static resize = (v: ArrayBuffer, newByteLength?: number, maxByteLength?: number): ArrayBuffer => {
|
|
161
161
|
if (newByteLength === undefined || newByteLength === null) {
|
|
162
162
|
return v;
|
|
163
163
|
}
|
|
@@ -167,7 +167,7 @@ export class ArrayBuffers {
|
|
|
167
167
|
if ('resizable' in v && v.resizable) {
|
|
168
168
|
if ('maxByteLength' in v && typeof v.maxByteLength === 'number' && v.maxByteLength >= newByteLength) {
|
|
169
169
|
v.resize(newByteLength);
|
|
170
|
-
return v;
|
|
170
|
+
return v as ArrayBuffer;
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
173
|
}
|
|
@@ -367,7 +367,7 @@ export class ArrayBuffers {
|
|
|
367
367
|
// return a;
|
|
368
368
|
// };
|
|
369
369
|
|
|
370
|
-
static concat = (buffers: Array<BufferSource>, result?: ArrayBuffer, offset = 0) => {
|
|
370
|
+
static concat = (buffers: Array<BufferSource>, result?: ArrayBuffer, offset = 0): ArrayBuffer => {
|
|
371
371
|
// https://stackoverflow.com/questions/10786128/appending-arraybuffers
|
|
372
372
|
|
|
373
373
|
const length = buffers.reduce((a, b) => a + b.byteLength, 0);
|
|
@@ -422,6 +422,10 @@ declare global {
|
|
|
422
422
|
resizable?: boolean;
|
|
423
423
|
}
|
|
424
424
|
|
|
425
|
+
interface ArrayBufferConstructor {
|
|
426
|
+
new (byteLength: number, opts?: { maxByteLength?: number }): ArrayBuffer;
|
|
427
|
+
}
|
|
428
|
+
|
|
425
429
|
interface SharedArrayBuffer {
|
|
426
430
|
resize?: (newByteLength: number) => void;
|
|
427
431
|
resizable?: boolean;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { assert, test } from 'vitest';
|
|
1
|
+
import { assert, describe, test } from 'vitest';
|
|
2
2
|
import { ByteBuffer } from './ByteBuffer';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
{
|
|
4
|
+
describe('ByteBuffer', async () => {
|
|
5
|
+
test('base', () => {
|
|
6
6
|
let buf = new ByteBuffer();
|
|
7
7
|
buf.writeString('Hello');
|
|
8
8
|
buf.writeString('World');
|
|
@@ -12,9 +12,36 @@ test('ByteBuffer', async () => {
|
|
|
12
12
|
assert.equal(buf.length, 5);
|
|
13
13
|
buf.position = 0;
|
|
14
14
|
assert.equal(buf.readString(5), 'Hello');
|
|
15
|
-
}
|
|
16
|
-
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('overflow max length', () => {
|
|
17
18
|
let buf = new ByteBuffer();
|
|
18
19
|
buf.writeBytes(new Uint8Array(1025));
|
|
19
|
-
|
|
20
|
+
assert.equal(buf.length, 1025);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('overflow max length tow step', () => {
|
|
24
|
+
let buf = new ByteBuffer();
|
|
25
|
+
buf.writeBytes(new Uint8Array(1023));
|
|
26
|
+
buf.writeBytes(new Uint8Array(2));
|
|
27
|
+
assert.equal(buf.length, 1025);
|
|
28
|
+
console.log(`Max`, (buf.buffer as any).maxByteLength);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('writeBytes fill zero', () => {
|
|
32
|
+
let buf = new ByteBuffer();
|
|
33
|
+
buf.writeBytes(new Uint8Array([1, 2]));
|
|
34
|
+
buf.position = 0;
|
|
35
|
+
// console.log('->', buf.position, buf.length, buf.remaining());
|
|
36
|
+
// console.log(toHexDump(buf.buffer));
|
|
37
|
+
|
|
38
|
+
// truncate to size fill zero
|
|
39
|
+
buf.writeBytes(new Uint8Array([1]), 10);
|
|
40
|
+
// console.log('->', buf.position, buf.length, buf.remaining());
|
|
41
|
+
// console.log(toHexDump(buf.buffer));
|
|
42
|
+
|
|
43
|
+
assert.equal(buf.view.getInt8(0), 1);
|
|
44
|
+
assert.equal(buf.view.getInt8(1), 0);
|
|
45
|
+
assert.equal(buf.length, 10);
|
|
46
|
+
});
|
|
20
47
|
});
|