@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.
Files changed (109) hide show
  1. package/lib/cn/DivisionCode.js.map +1 -1
  2. package/lib/cn/Mod11Checksum.js.map +1 -1
  3. package/lib/cn/Mod31Checksum.js.map +1 -1
  4. package/lib/cn/ResidentIdentityCardNumber.js.map +1 -1
  5. package/lib/cn/UnifiedSocialCreditCode.js.map +1 -1
  6. package/lib/cn/formatDate.js.map +1 -1
  7. package/lib/cn/parseSex.js.map +1 -1
  8. package/lib/cn/types.d.js.map +1 -1
  9. package/lib/consola/createStandardConsolaReporter.js +18 -0
  10. package/lib/consola/createStandardConsolaReporter.js.map +1 -0
  11. package/lib/consola/formatLogObject.js +125 -0
  12. package/lib/consola/formatLogObject.js.map +1 -0
  13. package/lib/consola/index.js +3 -0
  14. package/lib/consola/index.js.map +1 -0
  15. package/lib/data/formatSort.js +15 -0
  16. package/lib/data/formatSort.js.map +1 -0
  17. package/lib/data/index.js +4 -0
  18. package/lib/data/index.js.map +1 -0
  19. package/lib/data/maybeNumber.js +22 -0
  20. package/lib/data/maybeNumber.js.map +1 -0
  21. package/lib/data/parseSort.js +95 -0
  22. package/lib/data/parseSort.js.map +1 -0
  23. package/lib/data/resolvePagination.js +36 -0
  24. package/lib/data/resolvePagination.js.map +1 -0
  25. package/lib/data/types.d.js +3 -0
  26. package/lib/data/types.d.js.map +1 -0
  27. package/lib/index.js +6 -2
  28. package/lib/index.js.map +1 -1
  29. package/lib/jsonschema/JsonSchema.js +6 -6
  30. package/lib/jsonschema/JsonSchema.js.map +1 -1
  31. package/lib/jsonschema/types.d.js.map +1 -1
  32. package/lib/meta/defineFileType.js.map +1 -1
  33. package/lib/meta/defineInit.js.map +1 -1
  34. package/lib/meta/defineMetadata.js.map +1 -1
  35. package/lib/password/PHC.js +8 -8
  36. package/lib/password/PHC.js.map +1 -1
  37. package/lib/password/Password.js.map +1 -1
  38. package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -1
  39. package/lib/password/createBase64PasswordAlgorithm.js.map +1 -1
  40. package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -1
  41. package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -1
  42. package/lib/password/createScryptPasswordAlgorithm.js.map +1 -1
  43. package/lib/search/AdvanceSearch.js.map +1 -1
  44. package/lib/search/formatAdvanceSearch.js.map +1 -1
  45. package/lib/search/optimizeAdvanceSearch.js.map +1 -1
  46. package/lib/search/parseAdvanceSearch.js.map +1 -1
  47. package/lib/search/parser.d.js.map +1 -1
  48. package/lib/search/types.d.js.map +1 -1
  49. package/lib/tools/generateSchema.js +43 -0
  50. package/lib/tools/generateSchema.js.map +1 -0
  51. package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +1 -1
  52. package/package.json +19 -5
  53. package/src/cn/DivisionCode.test.ts +38 -45
  54. package/src/cn/DivisionCode.ts +140 -184
  55. package/src/cn/Mod11Checksum.ts +17 -17
  56. package/src/cn/Mod31Checksum.ts +25 -25
  57. package/src/cn/ResidentIdentityCardNumber.test.ts +12 -16
  58. package/src/cn/ResidentIdentityCardNumber.ts +82 -82
  59. package/src/cn/UnifiedSocialCreditCode.test.ts +11 -11
  60. package/src/cn/UnifiedSocialCreditCode.ts +115 -120
  61. package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +1 -1
  62. package/src/cn/formatDate.ts +10 -10
  63. package/src/cn/parseSex.ts +11 -25
  64. package/src/cn/types.d.ts +43 -43
  65. package/src/consola/createStandardConsolaReporter.ts +31 -0
  66. package/src/consola/formatLogObject.ts +171 -0
  67. package/src/consola/index.ts +2 -0
  68. package/src/data/formatSort.test.ts +13 -0
  69. package/src/data/formatSort.ts +18 -0
  70. package/src/data/index.ts +5 -0
  71. package/src/data/maybeNumber.ts +23 -0
  72. package/src/data/parseSort.test.ts +67 -0
  73. package/src/data/parseSort.ts +108 -0
  74. package/src/data/resolvePagination.test.ts +58 -0
  75. package/src/data/resolvePagination.ts +60 -0
  76. package/src/data/types.d.ts +33 -0
  77. package/src/index.ts +8 -2
  78. package/src/jsonschema/JsonSchema.test.ts +13 -22
  79. package/src/jsonschema/JsonSchema.ts +146 -178
  80. package/src/jsonschema/types.d.ts +151 -161
  81. package/src/meta/defineFileType.tsx +54 -54
  82. package/src/meta/defineInit.ts +32 -53
  83. package/src/meta/defineMetadata.test.ts +5 -7
  84. package/src/meta/defineMetadata.ts +28 -46
  85. package/src/password/PHC.test.ts +186 -277
  86. package/src/password/PHC.ts +243 -243
  87. package/src/password/Password.test.ts +38 -50
  88. package/src/password/Password.ts +73 -95
  89. package/src/password/createArgon2PasswordAlgorithm.ts +65 -69
  90. package/src/password/createBase64PasswordAlgorithm.ts +9 -9
  91. package/src/password/createBcryptPasswordAlgorithm.ts +20 -22
  92. package/src/password/createPBKDF2PasswordAlgorithm.ts +49 -61
  93. package/src/password/createScryptPasswordAlgorithm.ts +48 -59
  94. package/src/search/AdvanceSearch.test.ts +136 -143
  95. package/src/search/AdvanceSearch.ts +6 -6
  96. package/src/search/formatAdvanceSearch.ts +44 -53
  97. package/src/search/optimizeAdvanceSearch.ts +70 -83
  98. package/src/search/parseAdvanceSearch.ts +16 -19
  99. package/src/search/parser.d.ts +3 -3
  100. package/src/search/types.d.ts +28 -54
  101. package/src/tools/generateSchema.ts +39 -0
  102. package/src/tools/renderJsonSchemaToMarkdownDoc.ts +69 -69
  103. package/lib/normalizePagination.js +0 -14
  104. package/lib/normalizePagination.js.map +0 -1
  105. package/lib/parseSort.js +0 -106
  106. package/lib/parseSort.js.map +0 -1
  107. package/src/normalizePagination.ts +0 -25
  108. package/src/parseSort.test.ts +0 -42
  109. package/src/parseSort.ts +0 -133
