@wener/common 1.0.3 → 1.0.5
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/lib/cn/DivisionCode.js.map +1 -1
- package/lib/cn/Mod11Checksum.js.map +1 -1
- package/lib/cn/Mod31Checksum.js.map +1 -1
- package/lib/cn/ResidentIdentityCardNumber.js.map +1 -1
- package/lib/cn/UnifiedSocialCreditCode.js.map +1 -1
- package/lib/cn/formatDate.js.map +1 -1
- package/lib/cn/parseSex.js.map +1 -1
- package/lib/cn/types.d.js.map +1 -1
- package/lib/consola/createStandardConsolaReporter.js +18 -0
- package/lib/consola/createStandardConsolaReporter.js.map +1 -0
- package/lib/consola/formatLogObject.js +125 -0
- package/lib/consola/formatLogObject.js.map +1 -0
- package/lib/consola/index.js +3 -0
- package/lib/consola/index.js.map +1 -0
- package/lib/data/formatSort.js +15 -0
- package/lib/data/formatSort.js.map +1 -0
- package/lib/data/index.js +4 -0
- package/lib/data/index.js.map +1 -0
- package/lib/data/maybeNumber.js +22 -0
- package/lib/data/maybeNumber.js.map +1 -0
- package/lib/data/parseSort.js +95 -0
- package/lib/data/parseSort.js.map +1 -0
- package/lib/data/resolvePagination.js +36 -0
- package/lib/data/resolvePagination.js.map +1 -0
- package/lib/data/types.d.js +3 -0
- package/lib/data/types.d.js.map +1 -0
- package/lib/index.js +6 -2
- package/lib/index.js.map +1 -1
- package/lib/jsonschema/JsonSchema.js +6 -6
- package/lib/jsonschema/JsonSchema.js.map +1 -1
- package/lib/jsonschema/types.d.js.map +1 -1
- package/lib/meta/defineFileType.js.map +1 -1
- package/lib/meta/defineInit.js.map +1 -1
- package/lib/meta/defineMetadata.js.map +1 -1
- package/lib/password/PHC.js +8 -8
- package/lib/password/PHC.js.map +1 -1
- package/lib/password/Password.js.map +1 -1
- package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBase64PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -1
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createScryptPasswordAlgorithm.js.map +1 -1
- package/lib/search/AdvanceSearch.js.map +1 -1
- package/lib/search/formatAdvanceSearch.js.map +1 -1
- package/lib/search/optimizeAdvanceSearch.js.map +1 -1
- package/lib/search/parseAdvanceSearch.js.map +1 -1
- package/lib/search/parser.d.js.map +1 -1
- package/lib/search/types.d.js.map +1 -1
- package/lib/tools/generateSchema.js +43 -0
- package/lib/tools/generateSchema.js.map +1 -0
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +1 -1
- package/package.json +19 -5
- package/src/cn/DivisionCode.test.ts +38 -45
- package/src/cn/DivisionCode.ts +140 -184
- package/src/cn/Mod11Checksum.ts +17 -17
- package/src/cn/Mod31Checksum.ts +25 -25
- package/src/cn/ResidentIdentityCardNumber.test.ts +12 -16
- package/src/cn/ResidentIdentityCardNumber.ts +82 -82
- package/src/cn/UnifiedSocialCreditCode.test.ts +11 -11
- package/src/cn/UnifiedSocialCreditCode.ts +115 -120
- package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +1 -1
- package/src/cn/formatDate.ts +10 -10
- package/src/cn/parseSex.ts +11 -25
- package/src/cn/types.d.ts +43 -43
- package/src/consola/createStandardConsolaReporter.ts +31 -0
- package/src/consola/formatLogObject.ts +171 -0
- package/src/consola/index.ts +2 -0
- package/src/data/formatSort.test.ts +13 -0
- package/src/data/formatSort.ts +18 -0
- package/src/data/index.ts +5 -0
- package/src/data/maybeNumber.ts +23 -0
- package/src/data/parseSort.test.ts +67 -0
- package/src/data/parseSort.ts +108 -0
- package/src/data/resolvePagination.test.ts +58 -0
- package/src/data/resolvePagination.ts +60 -0
- package/src/data/types.d.ts +33 -0
- package/src/index.ts +8 -2
- package/src/jsonschema/JsonSchema.test.ts +13 -22
- package/src/jsonschema/JsonSchema.ts +146 -178
- package/src/jsonschema/types.d.ts +151 -161
- package/src/meta/defineFileType.tsx +54 -54
- package/src/meta/defineInit.ts +32 -53
- package/src/meta/defineMetadata.test.ts +5 -7
- package/src/meta/defineMetadata.ts +28 -46
- package/src/password/PHC.test.ts +186 -277
- package/src/password/PHC.ts +243 -243
- package/src/password/Password.test.ts +38 -50
- package/src/password/Password.ts +73 -95
- package/src/password/createArgon2PasswordAlgorithm.ts +65 -69
- package/src/password/createBase64PasswordAlgorithm.ts +9 -9
- package/src/password/createBcryptPasswordAlgorithm.ts +20 -22
- package/src/password/createPBKDF2PasswordAlgorithm.ts +49 -61
- package/src/password/createScryptPasswordAlgorithm.ts +48 -59
- package/src/search/AdvanceSearch.test.ts +136 -143
- package/src/search/AdvanceSearch.ts +6 -6
- package/src/search/formatAdvanceSearch.ts +44 -53
- package/src/search/optimizeAdvanceSearch.ts +70 -83
- package/src/search/parseAdvanceSearch.ts +16 -19
- package/src/search/parser.d.ts +3 -3
- package/src/search/types.d.ts +28 -54
- package/src/tools/generateSchema.ts +39 -0
- package/src/tools/renderJsonSchemaToMarkdownDoc.ts +69 -69
- package/lib/normalizePagination.js +0 -14
- package/lib/normalizePagination.js.map +0 -1
- package/lib/parseSort.js +0 -106
- package/lib/parseSort.js.map +0 -1
- package/src/normalizePagination.ts +0 -25
- package/src/parseSort.test.ts +0 -42
- package/src/parseSort.ts +0 -133
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { exec } from "node:child_process";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import * as Codegen from "@sinclair/typebox-codegen";
|
|
5
|
+
export async function generateSchema({ file, dir = path.dirname(file) }) {
|
|
6
|
+
const fn = path.basename(file).replace(/\.d\.ts$/, '.ts');
|
|
7
|
+
const types = await fs.readFile(file, 'utf-8');
|
|
8
|
+
const typeboxDir = path.join(dir, 'typebox');
|
|
9
|
+
const zodDir = path.join(dir, 'zod');
|
|
10
|
+
await fs.mkdir(typeboxDir, {
|
|
11
|
+
recursive: true
|
|
12
|
+
});
|
|
13
|
+
await fs.mkdir(zodDir, {
|
|
14
|
+
recursive: true
|
|
15
|
+
});
|
|
16
|
+
const typeBoxFile = path.join(typeboxDir, fn);
|
|
17
|
+
const zodFile = path.join(zodDir, fn);
|
|
18
|
+
{
|
|
19
|
+
// avoid import type error
|
|
20
|
+
let out = Codegen.TypeScriptToTypeBox.Generate(types);
|
|
21
|
+
out = out.replace(/^import \{ Type, Static\b/, `import { Type, type Static`);
|
|
22
|
+
await fs.writeFile(typeBoxFile, out);
|
|
23
|
+
}
|
|
24
|
+
const model = Codegen.TypeScriptToModel.Generate(types);
|
|
25
|
+
await fs.writeFile(zodFile, Codegen.ModelToZod.Generate(model));
|
|
26
|
+
await new Promise((resolve, reject)=>{
|
|
27
|
+
exec(`pnpm prettier --write "${dir}/{typebox,zod}/*.ts"`, (error, stdout, stderr)=>{
|
|
28
|
+
if (error) {
|
|
29
|
+
console.error(`exec error: ${error}`);
|
|
30
|
+
reject(error);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
resolve({
|
|
34
|
+
stderr,
|
|
35
|
+
stdout
|
|
36
|
+
});
|
|
37
|
+
stdout && console.log(`prettier:stdout: ${stdout}`);
|
|
38
|
+
stderr && console.error(`prettier:stderr: ${stderr}`);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=generateSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/tools/generateSchema.ts"],"sourcesContent":["import { exec } from 'node:child_process';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\nimport * as Codegen from '@sinclair/typebox-codegen';\n\nexport async function generateSchema({ file, dir = path.dirname(file) }: { file: string; dir?: string }) {\n const fn = path.basename(file).replace(/\\.d\\.ts$/, '.ts');\n const types = await fs.readFile(file, 'utf-8');\n\n const typeboxDir = path.join(dir, 'typebox');\n const zodDir = path.join(dir, 'zod');\n\n await fs.mkdir(typeboxDir, { recursive: true });\n await fs.mkdir(zodDir, { recursive: true });\n\n const typeBoxFile = path.join(typeboxDir, fn);\n const zodFile = path.join(zodDir, fn);\n {\n // avoid import type error\n let out = Codegen.TypeScriptToTypeBox.Generate(types);\n out = out.replace(/^import \\{ Type, Static\\b/, `import { Type, type Static`);\n await fs.writeFile(typeBoxFile, out);\n }\n const model = Codegen.TypeScriptToModel.Generate(types);\n await fs.writeFile(zodFile, Codegen.ModelToZod.Generate(model));\n\n await new Promise((resolve, reject) => {\n exec(`pnpm prettier --write \"${dir}/{typebox,zod}/*.ts\"`, (error, stdout, stderr) => {\n if (error) {\n console.error(`exec error: ${error}`);\n reject(error);\n return;\n }\n resolve({ stderr, stdout });\n stdout && console.log(`prettier:stdout: ${stdout}`);\n stderr && console.error(`prettier:stderr: ${stderr}`);\n });\n });\n}\n"],"names":["exec","fs","path","Codegen","generateSchema","file","dir","dirname","fn","basename","replace","types","readFile","typeboxDir","join","zodDir","mkdir","recursive","typeBoxFile","zodFile","out","TypeScriptToTypeBox","Generate","writeFile","model","TypeScriptToModel","ModelToZod","Promise","resolve","reject","error","stdout","stderr","console","log"],"mappings":"AAAA,SAASA,IAAI,QAAQ,qBAAqB;AAC1C,OAAOC,QAAQ,mBAAmB;AAClC,OAAOC,UAAU,YAAY;AAC7B,YAAYC,aAAa,4BAA4B;AAErD,OAAO,eAAeC,eAAe,EAAEC,IAAI,EAAEC,MAAMJ,KAAKK,OAAO,CAACF,KAAK,EAAkC;IACrG,MAAMG,KAAKN,KAAKO,QAAQ,CAACJ,MAAMK,OAAO,CAAC,YAAY;IACnD,MAAMC,QAAQ,MAAMV,GAAGW,QAAQ,CAACP,MAAM;IAEtC,MAAMQ,aAAaX,KAAKY,IAAI,CAACR,KAAK;IAClC,MAAMS,SAASb,KAAKY,IAAI,CAACR,KAAK;IAE9B,MAAML,GAAGe,KAAK,CAACH,YAAY;QAAEI,WAAW;IAAK;IAC7C,MAAMhB,GAAGe,KAAK,CAACD,QAAQ;QAAEE,WAAW;IAAK;IAEzC,MAAMC,cAAchB,KAAKY,IAAI,CAACD,YAAYL;IAC1C,MAAMW,UAAUjB,KAAKY,IAAI,CAACC,QAAQP;IAClC;QACE,0BAA0B;QAC1B,IAAIY,MAAMjB,QAAQkB,mBAAmB,CAACC,QAAQ,CAACX;QAC/CS,MAAMA,IAAIV,OAAO,CAAC,6BAA6B,CAAC,0BAA0B,CAAC;QAC3E,MAAMT,GAAGsB,SAAS,CAACL,aAAaE;IAClC;IACA,MAAMI,QAAQrB,QAAQsB,iBAAiB,CAACH,QAAQ,CAACX;IACjD,MAAMV,GAAGsB,SAAS,CAACJ,SAAShB,QAAQuB,UAAU,CAACJ,QAAQ,CAACE;IAExD,MAAM,IAAIG,QAAQ,CAACC,SAASC;QAC1B7B,KAAK,CAAC,uBAAuB,EAAEM,IAAI,oBAAoB,CAAC,EAAE,CAACwB,OAAOC,QAAQC;YACxE,IAAIF,OAAO;gBACTG,QAAQH,KAAK,CAAC,CAAC,YAAY,EAAEA,OAAO;gBACpCD,OAAOC;gBACP;YACF;YACAF,QAAQ;gBAAEI;gBAAQD;YAAO;YACzBA,UAAUE,QAAQC,GAAG,CAAC,CAAC,iBAAiB,EAAEH,QAAQ;YAClDC,UAAUC,QAAQH,KAAK,CAAC,CAAC,iBAAiB,EAAEE,QAAQ;QACtD;IACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/tools/renderJsonSchemaToMarkdownDoc.ts"],"sourcesContent":["import type { TObject, TSchema } from '@sinclair/typebox';\n\nexport function renderJsonSchemaToMarkdownDoc(rootSchema: any) {\n
|
|
1
|
+
{"version":3,"sources":["../../src/tools/renderJsonSchemaToMarkdownDoc.ts"],"sourcesContent":["import type { TObject, TSchema } from '@sinclair/typebox';\n\nexport function renderJsonSchemaToMarkdownDoc(rootSchema: any) {\n\t// markdown\n\tconst out: string[] = [];\n\tconst all = new Set();\n\tconst pending: TSchema[] = [];\n\n\tconst addObject = (o: TSchema) => {\n\t\tif (all.has(o.$id)) {\n\t\t\treturn;\n\t\t}\n\t\tall.add(o.$id);\n\t\tpending.push(o);\n\t};\n\n\tconst writeObjectProps = (T: TObject, { prefix = '' }: { prefix?: string } = {}) => {\n\t\tif (!T?.properties) {\n\t\t\treturn;\n\t\t}\n\t\tfor (const [name, schema] of Object.entries(T.properties)) {\n\t\t\tlet typ = schema.$id || schema.type;\n\n\t\t\tif (typ === 'array') {\n\t\t\t\ttyp = `${schema.items.$id || schema.items.type}[]`;\n\t\t\t\tif (schema.items.$id) {\n\t\t\t\t\taddObject(schema.items);\n\t\t\t\t}\n\t\t\t} else if (schema.$id) {\n\t\t\t\taddObject(schema);\n\t\t\t}\n\t\t\tif (!typ) {\n\t\t\t\tif ('anyOf' in schema) {\n\t\t\t\t\ttyp = 'enum';\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tout.push(`| ${prefix}${name} | ${typ} | ${schema.title || schema.description || ''} |`);\n\n\t\t\tif (typ === 'object') {\n\t\t\t\twriteObjectProps(schema as TObject, { prefix: `${prefix}${name}.` });\n\t\t\t} else if (schema.type === 'array') {\n\t\t\t\tif (schema.items.type === 'object' && !schema.items.$id) {\n\t\t\t\t\twriteObjectProps(schema.items as TObject, { prefix: `${prefix}${name}[].` });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\tconst writeObject = (T: TObject) => {\n\t\tout.push(`### ${T.title || T.$id}`);\n\t\tout.push(`\n| $id | 名字 |\n| --- | --- |\n| ${T.$id || ''} | ${T.title || ''} | \n `);\n\t\tif (T.description) {\n\t\t\tout.push('');\n\t\t\tout.push(`> ${T.description}`);\n\t\t\tout.push('');\n\t\t}\n\n\t\tout.push(`| 名字 | 类型 | 说明 |`);\n\t\tout.push(`| --- | --- | --- |`);\n\n\t\twriteObjectProps(T);\n\n\t\tout.push('');\n\t};\n\n\twriteObject(rootSchema);\n\n\tfor (const schema of pending) {\n\t\tif (schema.type === 'object') {\n\t\t\twriteObject(schema as TObject);\n\t\t} else if ('anyOf' in schema) {\n\t\t\tout.push(`### ${schema.$id || schema.title}`);\n\t\t\tout.push(`\n| $id | 名字 |\n| --- | --- |\n| ${schema.$id || ''} | ${schema.title || ''} | \n `);\n\n\t\t\tout.push(`| 值 | 说明 |`);\n\t\t\tout.push(`| --- | --- |`);\n\t\t\tfor (const item of schema.anyOf) {\n\t\t\t\tout.push(`| ${item.const} | ${item.title || item.description || ''} |`);\n\t\t\t}\n\t\t\tout.push('');\n\t\t}\n\t}\n\n\treturn out.join('\\n');\n}\n"],"names":["renderJsonSchemaToMarkdownDoc","rootSchema","out","all","Set","pending","addObject","o","has","$id","add","push","writeObjectProps","T","prefix","properties","name","schema","Object","entries","typ","type","items","title","description","writeObject","item","anyOf","const","join"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wener/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "",
|
|
6
6
|
"author": "",
|
|
@@ -14,6 +14,14 @@
|
|
|
14
14
|
"types": "./src/cn/index.ts",
|
|
15
15
|
"default": "./lib/cn/index.js"
|
|
16
16
|
},
|
|
17
|
+
"./consola": {
|
|
18
|
+
"types": "./src/consola/index.ts",
|
|
19
|
+
"default": "./lib/consola/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./data": {
|
|
22
|
+
"types": "./src/data/index.ts",
|
|
23
|
+
"default": "./lib/data/index.js"
|
|
24
|
+
},
|
|
17
25
|
"./jsonschema": {
|
|
18
26
|
"types": "./src/jsonschema/index.ts",
|
|
19
27
|
"default": "./lib/jsonschema/index.js"
|
|
@@ -40,12 +48,15 @@
|
|
|
40
48
|
],
|
|
41
49
|
"keywords": [],
|
|
42
50
|
"dependencies": {
|
|
43
|
-
"es-toolkit": "^1.
|
|
44
|
-
"
|
|
51
|
+
"es-toolkit": "^1.32.0",
|
|
52
|
+
"std-env": "^3.8.1",
|
|
53
|
+
"ts-pattern": "^5.6.2",
|
|
54
|
+
"zod": "^3.24.2",
|
|
45
55
|
"@wener/utils": "1.1.51"
|
|
46
56
|
},
|
|
47
57
|
"devDependencies": {
|
|
48
|
-
"@sinclair/typebox": "^0.34.
|
|
58
|
+
"@sinclair/typebox": "^0.34.28",
|
|
59
|
+
"@sinclair/typebox-codegen": "^0.10.5",
|
|
49
60
|
"@types/argon2-browser": "^1.18.4",
|
|
50
61
|
"@types/bcrypt": "^5.0.2",
|
|
51
62
|
"@types/bcryptjs": "^2.4.6",
|
|
@@ -56,7 +67,10 @@
|
|
|
56
67
|
"argon2": "^0.41.1",
|
|
57
68
|
"argon2-browser": "^1.18.0",
|
|
58
69
|
"bcrypt": "^5.1.1",
|
|
59
|
-
"bcryptjs": "^
|
|
70
|
+
"bcryptjs": "^3.0.2",
|
|
71
|
+
"chalk": "^5.4.1",
|
|
72
|
+
"consola": "^3.4.2",
|
|
73
|
+
"dayjs": "^1.11.13"
|
|
60
74
|
},
|
|
61
75
|
"scripts": {
|
|
62
76
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -2,49 +2,42 @@ import { describe, expect, it } from 'vitest';
|
|
|
2
2
|
import { DivisionCode } from './DivisionCode';
|
|
3
3
|
|
|
4
4
|
describe('DivisionCode', () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let out = DivisionCode.parse(a);
|
|
44
|
-
expect(out).toMatchObject(b);
|
|
45
|
-
if (out) {
|
|
46
|
-
expect(DivisionCode.format(out)).toBe(String(a));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
});
|
|
5
|
+
it('should parse', () => {
|
|
6
|
+
for (const [a, b] of [
|
|
7
|
+
[
|
|
8
|
+
'441900003001',
|
|
9
|
+
{
|
|
10
|
+
province: '44',
|
|
11
|
+
city: '19',
|
|
12
|
+
county: '00',
|
|
13
|
+
town: '003',
|
|
14
|
+
village: '001',
|
|
15
|
+
codes: ['44', '19', '00', '003', '001'],
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
[
|
|
19
|
+
441900003001,
|
|
20
|
+
{
|
|
21
|
+
province: '44',
|
|
22
|
+
city: '19',
|
|
23
|
+
county: '00',
|
|
24
|
+
town: '003',
|
|
25
|
+
village: '001',
|
|
26
|
+
codes: ['44', '19', '00', '003', '001'],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
[441900, { province: '44', city: '19', county: '00' }],
|
|
30
|
+
['31', { province: '31' }],
|
|
31
|
+
['4', undefined],
|
|
32
|
+
['', undefined],
|
|
33
|
+
[null, undefined],
|
|
34
|
+
[undefined, undefined],
|
|
35
|
+
] as Array<[string, any]>) {
|
|
36
|
+
let out = DivisionCode.parse(a);
|
|
37
|
+
expect(out).toMatchObject(b);
|
|
38
|
+
if (out) {
|
|
39
|
+
expect(DivisionCode.format(out)).toBe(String(a));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
50
43
|
});
|
package/src/cn/DivisionCode.ts
CHANGED
|
@@ -1,45 +1,9 @@
|
|
|
1
|
-
const DivisionCodeLevels: Array<{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}> = [
|
|
8
|
-
{
|
|
9
|
-
level: 1,
|
|
10
|
-
code: 'Province',
|
|
11
|
-
length: 2,
|
|
12
|
-
size: 2,
|
|
13
|
-
label: '省',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
level: 2,
|
|
17
|
-
code: 'City',
|
|
18
|
-
length: 4,
|
|
19
|
-
size: 2,
|
|
20
|
-
label: '市',
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
level: 3,
|
|
24
|
-
code: 'County',
|
|
25
|
-
length: 6,
|
|
26
|
-
size: 2,
|
|
27
|
-
label: '区县',
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
level: 4,
|
|
31
|
-
code: 'Town',
|
|
32
|
-
length: 9,
|
|
33
|
-
size: 3,
|
|
34
|
-
label: '乡镇',
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
level: 5,
|
|
38
|
-
code: 'Village',
|
|
39
|
-
length: 12,
|
|
40
|
-
size: 3,
|
|
41
|
-
label: '村',
|
|
42
|
-
},
|
|
1
|
+
const DivisionCodeLevels: Array<{ level: number; code: string; length: number; size: number; label: string }> = [
|
|
2
|
+
{ level: 1, code: 'Province', length: 2, size: 2, label: '省' },
|
|
3
|
+
{ level: 2, code: 'City', length: 4, size: 2, label: '市' },
|
|
4
|
+
{ level: 3, code: 'County', length: 6, size: 2, label: '区县' },
|
|
5
|
+
{ level: 4, code: 'Town', length: 9, size: 3, label: '乡镇' },
|
|
6
|
+
{ level: 5, code: 'Village', length: 12, size: 3, label: '村' },
|
|
43
7
|
] as const;
|
|
44
8
|
|
|
45
9
|
// String(Number.MAX_SAFE_INTEGER).length=16
|
|
@@ -51,128 +15,120 @@ const DivisionCodeLevels: Array<{
|
|
|
51
15
|
* @see https://zh.wikipedia.org/wiki/GB/T_2260 中华人民共和国行政区划代码
|
|
52
16
|
*/
|
|
53
17
|
export namespace DivisionCode {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// export function random(level: DivisionCodeLevel = 'County'): string {
|
|
161
|
-
// const l = DivisionCodeLevels.find((v) => v.code === level) || DivisionCodeLevels[2];
|
|
162
|
-
// const l1 = randomPick(provinces);
|
|
163
|
-
// if (l.level === 1) {
|
|
164
|
-
// return String(l1[0]);
|
|
165
|
-
// }
|
|
166
|
-
// return l1 + String(Math.floor(Math.random() * parseFloat(`1e${l.length - 2}`) - 1));
|
|
167
|
-
// }
|
|
18
|
+
enum DivisionCodeLevel {
|
|
19
|
+
Province = 1,
|
|
20
|
+
City = 2,
|
|
21
|
+
County = 3,
|
|
22
|
+
Town = 4,
|
|
23
|
+
Village = 5,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const levels = DivisionCodeLevels;
|
|
27
|
+
|
|
28
|
+
export const regex = /^(?<province>\d{2})(?<city>\d{2})?(?<county>\d{2})?(?<town>\d{3})?(?<village>\d{3})?$/;
|
|
29
|
+
|
|
30
|
+
const root: CodeValue[] = [
|
|
31
|
+
{ value: '11', label: '北京市' },
|
|
32
|
+
{ value: '12', label: '天津市' },
|
|
33
|
+
{ value: '13', label: '河北省' },
|
|
34
|
+
{ value: '14', label: '山西省' },
|
|
35
|
+
{ value: '15', label: '内蒙古自治区' },
|
|
36
|
+
{ value: '21', label: '辽宁省' },
|
|
37
|
+
{ value: '22', label: '吉林省' },
|
|
38
|
+
{ value: '23', label: '黑龙江省' },
|
|
39
|
+
{ value: '31', label: '上海市' },
|
|
40
|
+
{ value: '32', label: '江苏省' },
|
|
41
|
+
{ value: '33', label: '浙江省' },
|
|
42
|
+
{ value: '34', label: '安徽省' },
|
|
43
|
+
{ value: '35', label: '福建省' },
|
|
44
|
+
{ value: '36', label: '江西省' },
|
|
45
|
+
{ value: '37', label: '山东省' },
|
|
46
|
+
{ value: '41', label: '河南省' },
|
|
47
|
+
{ value: '42', label: '湖北省' },
|
|
48
|
+
{ value: '43', label: '湖南省' },
|
|
49
|
+
{ value: '44', label: '广东省' },
|
|
50
|
+
{ value: '45', label: '广西壮族自治区' },
|
|
51
|
+
{ value: '46', label: '海南省' },
|
|
52
|
+
{ value: '50', label: '重庆市' },
|
|
53
|
+
{ value: '51', label: '四川省' },
|
|
54
|
+
{ value: '52', label: '贵州省' },
|
|
55
|
+
{ value: '53', label: '云南省' },
|
|
56
|
+
{ value: '54', label: '西藏自治区' },
|
|
57
|
+
{ value: '61', label: '陕西省' },
|
|
58
|
+
{ value: '62', label: '甘肃省' },
|
|
59
|
+
{ value: '63', label: '青海省' },
|
|
60
|
+
{ value: '64', label: '宁夏回族自治区' },
|
|
61
|
+
{ value: '65', label: '新疆维吾尔自治区' },
|
|
62
|
+
{ value: '71', label: '台湾省' },
|
|
63
|
+
{ value: '81', label: '香港特别行政区' },
|
|
64
|
+
{ value: '82', label: '澳门特别行政区' },
|
|
65
|
+
// 9 国外
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
export type ParsedCode = {
|
|
69
|
+
province: string;
|
|
70
|
+
city?: string;
|
|
71
|
+
county?: string;
|
|
72
|
+
town?: string;
|
|
73
|
+
village?: string;
|
|
74
|
+
codes: string[];
|
|
75
|
+
level: DivisionCodeLevel;
|
|
76
|
+
labels: string[];
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export function parse(code: string | undefined | null | number): ParsedCode | undefined {
|
|
80
|
+
if (!code) return;
|
|
81
|
+
code = String(code);
|
|
82
|
+
const match = regex.exec(code);
|
|
83
|
+
if (!match) return;
|
|
84
|
+
const { province, city, county, town, village } = match.groups ?? {};
|
|
85
|
+
if (!province) return;
|
|
86
|
+
|
|
87
|
+
let codes = [province, city, county, town, village].filter(Boolean);
|
|
88
|
+
return { province, city, county, town, village, codes: codes, level: codes.length as DivisionCodeLevel };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function format({
|
|
92
|
+
province,
|
|
93
|
+
city,
|
|
94
|
+
county,
|
|
95
|
+
town,
|
|
96
|
+
village,
|
|
97
|
+
}: {
|
|
98
|
+
province: string | number;
|
|
99
|
+
city?: string | number;
|
|
100
|
+
county?: string | number;
|
|
101
|
+
town?: string | number;
|
|
102
|
+
village?: string | number;
|
|
103
|
+
}): string {
|
|
104
|
+
const codes: string[] = [];
|
|
105
|
+
for (let i = 0; i < [province, city, county, town, village].length; i++) {
|
|
106
|
+
let x = [province, city, county, town, village][i];
|
|
107
|
+
if (x === undefined || x === null || x === '') {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
let len = levels[i].size;
|
|
111
|
+
codes.push(String(x).padStart(len, '0').slice(0, len));
|
|
112
|
+
}
|
|
113
|
+
return codes.join('');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// export function random(level: DivisionCodeLevel = 'County'): string {
|
|
117
|
+
// const l = DivisionCodeLevels.find((v) => v.code === level) || DivisionCodeLevels[2];
|
|
118
|
+
// const l1 = randomPick(provinces);
|
|
119
|
+
// if (l.level === 1) {
|
|
120
|
+
// return String(l1[0]);
|
|
121
|
+
// }
|
|
122
|
+
// return l1 + String(Math.floor(Math.random() * parseFloat(`1e${l.length - 2}`) - 1));
|
|
123
|
+
// }
|
|
168
124
|
}
|
|
169
125
|
|
|
170
126
|
interface DivisionTreeNode {
|
|
171
|
-
|
|
172
|
-
|
|
127
|
+
sub: string; // sub code
|
|
128
|
+
children?: Record<string, DivisionTreeNode>;
|
|
173
129
|
|
|
174
|
-
|
|
175
|
-
|
|
130
|
+
code: string; // full code
|
|
131
|
+
name?: string; // name of division
|
|
176
132
|
}
|
|
177
133
|
|
|
178
134
|
// export type DivisionCodeLevel = 'Village' | 'Town' | 'County' | 'City' | 'Province';
|
|
@@ -220,34 +176,34 @@ interface DivisionTreeNode {
|
|
|
220
176
|
// }
|
|
221
177
|
|
|
222
178
|
export function randomPick<T>(s: T[]) {
|
|
223
|
-
|
|
179
|
+
return s[Math.floor(Math.random() * s.length)];
|
|
224
180
|
}
|
|
225
181
|
|
|
226
182
|
function lookup(opts: { values: string[]; root: CodeValue[] }): { found: CodeValue[] } {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
183
|
+
const { values, root } = opts;
|
|
184
|
+
const found: CodeValue[] = [];
|
|
185
|
+
let currentLevel = root;
|
|
186
|
+
|
|
187
|
+
for (const v of values) {
|
|
188
|
+
const node = currentLevel.find((n) => n.value === v);
|
|
189
|
+
if (!node) {
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
found.push(node);
|
|
194
|
+
|
|
195
|
+
if (node.children) {
|
|
196
|
+
currentLevel = node.children;
|
|
197
|
+
} else {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return { found };
|
|
247
203
|
}
|
|
248
204
|
|
|
249
205
|
interface CodeValue {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
206
|
+
value: string;
|
|
207
|
+
label: string;
|
|
208
|
+
children?: Array<CodeValue>;
|
|
253
209
|
}
|
package/src/cn/Mod11Checksum.ts
CHANGED
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
* ISO 7064:1983, MOD 11-2.
|
|
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
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
validate(s: string) {
|
|
8
|
+
return s.at(-1) === this.compute(s.slice(0, s.length - 1));
|
|
9
|
+
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
11
|
+
compute(s: string) {
|
|
12
|
+
const { weights } = this;
|
|
13
|
+
let sum = 0;
|
|
14
|
+
for (let i = 0; i < s.length; i++) {
|
|
15
|
+
sum += parseInt(s[i]) * weights[i];
|
|
16
|
+
}
|
|
17
|
+
const num = (12 - (sum % 11)) % 11;
|
|
18
|
+
if (num < 10) {
|
|
19
|
+
return num.toString();
|
|
20
|
+
} else {
|
|
21
|
+
return 'X';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
24
|
}
|