@wener/common 1.0.2 → 1.0.4

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.
Files changed (120) hide show
  1. package/lib/cn/DivisionCode.js +311 -0
  2. package/lib/cn/DivisionCode.js.map +1 -0
  3. package/lib/cn/Mod11Checksum.js +42 -0
  4. package/lib/cn/Mod11Checksum.js.map +1 -0
  5. package/lib/cn/Mod31Checksum.js +48 -0
  6. package/lib/cn/Mod31Checksum.js.map +1 -0
  7. package/lib/cn/ResidentIdentityCardNumber.js +50 -0
  8. package/lib/cn/ResidentIdentityCardNumber.js.map +1 -0
  9. package/lib/cn/UnifiedSocialCreditCode.js +118 -0
  10. package/lib/cn/UnifiedSocialCreditCode.js.map +1 -0
  11. package/lib/cn/formatDate.js +15 -0
  12. package/lib/cn/formatDate.js.map +1 -0
  13. package/lib/cn/index.js +4 -0
  14. package/lib/cn/index.js.map +1 -0
  15. package/lib/cn/parseSex.js +22 -0
  16. package/lib/cn/parseSex.js.map +1 -0
  17. package/lib/cn/types.d.js +8 -0
  18. package/lib/cn/types.d.js.map +1 -0
  19. package/lib/data/formatSort.js +15 -0
  20. package/lib/data/formatSort.js.map +1 -0
  21. package/lib/data/index.js +4 -0
  22. package/lib/data/index.js.map +1 -0
  23. package/lib/data/maybeNumber.js +22 -0
  24. package/lib/data/maybeNumber.js.map +1 -0
  25. package/lib/data/parseSort.js +95 -0
  26. package/lib/data/parseSort.js.map +1 -0
  27. package/lib/data/resolvePagination.js +36 -0
  28. package/lib/data/resolvePagination.js.map +1 -0
  29. package/lib/data/types.d.js +3 -0
  30. package/lib/data/types.d.js.map +1 -0
  31. package/lib/index.js +6 -2
  32. package/lib/index.js.map +1 -1
  33. package/lib/jsonschema/JsonSchema.js +4 -4
  34. package/lib/jsonschema/JsonSchema.js.map +1 -1
  35. package/lib/jsonschema/types.d.js.map +1 -1
  36. package/lib/meta/defineFileType.js +44 -0
  37. package/lib/meta/defineFileType.js.map +1 -0
  38. package/lib/meta/defineInit.js.map +1 -1
  39. package/lib/meta/defineMetadata.js +14 -3
  40. package/lib/meta/defineMetadata.js.map +1 -1
  41. package/lib/meta/index.js +1 -0
  42. package/lib/meta/index.js.map +1 -1
  43. package/lib/password/PHC.js +8 -8
  44. package/lib/password/PHC.js.map +1 -1
  45. package/lib/password/Password.js.map +1 -1
  46. package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -1
  47. package/lib/password/createBase64PasswordAlgorithm.js.map +1 -1
  48. package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -1
  49. package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -1
  50. package/lib/password/createScryptPasswordAlgorithm.js.map +1 -1
  51. package/lib/search/AdvanceSearch.js.map +1 -1
  52. package/lib/search/formatAdvanceSearch.js +1 -1
  53. package/lib/search/formatAdvanceSearch.js.map +1 -1
  54. package/lib/search/optimizeAdvanceSearch.js.map +1 -1
  55. package/lib/search/parseAdvanceSearch.js.map +1 -1
  56. package/lib/search/parser.d.js.map +1 -1
  57. package/lib/search/parser.js +380 -76
  58. package/lib/search/parser.js.map +1 -1
  59. package/lib/search/types.d.js.map +1 -1
  60. package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +1 -1
  61. package/package.json +14 -5
  62. package/src/cn/DivisionCode.test.ts +43 -0
  63. package/src/cn/DivisionCode.ts +209 -0
  64. package/src/cn/Mod11Checksum.ts +24 -0
  65. package/src/cn/Mod31Checksum.ts +36 -0
  66. package/src/cn/ResidentIdentityCardNumber.test.ts +17 -0
  67. package/src/cn/ResidentIdentityCardNumber.ts +96 -0
  68. package/src/cn/UnifiedSocialCreditCode.test.ts +16 -0
  69. package/src/cn/UnifiedSocialCreditCode.ts +143 -0
  70. package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +15 -0
  71. package/src/cn/__snapshots__/UnifiedSocialCreditCode.test.ts.snap +41 -0
  72. package/src/cn/formatDate.ts +12 -0
  73. package/src/cn/index.ts +3 -0
  74. package/src/cn/parseSex.ts +13 -0
  75. package/src/cn/types.d.ts +51 -0
  76. package/src/data/formatSort.test.ts +13 -0
  77. package/src/data/formatSort.ts +18 -0
  78. package/src/data/index.ts +5 -0
  79. package/src/data/maybeNumber.ts +23 -0
  80. package/src/data/parseSort.test.ts +67 -0
  81. package/src/data/parseSort.ts +108 -0
  82. package/src/data/resolvePagination.test.ts +58 -0
  83. package/src/data/resolvePagination.ts +60 -0
  84. package/src/data/types.d.ts +33 -0
  85. package/src/index.ts +8 -2
  86. package/src/jsonschema/JsonSchema.test.ts +13 -22
  87. package/src/jsonschema/JsonSchema.ts +145 -177
  88. package/src/jsonschema/types.d.ts +151 -161
  89. package/src/meta/defineFileType.tsx +68 -0
  90. package/src/meta/defineInit.ts +32 -53
  91. package/src/meta/defineMetadata.test.ts +5 -7
  92. package/src/meta/defineMetadata.ts +35 -40
  93. package/src/meta/index.ts +2 -0
  94. package/src/password/PHC.test.ts +186 -277
  95. package/src/password/PHC.ts +243 -243
  96. package/src/password/Password.test.ts +38 -50
  97. package/src/password/Password.ts +73 -95
  98. package/src/password/createArgon2PasswordAlgorithm.ts +65 -69
  99. package/src/password/createBase64PasswordAlgorithm.ts +9 -9
  100. package/src/password/createBcryptPasswordAlgorithm.ts +20 -22
  101. package/src/password/createPBKDF2PasswordAlgorithm.ts +49 -61
  102. package/src/password/createScryptPasswordAlgorithm.ts +48 -59
  103. package/src/search/AdvanceSearch.test.ts +136 -143
  104. package/src/search/AdvanceSearch.ts +6 -6
  105. package/src/search/__snapshots__/AdvanceSearch.test.ts.snap +3 -3
  106. package/src/search/formatAdvanceSearch.ts +44 -53
  107. package/src/search/optimizeAdvanceSearch.ts +70 -83
  108. package/src/search/parseAdvanceSearch.ts +16 -19
  109. package/src/search/parser.d.ts +3 -3
  110. package/src/search/parser.js +325 -73
  111. package/src/search/parser.peggy +42 -9
  112. package/src/search/types.d.ts +28 -54
  113. package/src/tools/renderJsonSchemaToMarkdownDoc.ts +69 -69
  114. package/lib/normalizePagination.js +0 -14
  115. package/lib/normalizePagination.js.map +0 -1
  116. package/lib/parseSort.js +0 -91
  117. package/lib/parseSort.js.map +0 -1
  118. package/src/normalizePagination.ts +0 -25
  119. package/src/parseSort.test.ts +0 -42
  120. package/src/parseSort.ts +0 -115