@@ -1,173 +1,163 @@
1
1
  type JsonSchemaTypeName =
2
- | 'string' //
3
- | 'number'
4
- | 'integer'
5
- | 'boolean'
6
- | 'object'
7
- | 'array'
8
- | 'null';
2
+ | 'string' //
3
+ | 'number'
4
+ | 'integer'
5
+ | 'boolean'
6
+ | 'object'
7
+ | 'array'
8
+ | 'null';
9
9
 
10
10
  type JsonValue =
11
- | string //
12
- | number
13
- | boolean
14
- | {
15
- [key: string]: JsonValue;
16
- }
17
- | JsonValue[]
18
- | null;
11
+ | string //
12
+ | number
13
+ | boolean
14
+ | { [key: string]: JsonValue }
15
+ | JsonValue[]
16
+ | null;
19
17
 
20
18
  type JsonSchemaVersion =
21
- | 'http://json-schema.org/schema' // latest
22
- | 'https://json-schema.org/draft/2020-12/schema'
23
- // draft-07
24
- | 'http://json-schema.org/draft-07/schema#' //
25
- | 'http://json-schema.org/draft-07/hyper-schema#';
19
+ | 'http://json-schema.org/schema' // latest
20
+ | 'https://json-schema.org/draft/2020-12/schema'
21
+ // draft-07
22
+ | 'http://json-schema.org/draft-07/schema#' //
23
+ | 'http://json-schema.org/draft-07/hyper-schema#';
26
24
 
