@wener/utils 1.1.40 → 1.1.42
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 +24 -0
- package/lib/cn/index.js +1 -0
- package/lib/cn/index.js.map +1 -1
- package/lib/cn/pinyin/loader.js +15 -0
- package/lib/cn/pinyin/loader.js.map +1 -0
- package/lib/cn/pinyin/preload.js +4 -0
- package/lib/cn/pinyin/preload.js.map +1 -0
- package/lib/cn/pinyin/toPinyinPure.js +4 -11
- package/lib/cn/pinyin/toPinyinPure.js.map +1 -1
- package/lib/cn/pinyin/transform.js +14 -0
- package/lib/cn/pinyin/transform.js.map +1 -0
- package/package.json +6 -1
- package/src/cn/README.md +3 -0
- package/src/cn/index.ts +1 -0
- package/src/cn/pinyin/loader.ts +12 -0
- package/src/cn/pinyin/preload.ts +3 -0
- package/src/cn/pinyin/toPinyin.test.ts +3 -1
- package/src/cn/pinyin/toPinyinPure.ts +7 -16
- package/src/cn/pinyin/transform.ts +12 -0
package/README.md
CHANGED
|
@@ -17,6 +17,30 @@
|
|
|
17
17
|
- see also
|
|
18
18
|
- https://github.com/Daninet/hash-wasm
|
|
19
19
|
|
|
20
|
+
# 日用工具集
|
|
21
|
+
|
|
22
|
+
- 默认 0 依赖
|
|
23
|
+
- 默认 ESM
|
|
24
|
+
- 面向 bundler
|
|
25
|
+
- 又 bundler 来做 tree shaking
|
|
26
|
+
- 目前不提供 bundle 的 dist js
|
|
27
|
+
- 提供 中国 相关的工具
|
|
28
|
+
- 身份证解析 解析/校验/生成
|
|
29
|
+
- 行政区划代码 解析/查询
|
|
30
|
+
- 提供三级 json data - 约 100kb 的 JSON
|
|
31
|
+
- 统一社会信用代码 解析/校验/生成
|
|
32
|
+
- 拼音 查询 - 约 145kb 的 JSON
|
|
33
|
+
- 合并了一些小工具包或者替代品,进行现代化和改进
|
|
34
|
+
- randomUUID
|
|
35
|
+
- ULID
|
|
36
|
+
- ms
|
|
37
|
+
- dlv
|
|
38
|
+
- is-buffer
|
|
39
|
+
- dset
|
|
40
|
+
- templite
|
|
41
|
+
- rosetta
|
|
42
|
+
- camelcase
|
|
43
|
+
|
|
20
44
|
<!-- LINK:BEGIN -->
|
|
21
45
|
|
|
22
46
|
# Links
|
package/lib/cn/index.js
CHANGED
|
@@ -8,5 +8,6 @@ export { USCC } from './uscc/USCC.js';
|
|
|
8
8
|
export { ResidentIdNumber } from './id/ResidentIdNumber.js';
|
|
9
9
|
export { DivisionCode } from './division/DivisionCode.js';
|
|
10
10
|
export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable } from './pinyin/toPinyinPure.js';
|
|
11
|
+
export { loadCharToPinyinTable } from './pinyin/loader.js';
|
|
11
12
|
|
|
12
13
|
//# sourceMappingURL=index.js.map
|
package/lib/cn/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cn/index.ts"],"sourcesContent":["/**\n * China related utilities.\n *\n * @packageDocumentation\n */\n\nexport { parseChineseNumber } from './parseChineseNumber';\n\nexport { isUSCC } from './uscc/isUSCC';\nexport { USCC } from './uscc/USCC';\n\nexport { ResidentIdNumber } from './id/ResidentIdNumber';\n\nexport { DivisionCode } from './division/DivisionCode';\n\nexport { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable } from './pinyin/toPinyinPure';\n"],"names":["parseChineseNumber","isUSCC","USCC","ResidentIdNumber","DivisionCode","toPinyinPure","toPinyinPureFirst","getCharToPinyinTable"],"mappings":"AAAA;;;;CAIC,GAED,SAASA,kBAAkB,QAAQ,uBAAuB;AAE1D,SAASC,MAAM,QAAQ,gBAAgB;AACvC,SAASC,IAAI,QAAQ,cAAc;AAEnC,SAASC,gBAAgB,QAAQ,wBAAwB;AAEzD,SAASC,YAAY,QAAQ,0BAA0B;AAEvD,SAASC,YAAY,EAAEC,iBAAiB,EAAEC,oBAAoB,QAAQ,wBAAwB"}
|
|
1
|
+
{"version":3,"sources":["../../src/cn/index.ts"],"sourcesContent":["/**\n * China related utilities.\n *\n * @packageDocumentation\n */\n\nexport { parseChineseNumber } from './parseChineseNumber';\n\nexport { isUSCC } from './uscc/isUSCC';\nexport { USCC } from './uscc/USCC';\n\nexport { ResidentIdNumber } from './id/ResidentIdNumber';\n\nexport { DivisionCode } from './division/DivisionCode';\n\nexport { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable } from './pinyin/toPinyinPure';\nexport { loadCharToPinyinTable } from './pinyin/loader';\n"],"names":["parseChineseNumber","isUSCC","USCC","ResidentIdNumber","DivisionCode","toPinyinPure","toPinyinPureFirst","getCharToPinyinTable","loadCharToPinyinTable"],"mappings":"AAAA;;;;CAIC,GAED,SAASA,kBAAkB,QAAQ,uBAAuB;AAE1D,SAASC,MAAM,QAAQ,gBAAgB;AACvC,SAASC,IAAI,QAAQ,cAAc;AAEnC,SAASC,gBAAgB,QAAQ,wBAAwB;AAEzD,SAASC,YAAY,QAAQ,0BAA0B;AAEvD,SAASC,YAAY,EAAEC,iBAAiB,EAAEC,oBAAoB,QAAQ,wBAAwB;AAC9F,SAASC,qBAAqB,QAAQ,kBAAkB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { setCharToPinyinTable } from './toPinyinPure.js';
|
|
2
|
+
import { transformData } from './transform.js';
|
|
3
|
+
export function loadCharToPinyinTable() {
|
|
4
|
+
return import('./data.json', {
|
|
5
|
+
with: {
|
|
6
|
+
type: 'json'
|
|
7
|
+
}
|
|
8
|
+
}).then((v)=>v.default).then((data)=>{
|
|
9
|
+
let out = transformData(data);
|
|
10
|
+
setCharToPinyinTable(out);
|
|
11
|
+
return out;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/cn/pinyin/loader.ts"],"sourcesContent":["import { setCharToPinyinTable } from './toPinyinPure';\nimport { transformData } from './transform';\n\nexport function loadCharToPinyinTable(): Promise<Record<string, string[]>> {\n return import('./data.json' as unknown as string, { with: { type: 'json' } })\n .then((v) => v.default as Record<string, string[]>)\n .then((data) => {\n let out = transformData(data);\n setCharToPinyinTable(out);\n return out;\n });\n}\n"],"names":["setCharToPinyinTable","transformData","loadCharToPinyinTable","with","type","then","v","default","data","out"],"mappings":"AAAA,SAASA,oBAAoB,QAAQ,iBAAiB;AACtD,SAASC,aAAa,QAAQ,cAAc;AAE5C,OAAO,SAASC;IACd,OAAO,MAAM,CAAC,eAAoC;QAAEC,MAAM;YAAEC,MAAM;QAAO;IAAE,GACxEC,IAAI,CAAC,CAACC,IAAMA,EAAEC,OAAO,EACrBF,IAAI,CAAC,CAACG;QACL,IAAIC,MAAMR,cAAcO;QACxBR,qBAAqBS;QACrB,OAAOA;IACT;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/cn/pinyin/preload.ts"],"sourcesContent":["import { loadCharToPinyinTable } from './loader';\n\nawait loadCharToPinyinTable();\n"],"names":["loadCharToPinyinTable"],"mappings":"AAAA,SAASA,qBAAqB,QAAQ,WAAW;AAEjD,MAAMA"}
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import { cartesianProduct } from './cartesianProduct.js';
|
|
2
|
-
// import CharToPinyins from './pinyin.json' with { type: 'json' };
|
|
3
|
-
import PyToChar from './data.json';
|
|
4
2
|
let CharToPinyinTable;
|
|
3
|
+
export function setCharToPinyinTable(table) {
|
|
4
|
+
CharToPinyinTable = table;
|
|
5
|
+
}
|
|
5
6
|
export function getCharToPinyinTable() {
|
|
6
7
|
if (!CharToPinyinTable) {
|
|
7
|
-
|
|
8
|
-
for (let [py, chars] of Object.entries(PyToChar)){
|
|
9
|
-
for (let c of chars){
|
|
10
|
-
if (!CharToPinyinTable[c]) {
|
|
11
|
-
CharToPinyinTable[c] = [];
|
|
12
|
-
}
|
|
13
|
-
CharToPinyinTable[c].push(py);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
8
|
+
return {};
|
|
16
9
|
}
|
|
17
10
|
return CharToPinyinTable;
|
|
18
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/cn/pinyin/toPinyinPure.ts"],"sourcesContent":["import { cartesianProduct } from './cartesianProduct';\n
|
|
1
|
+
{"version":3,"sources":["../../../src/cn/pinyin/toPinyinPure.ts"],"sourcesContent":["import { cartesianProduct } from './cartesianProduct';\n\nlet CharToPinyinTable: Record<string, string[]> | undefined;\n\nexport function setCharToPinyinTable(table: Record<string, string[]>) {\n CharToPinyinTable = table;\n}\n\nexport function getCharToPinyinTable() {\n if (!CharToPinyinTable) {\n return {};\n }\n return CharToPinyinTable;\n}\n\nexport function toPinyinPureFirst(s: string, sep = ','): string {\n let tab = getCharToPinyinTable();\n return s\n .split('')\n .map((c) => tab[c]?.[0])\n .filter(Boolean)\n .join(sep);\n}\n\nexport function toPinyinPure(s: string, sep = ','): string[] {\n let tab = getCharToPinyinTable();\n // ensure order\n return cartesianProduct(s.split('').map((c) => tab[c] || ''))\n .sort((a, b) => {\n for (let i = 0; i < a.length; i++) {\n let x = a[i];\n let y = b[i];\n if (x < y) return -1;\n if (x > y) return 1;\n }\n return 0;\n })\n .map((v) => v.filter(Boolean).join(sep));\n}\n\n// export function lookupPinyinPure(s: string): Array<Array<string>> {\n// return cartesianProductOfArray(s.split('').map(c => Lookup[c]));\n// }\n"],"names":["cartesianProduct","CharToPinyinTable","setCharToPinyinTable","table","getCharToPinyinTable","toPinyinPureFirst","s","sep","tab","split","map","c","filter","Boolean","join","toPinyinPure","sort","a","b","i","length","x","y","v"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,qBAAqB;AAEtD,IAAIC;AAEJ,OAAO,SAASC,qBAAqBC,KAA+B;IAClEF,oBAAoBE;AACtB;AAEA,OAAO,SAASC;IACd,IAAI,CAACH,mBAAmB;QACtB,OAAO,CAAC;IACV;IACA,OAAOA;AACT;AAEA,OAAO,SAASI,kBAAkBC,CAAS,EAAEC,MAAM,GAAG;IACpD,IAAIC,MAAMJ;IACV,OAAOE,EACJG,KAAK,CAAC,IACNC,GAAG,CAAC,CAACC,IAAMH,GAAG,CAACG,EAAE,EAAE,CAAC,EAAE,EACtBC,MAAM,CAACC,SACPC,IAAI,CAACP;AACV;AAEA,OAAO,SAASQ,aAAaT,CAAS,EAAEC,MAAM,GAAG;IAC/C,IAAIC,MAAMJ;IACV,eAAe;IACf,OAAOJ,iBAAiBM,EAAEG,KAAK,CAAC,IAAIC,GAAG,CAAC,CAACC,IAAMH,GAAG,CAACG,EAAE,IAAI,KACtDK,IAAI,CAAC,CAACC,GAAGC;QACR,IAAK,IAAIC,IAAI,GAAGA,IAAIF,EAAEG,MAAM,EAAED,IAAK;YACjC,IAAIE,IAAIJ,CAAC,CAACE,EAAE;YACZ,IAAIG,IAAIJ,CAAC,CAACC,EAAE;YACZ,IAAIE,IAAIC,GAAG,OAAO,CAAC;YACnB,IAAID,IAAIC,GAAG,OAAO;QACpB;QACA,OAAO;IACT,GACCZ,GAAG,CAAC,CAACa,IAAMA,EAAEX,MAAM,CAACC,SAASC,IAAI,CAACP;AACvC,EAEA,sEAAsE;CACtE,qEAAqE;CACrE,IAAI"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function transformData(input) {
|
|
2
|
+
let out = {};
|
|
3
|
+
for (let [py, chars] of Object.entries(input)){
|
|
4
|
+
for (let c of chars){
|
|
5
|
+
if (!out[c]) {
|
|
6
|
+
out[c] = [];
|
|
7
|
+
}
|
|
8
|
+
out[c].push(py);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/cn/pinyin/transform.ts"],"sourcesContent":["export function transformData(input: Record<string, string[]>) {\n let out = {} as Record<string, string[]>;\n for (let [py, chars] of Object.entries(input)) {\n for (let c of chars) {\n if (!out[c]) {\n out[c] = [];\n }\n out[c].push(py);\n }\n }\n return out;\n}\n"],"names":["transformData","input","out","py","chars","Object","entries","c","push"],"mappings":"AAAA,OAAO,SAASA,cAAcC,KAA+B;IAC3D,IAAIC,MAAM,CAAC;IACX,KAAK,IAAI,CAACC,IAAIC,MAAM,IAAIC,OAAOC,OAAO,CAACL,OAAQ;QAC7C,KAAK,IAAIM,KAAKH,MAAO;YACnB,IAAI,CAACF,GAAG,CAACK,EAAE,EAAE;gBACXL,GAAG,CAACK,EAAE,GAAG,EAAE;YACb;YACAL,GAAG,CAACK,EAAE,CAACC,IAAI,CAACL;QACd;IACF;IACA,OAAOD;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wener/utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.42",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Utils for daily use",
|
|
6
6
|
"repository": {
|
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
"types": "./src/cn/index.ts",
|
|
20
20
|
"default": "./lib/cn/index.js"
|
|
21
21
|
},
|
|
22
|
+
"./cn/pinyin/data.json": "./src/cn/pinyin/data.json",
|
|
23
|
+
"./cn/pinyin/preload": {
|
|
24
|
+
"types": "./src/cn/pinyin/preload.ts",
|
|
25
|
+
"default": "./lib/cn/pinyin/preload.js"
|
|
26
|
+
},
|
|
22
27
|
"./schema/typebox": {
|
|
23
28
|
"types": "./src/schema/typebox/index.ts",
|
|
24
29
|
"default": "./lib/schema/typebox/index.js"
|
package/src/cn/README.md
ADDED
package/src/cn/index.ts
CHANGED
|
@@ -14,3 +14,4 @@ export { ResidentIdNumber } from './id/ResidentIdNumber';
|
|
|
14
14
|
export { DivisionCode } from './division/DivisionCode';
|
|
15
15
|
|
|
16
16
|
export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable } from './pinyin/toPinyinPure';
|
|
17
|
+
export { loadCharToPinyinTable } from './pinyin/loader';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { setCharToPinyinTable } from './toPinyinPure';
|
|
2
|
+
import { transformData } from './transform';
|
|
3
|
+
|
|
4
|
+
export function loadCharToPinyinTable(): Promise<Record<string, string[]>> {
|
|
5
|
+
return import('./data.json' as unknown as string, { with: { type: 'json' } })
|
|
6
|
+
.then((v) => v.default as Record<string, string[]>)
|
|
7
|
+
.then((data) => {
|
|
8
|
+
let out = transformData(data);
|
|
9
|
+
setCharToPinyinTable(out);
|
|
10
|
+
return out;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { assert, test } from 'vitest';
|
|
2
|
+
import { loadCharToPinyinTable } from './loader';
|
|
2
3
|
import { toPinyinPure } from './toPinyinPure';
|
|
3
4
|
|
|
4
|
-
test('toPinyin', () => {
|
|
5
|
+
test('toPinyin', async () => {
|
|
6
|
+
await loadCharToPinyinTable();
|
|
5
7
|
assert.deepEqual(toPinyinPure('真思'), ['zhen,sai', 'zhen,si']);
|
|
6
8
|
|
|
7
9
|
// char to py -> 350k
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { cartesianProduct } from './cartesianProduct';
|
|
2
|
-
// import CharToPinyins from './pinyin.json' with { type: 'json' };
|
|
3
|
-
import PyToChar from './data.json' with { type: 'json' };
|
|
4
2
|
|
|
5
|
-
let CharToPinyinTable: Record<string, string[]
|
|
3
|
+
let CharToPinyinTable: Record<string, string[]> | undefined;
|
|
4
|
+
|
|
5
|
+
export function setCharToPinyinTable(table: Record<string, string[]>) {
|
|
6
|
+
CharToPinyinTable = table;
|
|
7
|
+
}
|
|
6
8
|
|
|
7
9
|
export function getCharToPinyinTable() {
|
|
8
10
|
if (!CharToPinyinTable) {
|
|
9
|
-
|
|
10
|
-
for (let [py, chars] of Object.entries(PyToChar)) {
|
|
11
|
-
for (let c of chars) {
|
|
12
|
-
if (!CharToPinyinTable[c]) {
|
|
13
|
-
CharToPinyinTable[c] = [];
|
|
14
|
-
}
|
|
15
|
-
CharToPinyinTable[c].push(py);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
11
|
+
return {};
|
|
18
12
|
}
|
|
19
13
|
return CharToPinyinTable;
|
|
20
14
|
}
|
|
@@ -28,12 +22,10 @@ export function toPinyinPureFirst(s: string, sep = ','): string {
|
|
|
28
22
|
.join(sep);
|
|
29
23
|
}
|
|
30
24
|
|
|
31
|
-
|
|
32
25
|
export function toPinyinPure(s: string, sep = ','): string[] {
|
|
33
26
|
let tab = getCharToPinyinTable();
|
|
34
27
|
// ensure order
|
|
35
|
-
return cartesianProduct(s.split('')
|
|
36
|
-
.map((c) => tab[c] || ''))
|
|
28
|
+
return cartesianProduct(s.split('').map((c) => tab[c] || ''))
|
|
37
29
|
.sort((a, b) => {
|
|
38
30
|
for (let i = 0; i < a.length; i++) {
|
|
39
31
|
let x = a[i];
|
|
@@ -49,4 +41,3 @@ export function toPinyinPure(s: string, sep = ','): string[] {
|
|
|
49
41
|
// export function lookupPinyinPure(s: string): Array<Array<string>> {
|
|
50
42
|
// return cartesianProductOfArray(s.split('').map(c => Lookup[c]));
|
|
51
43
|
// }
|
|
52
|
-
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function transformData(input: Record<string, string[]>) {
|
|
2
|
+
let out = {} as Record<string, string[]>;
|
|
3
|
+
for (let [py, chars] of Object.entries(input)) {
|
|
4
|
+
for (let c of chars) {
|
|
5
|
+
if (!out[c]) {
|
|
6
|
+
out[c] = [];
|
|
7
|
+
}
|
|
8
|
+
out[c].push(py);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return out;
|
|
12
|
+
}
|