@@ -0,0 +1,68 @@
1
+ type DefineFileTypeOptions = {
2
+ name: string;
3
+ title?: string;
4
+ description?: string;
5
+ type?: string;
6
+ types?: string[];
7
+ extension?: string;
8
+ extensions?: string[];
9
+ tags?: string[];
10
+ metadata?: Record<string, any>;
11
+ };
12
+ export type FileTypeDef = {
13
+ name: string;
14
+ title: string;
15
+ description?: string;
16
+ type: string; // primary type
17
+ extension: string; // primary extension
18
+ types: string[];
19
+ extensions: string[];
20
+ tags: string[];
21
+ metadata: Record<string, any>;
22
+ };
23
+
24
+ let _all: FileTypeDef[] = [];
25
+
26
+ export function defineFileType({
27
+ type = '',
28
+ types = [],
29
+ extension = '',
30
+ extensions = [],
31
+ ...opts
32
+ }: DefineFileTypeOptions): FileTypeDef {
33
+ if (!type) {
34
+ type = types[0] || '';
35
+ }
36
+ if (!extension) {
37
+ extension = extensions[0] || '';
38
+ }
39
+ if (!types.includes(type)) {
40
+ types = [type, ...types];
41
+ }
42
+ if (!extensions.includes(extension)) {
43
+ extensions = [extension, ...extensions];
44
+ }
45
+
46
+ const def: FileTypeDef = {
47
+ title: opts.title || opts.name,
48
+ type,
49
+ extension,
50
+ types,
51
+ extensions,
52
+ tags: [],
53
+ metadata: {},
54
+ ...opts,
55
+ };
56
+ let idx = _all.findIndex((v) => v.name === def.name);
57
+ if (idx >= 0) {
58
+ _all[idx] = def;
59
+ console.warn(`File type ${def.name} is redefined`);
60
+ } else {
61
+ _all.push(def);
62
+ }
63
+ return def;
64
+ }
65
+
66
+ export function getFileType(): FileTypeDef[] {
67
+ return _all;
68
+ }
@@ -1,68 +1,47 @@
1
1
  import type { MaybePromise } from '@wener/utils';
