@wener/utils 1.1.40 → 1.1.41

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 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
@@ -7,6 +7,6 @@ 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 } from './pinyin/toPinyinPure.js';
10
+ export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable, loadCharToPinyinTable } from './pinyin/toPinyinPure.js';
11
11
 
12
12
  //# 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 } 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, 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,11 +1,21 @@
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;
5
- export function getCharToPinyinTable() {
6
- if (!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
+ }
7
17
  CharToPinyinTable = {};
8
- for (let [py, chars] of Object.entries(PyToChar)){
18
+ for (let [py, chars] of Object.entries(data)){
9
19
  for (let c of chars){
10
20
  if (!CharToPinyinTable[c]) {
11
21
  CharToPinyinTable[c] = [];
@@ -13,6 +23,15 @@ export function getCharToPinyinTable() {
13
23
  CharToPinyinTable[c].push(py);
14
24
  }
15
25
  }
26
+ return CharToPinyinTable;
27
+ });
28
+ }
29
+ export function setCharToPinyinTable(table) {
30
+ CharToPinyinTable = table;
31
+ }
32
+ export function getCharToPinyinTable() {
33
+ if (!CharToPinyinTable) {
34
+ return {};
16
35
  }
17
36
  return CharToPinyinTable;
18
37
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/cn/pinyin/toPinyinPure.ts"],"sourcesContent":["import { cartesianProduct } from './cartesianProduct';\n// import CharToPinyins from './pinyin.json' with { type: 'json' };\nimport PyToChar from './data.json' with { type: 'json' };\n\nlet CharToPinyinTable: Record<string, string[]>;\n\nexport function getCharToPinyinTable() {\n if (!CharToPinyinTable) {\n CharToPinyinTable = {};\n for (let [py, chars] of Object.entries(PyToChar)) {\n for (let c of chars) {\n if (!CharToPinyinTable[c]) {\n CharToPinyinTable[c] = [];\n }\n CharToPinyinTable[c].push(py);\n }\n }\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\n\nexport function toPinyinPure(s: string, sep = ','): string[] {\n let tab = getCharToPinyinTable();\n // ensure order\n return cartesianProduct(s.split('')\n .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\n"],"names":["cartesianProduct","PyToChar","CharToPinyinTable","getCharToPinyinTable","py","chars","Object","entries","c","push","toPinyinPureFirst","s","sep","tab","split","map","filter","Boolean","join","toPinyinPure","sort","a","b","i","length","x","y","v"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,qBAAqB;AACtD,mEAAmE;AACnE,OAAOC,cAAc,cAAoC;AAEzD,IAAIC;AAEJ,OAAO,SAASC;IACd,IAAI,CAACD,mBAAmB;QACtBA,oBAAoB,CAAC;QACrB,KAAK,IAAI,CAACE,IAAIC,MAAM,IAAIC,OAAOC,OAAO,CAACN,UAAW;YAChD,KAAK,IAAIO,KAAKH,MAAO;gBACnB,IAAI,CAACH,iBAAiB,CAACM,EAAE,EAAE;oBACzBN,iBAAiB,CAACM,EAAE,GAAG,EAAE;gBAC3B;gBACAN,iBAAiB,CAACM,EAAE,CAACC,IAAI,CAACL;YAC5B;QACF;IACF;IACA,OAAOF;AACT;AAEA,OAAO,SAASQ,kBAAkBC,CAAS,EAAEC,MAAM,GAAG;IACpD,IAAIC,MAAMV;IACV,OAAOQ,EACJG,KAAK,CAAC,IACNC,GAAG,CAAC,CAACP,IAAMK,GAAG,CAACL,EAAE,EAAE,CAAC,EAAE,EACtBQ,MAAM,CAACC,SACPC,IAAI,CAACN;AACV;AAGA,OAAO,SAASO,aAAaR,CAAS,EAAEC,MAAM,GAAG;IAC/C,IAAIC,MAAMV;IACV,eAAe;IACf,OAAOH,iBAAiBW,EAAEG,KAAK,CAAC,IAC7BC,GAAG,CAAC,CAACP,IAAMK,GAAG,CAACL,EAAE,IAAI,KACrBY,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,GACCX,GAAG,CAAC,CAACY,IAAMA,EAAEX,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 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,qBAAqB;AAEtD,IAAIC;AAEJ,OAAO,SAASC;IACd;QACE,IAAID,mBAAmB;YACrB,OAAOE,QAAQC,OAAO,CAACH;QACzB;IACF;IACA,OAAO,MAAM,CAAC,eAAoC;QAAEI,MAAM;YAAEC,MAAM;QAAO;IAAE,GACxEC,IAAI,CAAC,CAACC,IAAMA,EAAEC,OAAO,EACrBF,IAAI,CAAC,CAACG;QACL,IAAIT,mBAAmB;YACrB,OAAOA;QACT;QACAA,oBAAoB,CAAC;QACrB,KAAK,IAAI,CAACU,IAAIC,MAAM,IAAIC,OAAOC,OAAO,CAACJ,MAAO;YAC5C,KAAK,IAAIK,KAAKH,MAAO;gBACnB,IAAI,CAACX,iBAAiB,CAACc,EAAE,EAAE;oBACzBd,iBAAiB,CAACc,EAAE,GAAG,EAAE;gBAC3B;gBACAd,iBAAiB,CAACc,EAAE,CAACC,IAAI,CAACL;YAC5B;QACF;QACA,OAAOV;IACT;AACJ;AAEA,OAAO,SAASgB,qBAAqBC,KAA+B;IAClEjB,oBAAoBiB;AACtB;AAEA,OAAO,SAASC;IACd,IAAI,CAAClB,mBAAmB;QACtB,OAAO,CAAC;IACV;IACA,OAAOA;AACT;AAEA,OAAO,SAASmB,kBAAkBC,CAAS,EAAEC,MAAM,GAAG;IACpD,IAAIC,MAAMJ;IACV,OAAOE,EACJG,KAAK,CAAC,IACNC,GAAG,CAAC,CAACV,IAAMQ,GAAG,CAACR,EAAE,EAAE,CAAC,EAAE,EACtBW,MAAM,CAACC,SACPC,IAAI,CAACN;AACV;AAEA,OAAO,SAASO,aAAaR,CAAS,EAAEC,MAAM,GAAG;IAC/C,IAAIC,MAAMJ;IACV,eAAe;IACf,OAAOnB,iBAAiBqB,EAAEG,KAAK,CAAC,IAAIC,GAAG,CAAC,CAACV,IAAMQ,GAAG,CAACR,EAAE,IAAI,KACtDe,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,GACCX,GAAG,CAAC,CAACjB,IAAMA,EAAEkB,MAAM,CAACC,SAASC,IAAI,CAACN;AACvC,EAEA,sEAAsE;CACtE,qEAAqE;CACrE,IAAI"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wener/utils",
3
- "version": "1.1.40",
3
+ "version": "1.1.41",
4
4
  "type": "module",
5
5
  "description": "Utils for daily use",
6
6
  "repository": {
@@ -0,0 +1,3 @@
1
+ - id
2
+ - https://zh.wikipedia.org/wiki/中华人民共和国外国人永久居留身份证
3
+ - https://learn.microsoft.com/en-us/purview/sit-defn-china-resident-identity-card-number
package/src/cn/index.ts CHANGED
@@ -13,4 +13,4 @@ export { ResidentIdNumber } from './id/ResidentIdNumber';
13
13
 
14
14
  export { DivisionCode } from './division/DivisionCode';
15
15
 
16
- export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable } from './pinyin/toPinyinPure';
16
+ export { toPinyinPure, toPinyinPureFirst, getCharToPinyinTable, loadCharToPinyinTable } from './pinyin/toPinyinPure';
@@ -1,7 +1,8 @@
1
1
  import { assert, test } from 'vitest';
2
- import { toPinyinPure } from './toPinyinPure';
2
+ import { loadCharToPinyinTable, toPinyinPure } from './toPinyinPure';
3
3
 
4
- test('toPinyin', () => {
4
+ test('toPinyin', async () => {
5
+ await loadCharToPinyinTable();
5
6
  assert.deepEqual(toPinyinPure('真思'), ['zhen,sai', 'zhen,si']);
6
7
 
7
8
  // char to py -> 350k
@@ -1,20 +1,39 @@
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;
6
4
 
7
- export function getCharToPinyinTable() {
8
- if (!CharToPinyinTable) {
9
- CharToPinyinTable = {};
10
- for (let [py, chars] of Object.entries(PyToChar)) {
11
- for (let c of chars) {
12
- if (!CharToPinyinTable[c]) {
13
- CharToPinyinTable[c] = [];
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);
14
24
  }
15
- CharToPinyinTable[c].push(py);
16
25
  }
17
- }
26
+ return CharToPinyinTable;
27
+ });
28
+ }
29
+
30
+ export function setCharToPinyinTable(table: Record<string, string[]>) {
31
+ CharToPinyinTable = table;
32
+ }
33
+
34
+ export function getCharToPinyinTable() {
35
+ if (!CharToPinyinTable) {
36
+ return {};
18
37
  }
19
38
  return CharToPinyinTable;
20
39
  }
@@ -28,12 +47,10 @@ export function toPinyinPureFirst(s: string, sep = ','): string {
28
47
  .join(sep);
29
48
  }
30
49
 
31
-
32
50
  export function toPinyinPure(s: string, sep = ','): string[] {
33
51
  let tab = getCharToPinyinTable();
34
52
  // ensure order
35
- return cartesianProduct(s.split('')
36
- .map((c) => tab[c] || ''))
53
+ return cartesianProduct(s.split('').map((c) => tab[c] || ''))
37
54
  .sort((a, b) => {
38
55
  for (let i = 0; i < a.length; i++) {
39
56
  let x = a[i];
@@ -49,4 +66,3 @@ export function toPinyinPure(s: string, sep = ','): string[] {
49
66
  // export function lookupPinyinPure(s: string): Array<Array<string>> {
50
67
  // return cartesianProductOfArray(s.split('').map(c => Lookup[c]));
51
68
  // }
52
-