27
25
  type JsonSchemaFormatName =
28
- //
29
- | 'date-time'
30
- | 'date'
31
- | 'time'
32
- | 'duration'
33
- //
34
- | 'email'
35
- | 'idn-email'
36
- //
37
- | 'hostname'
38
- | 'idn-hostname'
39
- | 'ipv4'
40
- | 'ipv6'
41
- //
42
- | 'uri'
43
- | 'uri-reference'
44
- | 'iri'
45
- | 'iri-reference'
46
- | 'uuid'
47
- | 'uri-template'
48
- | 'regex'
49
- | 'json-pointer'
50
- | 'relative-json-pointer';
26
+ //
27
+ | 'date-time'
28
+ | 'date'
29
+ | 'time'
30
+ | 'duration'
31
+ //
32
+ | 'email'
33
+ | 'idn-email'
34
+ //
35
+ | 'hostname'
36
+ | 'idn-hostname'
37
+ | 'ipv4'
38
+ | 'ipv6'
39
+ //
40
+ | 'uri'
41
+ | 'uri-reference'
42
+ | 'iri'
43
+ | 'iri-reference'
44
+ | 'uuid'
45
+ | 'uri-template'
46
+ | 'regex'
47
+ | 'json-pointer'
48
+ | 'relative-json-pointer';
51
49
 