2
2
  import { startCase } from 'es-toolkit';
3
3
 
4
- type DefineInitOptions = {
5
- name: string;
6
- title?: string;
7
- onInit?: () => any;
8
- metadata?: Record<string, any>;
9
- };
10
- export type InitDef = {
11
- name: string;
12
- title: string;
13
- onInit?: () => any;
14
- metadata: Record<string, any>;
15
- };
4
+ type DefineInitOptions = { name: string; title?: string; onInit?: () => any; metadata?: Record<string, any> };
5
+ export type InitDef = { name: string; title: string; onInit?: () => any; metadata: Record<string, any> };
16
6
  let _all: InitDef[] = [];
17
7
 
18
8
  export function defineInit(o: DefineInitOptions) {
19
- const def = {
20
- title: startCase(o.name),
21
- metadata: {},
22
- ...o,
23
- };
24
- let idx = _all.findIndex((v) => v.name === def.name);
25
- if (idx >= 0) {
26
- console.warn(`skip redefined init: ${def.name}`);
27
- } else {
28
- _all.push(def);
29
- }
9
+ const def = { title: startCase(o.name), metadata: {}, ...o };
10
+ let idx = _all.findIndex((v) => v.name === def.name);
11
+ if (idx >= 0) {
12
+ console.warn(`skip redefined init: ${def.name}`);
13
+ } else {
14
+ _all.push(def);
15
+ }
30
16
 
31
- return def;
17
+ return def;
32
18
  }
33
19
 
34
20
  let result = new Map<any, MaybePromise<InitResult>>();
35
21
 
36
- type InitResult = {
37
- name: string;
38
- success: boolean;
39
- error?: any;
40
- };
22
+ type InitResult = { name: string; success: boolean; error?: any };
41
23
 
42
24
  export async function runInit(inits = _all) {
43
- for (let init of inits) {
44
- if (result.get(init)) {
45
- return;
46
- }
47
- result.set(
48
- init,
49
- Promise.resolve().then(async () => {
50
- let out: InitResult = {
51
- name: init.name,
52
- success: true,
53
- };
54
- try {
55
- await init.onInit?.();
56
- } catch (e) {
57
- console.error(`Failed to init ${init.name}`, e);
58
- out.success = false;
59
- out.error = e;
60
- }
61
- result.set(init, out);
62
- return out;
63
- }),
64
- );
65
- }
25
+ for (let init of inits) {
26
+ if (result.get(init)) {
27
+ return;
28
+ }
29
+ result.set(
30
+ init,
31
+ Promise.resolve().then(async () => {
32
+ let out: InitResult = { name: init.name, success: true };
33
+ try {
34
+ await init.onInit?.();
35
+ } catch (e) {
36
+ console.error(`Failed to init ${init.name}`, e);
37
+ out.success = false;
38
+ out.error = e;
39
+ }
40
+ result.set(init, out);
41
+ return out;
42
+ }),
43
+ );
44
+ }
66
45
 
67
- return Promise.all(result.values());
46
+ return Promise.all(result.values());
68
47
  }
@@ -2,14 +2,12 @@ import { expect, test } from 'vitest';
2
2
  import { createMetadataKey, defineMetadata, getMetadata } from './defineMetadata';
3
3
 
