@wener/utils 1.1.41 → 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/lib/cn/index.js CHANGED
@@ -7,6 +7,7 @@ export { isUSCC } from './uscc/isUSCC.js';
7
7
  export { USCC } from './uscc/USCC.js';
8
8
  export { ResidentIdNumber } from './id/ResidentIdNumber.js';
9
9
  export { DivisionCode } from './division/DivisionCode.js';
10
- export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable, loadCharToPinyinTable } from './pinyin/toPinyinPure.js';
10
+ export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable } from './pinyin/toPinyinPure.js';
11
+ export { loadCharToPinyinTable } from './pinyin/loader.js';
11
12
 
12
13
  //# sourceMappingURL=index.js.map
@@ -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, loadCharToPinyinTable } from './pinyin/toPinyinPure';\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,EAAEC,qBAAqB,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,4 @@
1
+ import { loadCharToPinyinTable } from './loader.js';
2
+ await loadCharToPinyinTable();
3
+
4
+ //# sourceMappingURL=preload.js.map
@@ -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,31 +1,5 @@
1
1
  import { cartesianProduct } from './cartesianProduct.js';
2
2
  let CharToPinyinTable;
3
- export function loadCharToPinyinTable() {
4
- {
5
- if (CharToPinyinTable) {
6
- return Promise.resolve(CharToPinyinTable);
7
- }
8
- }
9
- return import('./data.json', {
10
- with: {
11
- type: 'json'
12
- }
13
- }).then((v)=>v.default).then((data)=>{
14
- if (CharToPinyinTable) {
15
- return CharToPinyinTable;
16
- }
17
- CharToPinyinTable = {};
18
- for (let [py, chars] of Object.entries(data)){
19
- for (let c of chars){
20
- if (!CharToPinyinTable[c]) {
21
- CharToPinyinTable[c] = [];
22
- }
23
- CharToPinyinTable[c].push(py);
24
- }
25
- }
26
- return CharToPinyinTable;
27
- });
28
- }
29
3
  export function setCharToPinyinTable(table) {
30
4
  CharToPinyinTable = table;
31
5
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/cn/pinyin/toPinyinPure.ts"],"sourcesContent":["import { cartesianProduct } from './cartesianProduct';\n\nlet CharToPinyinTable: Record<string, string[]> | undefined;\n\nexport function loadCharToPinyinTable() {\n {\n if (CharToPinyinTable) {\n return Promise.resolve(CharToPinyinTable);\n }\n }\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 if (CharToPinyinTable) {\n return CharToPinyinTable;\n }\n CharToPinyinTable = {} as Record<string, string[]>;\n for (let [py, chars] of Object.entries(data)) {\n for (let c of chars) {\n if (!CharToPinyinTable[c]) {\n CharToPinyinTable[c] = [];\n }\n CharToPinyinTable[c].push(py);\n }\n }\n return CharToPinyinTable;\n });\n}\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","loadCharToPinyinTable","Promise","resolve","with","type","then","v","default","data","py","chars","Object","entries","c","push","setCharToPinyinTable","table","getCharToPinyinTable","toPinyinPureFirst","s","sep","tab","split","map","filter","Boolean","join","toPinyinPure","sort","a","b","i","length","x","y"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,qBA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C,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,GACCX,GAAG,CAAC,CAACjB,IAAMA,EAAEkB,MAAM,CAACC,SAASC,IAAI,CAACN;AACvC,EAEA,sEAAsE;CACtE,qEAAqE;CACrE,IAAI"}
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.41",
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/index.ts CHANGED
@@ -13,4 +13,5 @@ export { ResidentIdNumber } from './id/ResidentIdNumber';
13
13
 
14
14
  export { DivisionCode } from './division/DivisionCode';
15
15
 
16
- export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable, loadCharToPinyinTable } from './pinyin/toPinyinPure';
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
+ }
@@ -0,0 +1,3 @@
1
+ import { loadCharToPinyinTable } from './loader';
2
+
3
+ await loadCharToPinyinTable();
@@ -1,5 +1,6 @@
1
1
  import { assert, test } from 'vitest';
2
- import { loadCharToPinyinTable, toPinyinPure } from './toPinyinPure';
2
+ import { loadCharToPinyinTable } from './loader';
3
+ import { toPinyinPure } from './toPinyinPure';
3
4
 
4
5
  test('toPinyin', async () => {
5
6
  await loadCharToPinyinTable();
@@ -2,31 +2,6 @@ import { cartesianProduct } from './cartesianProduct';
2
2
 
3
3
  let CharToPinyinTable: Record<string, string[]> | undefined;
4
4
 
5
- export function loadCharToPinyinTable() {
6
- {
7
- if (CharToPinyinTable) {
8
- return Promise.resolve(CharToPinyinTable);
9
- }
10
- }
11
- return import('./data.json' as unknown as string, { with: { type: 'json' } })
12
- .then((v) => v.default as Record<string, string[]>)
13
- .then((data) => {
14
- if (CharToPinyinTable) {
15
- return CharToPinyinTable;
16
- }
17
- CharToPinyinTable = {} as Record<string, string[]>;
18
- for (let [py, chars] of Object.entries(data)) {
19
- for (let c of chars) {
20
- if (!CharToPinyinTable[c]) {
21
- CharToPinyinTable[c] = [];
22
- }
23
- CharToPinyinTable[c].push(py);
24
- }
25
- }
26
- return CharToPinyinTable;
27
- });
28
- }
29
-
30
5
  export function setCharToPinyinTable(table: Record<string, string[]>) {
31
6
  CharToPinyinTable = table;
32
7
  }
@@ -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
+ }