52
50
  export type JsonSchemaDef = {
53
- $id?: string;
54
- $ref?: string;
55
- /**
56
- * Meta schema
57
- *
58
- * Recommended values:
59
- * - 'http://json-schema.org/schema#'
60
- * - 'http://json-schema.org/hyper-schema#'
61
- * - 'http://json-schema.org/draft-07/schema#'
62
- * - 'http://json-schema.org/draft-07/hyper-schema#'
63
- *
64
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
65
- */
66
- $schema?: JsonSchemaVersion | string;
67
- $comment?: string;
68
-
69
- $defs?: {
70
- [key: string]: JsonSchemaDef;
71
- };
72
-
73
- type?: JsonSchemaTypeName | JsonSchemaTypeName[];
74
- enum?: JsonValue[];
75
- const?: JsonValue;
76
-
77
- //region Numeric Validation
78
-
79
- multipleOf?: number;
80
- maximum?: number;
81
- exclusiveMaximum?: number;
82
- minimum?: number;
83
- exclusiveMinimum?: number;
84
-
85
- //endregion
86
-
87
- //region String Validation
88
-
89
- maxLength?: number;
90
- minLength?: number;
91
- pattern?: string;
92
-
93
- //endregion
94
-
95
- //region Array Validation
96
-
97
- items?: JsonSchemaDef | JsonSchemaDef[];
98
- additionalItems?: JsonSchemaDef;
99
- maxItems?: number;
100
- minItems?: number;
101
- uniqueItems?: boolean;
102
- contains?: JsonSchemaDef;
103
- //endregion
104
-
105
- //region Object Validation
106
-
107
- maxProperties?: number;
108
- minProperties?: number;
109
- required?: string[];
110
- properties?: {
111
- [key: string]: JsonSchemaDef;
112
- };
113
- patternProperties?: {
114
- [key: string]: JsonSchemaDef;
115
- };
116
- additionalProperties?: JsonSchemaDef;
117
- dependencies?: {
118
- [key: string]: JsonSchemaDef | string[];
119
- };
120
- propertyNames?: JsonSchemaDef;
121
- //endregion
122
-
123
- //region Conditional
124
-
125
- if?: JsonSchemaDef;
126
- then?: JsonSchemaDef;
127
- else?: JsonSchemaDef;
128
-
129
- //endregion
130
-
131
- //region Boolean Logic
132
-
133
- allOf?: JsonSchemaDef[];
134
- anyOf?: JsonSchemaDef[];
135
- oneOf?: JsonSchemaDef[];
136
- not?: JsonSchemaDef;
137
- //endregion
138
-
139
- //region Semantic
140
-
141
- format?: JsonSchemaFormatName | string;
142
-
143
- //endregion
144
-
145
- //region String-Encoding Non-JSON Data
146
-
147
- contentMediaType?: string;
148
- contentEncoding?: string;
149
- contentSchema?: JsonSchemaDef;
150
- //endregion
151
-
152
- /**
153
- * use {@link $defs} instead
154
- */
155
- definitions?: Record<string, JsonSchemaDef>;
156
-
157
- //region Meta-Data Annotations
158
- /**
159
- * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
160
- */
161
-
162
- title?: string;
163
- description?: string;
164
- default?: JsonValue;
165
- readOnly?: boolean;
166
- writeOnly?: boolean;
167
- examples?: JsonValue;
168
- deprecated?: boolean;
51
+ $id?: string;
52
+ $ref?: string;
53
+ /**
54
+ * Meta schema
55
+ *
56
+ * Recommended values:
57
+ * - 'http://json-schema.org/schema#'
58
+ * - 'http://json-schema.org/hyper-schema#'
59
+ * - 'http://json-schema.org/draft-07/schema#'
60
+ * - 'http://json-schema.org/draft-07/hyper-schema#'
61
+ *
62
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
63
+ */
64
+ $schema?: JsonSchemaVersion | string;
65
+ $comment?: string;
66
+
67
+ $defs?: { [key: string]: JsonSchemaDef };
68
+
69
+ type?: JsonSchemaTypeName | JsonSchemaTypeName[];
70
+ enum?: JsonValue[];
71
+ const?: JsonValue;
72
+
73
+ //region Numeric Validation
74
+
75
+ multipleOf?: number;
76
+ maximum?: number;
77
+ exclusiveMaximum?: number;
78
+ minimum?: number;
79
+ exclusiveMinimum?: number;
80
+
81
+ //endregion
82
+
83
+ //region String Validation
84
+
85
+ maxLength?: number;
86
+ minLength?: number;
87
+ pattern?: string;
88
+
89
+ //endregion
90
+
91
+ //region Array Validation
92
+
93
+ items?: JsonSchemaDef | JsonSchemaDef[];
94
+ additionalItems?: JsonSchemaDef;
95
+ maxItems?: number;
96
+ minItems?: number;
97
+ uniqueItems?: boolean;
98
+ contains?: JsonSchemaDef;
99
+ //endregion
100
+
101
+ //region Object Validation
102
+
103
+ maxProperties?: number;
104
+ minProperties?: number;
105
+ required?: string[];
106
+ properties?: { [key: string]: JsonSchemaDef };
107
+ patternProperties?: { [key: string]: JsonSchemaDef };
108
+ additionalProperties?: JsonSchemaDef;
109
+ dependencies?: { [key: string]: JsonSchemaDef | string[] };
110
+ propertyNames?: JsonSchemaDef;
111
+ //endregion
112
+
113
+ //region Conditional
114
+
115
+ if?: JsonSchemaDef;
116
+ then?: JsonSchemaDef;
117
+ else?: JsonSchemaDef;
118
+
119
+ //endregion
120
+
121
+ //region Boolean Logic
122
+
123
+ allOf?: JsonSchemaDef[];
124
+ anyOf?: JsonSchemaDef[];
125
+ oneOf?: JsonSchemaDef[];
126
+ not?: JsonSchemaDef;
127
+ //endregion
128
+
129
+ //region Semantic
130
+
131
+ format?: JsonSchemaFormatName | string;
132
+
133
+ //endregion
134
+
135
+ //region String-Encoding Non-JSON Data
136
+
137
+ contentMediaType?: string;
138
+ contentEncoding?: string;
139
+ contentSchema?: JsonSchemaDef;
140
+ //endregion
141
+
142
+ /**
143
+ * use {@link $defs} instead
144
+ */
145
+ definitions?: Record<string, JsonSchemaDef>;
146
+
147
+ //region Meta-Data Annotations
148
+ /**
149
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
150
+ */
151
+
152
+ title?: string;
153
+ description?: string;
154
+ default?: JsonValue;
155
+ readOnly?: boolean;
156
+ writeOnly?: boolean;
157
+ examples?: JsonValue;
158
+ deprecated?: boolean;
169
159
 
170
- //endregion
160
+ //endregion
171
161
 
172
- nullable?: boolean;
162
+ nullable?: boolean;
173
163
  };