4
4
  test('defineMetadata', () => {
5
- const key = createMetadataKey<string>('name');
5
+ const key = createMetadataKey<string>('name');
6
6
 
7
- const user = {
8
- metadata: {},
9
- };
7
+ const user = { metadata: {} };
10
8
 
11
- defineMetadata(user, key, 'wener');
9
+ defineMetadata(user, key, 'wener');
12
10
 
13
- expect(user.metadata).toEqual({ name: 'wener' });
14
- expect(getMetadata(user, key)).toEqual('wener');
11
+ expect(user.metadata).toEqual({ name: 'wener' });
12
+ expect(getMetadata(user, key)).toEqual('wener');
15
13
  });
@@ -1,57 +1,52 @@
1
1
  import { startCase } from 'es-toolkit';
2
2
  import { get, set } from 'es-toolkit/compat';
3
3
 
4
- type HasMetadata = {
5
- metadata?: Record<string, any>;
6
- };
4
+ type HasMetadata = { metadata?: Record<string, any> };
7
5
 
8
- type MetadataKey<T> = {
9
- key: string;
10
- type: T;
11
- title: string;
12
- description?: string;
13
- };
6
+ type MetadataKey<T> = { key: string; type: T; title: string; description?: string };
14
7
 
15
8
  interface CreateMetadataKeyOptions {
16
- key: string;
17
- title?: string;
18
- description?: string;
9
+ key: string;
10
+ title?: string;
11
+ description?: string;
19
12
  }
20
13
 
21
14
  export function createMetadataKey<T = never>(key: string, opts?: Omit<CreateMetadataKeyOptions, 'key'>): MetadataKey<T>;
22
15
  export function createMetadataKey<T = never>(opts: CreateMetadataKeyOptions): MetadataKey<T>;
23
16
  export function createMetadataKey<T = never>(a: any, b?: any): MetadataKey<T> {
24
- const opts: CreateMetadataKeyOptions =
25
- typeof a === 'string'
26
- ? {
27
- key: a,
28
- ...b,
29
- }
30
- : a;
31
-
32
- const { key } = opts;
33
- opts.title ||= startCase(key);
34
-
35
- const k: MetadataKey<T> = {
36
- ...opts,
37
- type: null as any,
38
- } as MetadataKey<T>;
39
-
40
- if ('toStringTag' in Symbol && typeof Symbol.toStringTag === 'symbol') {
41
- Object.defineProperty(k, Symbol.toStringTag, {
42
- value: key,
43
- });
44
- }
45
-
46
- return k;
17
+ const opts: CreateMetadataKeyOptions = typeof a === 'string' ? { key: a, ...b } : a;
18
+
19
+ const { key } = opts;
20
+ opts.title ||= startCase(key);
21
+
22
+ const k: MetadataKey<T> = { ...opts, type: null as any } as MetadataKey<T>;
23
+
24
+ if ('toStringTag' in Symbol && typeof Symbol.toStringTag === 'symbol') {
25
+ Object.defineProperty(k, Symbol.toStringTag, { value: key });
26
+ }
27
+
28
+ return k;
47
29
  }
48
30
 
49
- export function defineMetadata<T>(res: HasMetadata, key: MetadataKey<T>, opts: T) {
50
- res.metadata = res.metadata || {};
51
- set(res.metadata, key.key, opts);
31
+ export function defineMetadata<T>(res: HasMetadata, key: MetadataKey<T>, opts: T): void;
32
+ export function defineMetadata<T>(key: MetadataKey<T>, items: Array<[HasMetadata, T]>): void;
33
+ export function defineMetadata<T>(a: any, b: any, c?: any) {
34
+ if (Array.isArray(b)) {
35
+ const key = a;
36
+ const items = b;
37
+ for (const [res, opts] of items) {
38
+ defineMetadata(res, key, opts);
39
+ }
40
+ } else {
41
+ const res = a;
42
+ const key = b;
43
+ const opts = c;
44
+ res.metadata = res.metadata || {};
45
+ set(res.metadata, key.key, opts);
46
+ }
52
47
  }
53
48
 
54
49
  export function getMetadata<T>(res: HasMetadata | undefined | null, key: MetadataKey<T>): T | undefined {
55
- if (!res?.metadata) return undefined;
56
- return get(res.metadata, key.key);
50
+ if (!res?.metadata) return undefined;
51
+ return get(res.metadata, key.key);
57
52
  }
package/src/meta/index.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export { createMetadataKey, defineMetadata, getMetadata } from './defineMetadata';
2
2
 
3
3
  export { defineInit, type InitDef, runInit } from './defineInit';
4
+
5
+ export { defineFileType, getFileType, type FileTypeDef } from './defineFileType';