@@ -1,68 +1,68 @@
1
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>;
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
11
  };
12
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>;
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
22
  };
23
23
 
24
24
  let _all: FileTypeDef[] = [];
25
25
 
26
26
  export function defineFileType({
27
- type = '',
28
- types = [],
29
- extension = '',
30
- extensions = [],
31
- ...opts
27
+ type = '',
28
+ types = [],
29
+ extension = '',
30
+ extensions = [],
31
+ ...opts
32
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
- }
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
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;
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
64
  }
65
65
 
66
66
  export function getFileType(): FileTypeDef[] {
67
- return _all;
67
+ return _all;
68
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,70 +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;
17
+ const opts: CreateMetadataKeyOptions = typeof a === 'string' ? { key: a, ...b } : a;
31
18
 
32
- const { key } = opts;
33
- opts.title ||= startCase(key);
19
+ const { key } = opts;
20
+ opts.title ||= startCase(key);
34
21
 
35
- const k: MetadataKey<T> = {
36
- ...opts,
37
- type: null as any,
38
- } as MetadataKey<T>;
22
+ const k: MetadataKey<T> = { ...opts, type: null as any } as MetadataKey<T>;
39
23
 
40
- if ('toStringTag' in Symbol && typeof Symbol.toStringTag === 'symbol') {
41
- Object.defineProperty(k, Symbol.toStringTag, {
42
- value: key,
43
- });
44
- }
24
+ if ('toStringTag' in Symbol && typeof Symbol.toStringTag === 'symbol') {
25
+ Object.defineProperty(k, Symbol.toStringTag, { value: key });
26
+ }
45
27
 
46
- return k;
28
+ return k;
47
29
  }
48
30
 
49
31
  export function defineMetadata<T>(res: HasMetadata, key: MetadataKey<T>, opts: T): void;
50
32
  export function defineMetadata<T>(key: MetadataKey<T>, items: Array<[HasMetadata, T]>): void;
51
33
  export function defineMetadata<T>(a: any, b: any, c?: any) {
52
- if (Array.isArray(b)) {
53
- const key = a;
54
- const items = b;
55
- for (const [res, opts] of items) {
56
- defineMetadata(res, key, opts);
57
- }
58
- } else {
59
- const res = a;
60
- const key = b;
61
- const opts = c;
62
- res.metadata = res.metadata || {};
63
- set(res.metadata, key.key, opts);
64
- }
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
+ }
65
47
  }
66
48
 
67
49
  export function getMetadata<T>(res: HasMetadata | undefined | null, key: MetadataKey<T>): T | undefined {
68
- if (!res?.metadata) return undefined;
69
- return get(res.metadata, key.key);
50
+ if (!res?.metadata) return undefined;
51
+ return get(res.metadata, key.key);
70
52
  }