@wener/utils 1.1.26 → 1.1.27

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 (156) hide show
  1. package/README.md +4 -0
  2. package/dist/cjs/createFetchWith-qhRObsE4.js +2 -0
  3. package/dist/cjs/createFetchWith-qhRObsE4.js.map +1 -0
  4. package/dist/cjs/{getGlobalThis-304f74e0.js → getGlobalThis-GhffAgiG.js} +1 -1
  5. package/dist/cjs/{getGlobalThis-304f74e0.js.map → getGlobalThis-GhffAgiG.js.map} +1 -1
  6. package/dist/cjs/index.cjs +11 -11
  7. package/dist/cjs/index.cjs.map +1 -1
  8. package/dist/cjs/schema/typebox/gen/index.cjs +118 -0
  9. package/dist/cjs/schema/typebox/gen/index.cjs.map +1 -0
  10. package/dist/cjs/schema/typebox/index.cjs +2 -0
  11. package/dist/cjs/schema/typebox/index.cjs.map +1 -0
  12. package/dist/cjs/server.cjs +1 -1
  13. package/dist/cjs/server.cjs.map +1 -1
  14. package/dist/cjs/servers/jsdom.cjs +1 -1
  15. package/dist/cjs/servers/jsdom.cjs.map +1 -1
  16. package/dist/cjs/servers/node-fetch.cjs +1 -1
  17. package/dist/cjs/servers/ws.cjs +1 -1
  18. package/dist/esm/createFetchWith-1q1S-Xro.js +2 -0
  19. package/dist/esm/createFetchWith-1q1S-Xro.js.map +1 -0
  20. package/dist/esm/{getGlobalThis-b7ad0cf9.js → getGlobalThis-A8F-nU7J.js} +1 -1
  21. package/dist/esm/{getGlobalThis-b7ad0cf9.js.map → getGlobalThis-A8F-nU7J.js.map} +1 -1
  22. package/dist/esm/index.js +11 -11
  23. package/dist/esm/index.js.map +1 -1
  24. package/dist/esm/schema/typebox/gen.js +118 -0
  25. package/dist/esm/schema/typebox/gen.js.map +1 -0
  26. package/dist/esm/schema/typebox.js +2 -0
  27. package/dist/esm/schema/typebox.js.map +1 -0
  28. package/dist/esm/server.js +1 -1
  29. package/dist/esm/server.js.map +1 -1
  30. package/dist/esm/servers/jsdom.js +1 -1
  31. package/dist/esm/servers/jsdom.js.map +1 -1
  32. package/dist/esm/servers/node-fetch.js +1 -1
  33. package/dist/esm/servers/ws.js +1 -1
  34. package/dist/system/createFetchWith-TjUle-dI.js +2 -0
  35. package/dist/system/createFetchWith-TjUle-dI.js.map +1 -0
  36. package/dist/system/{getGlobalThis-8951eb0e.js → getGlobalThis-X5ifUXDI.js} +1 -1
  37. package/dist/system/{getGlobalThis-8951eb0e.js.map → getGlobalThis-X5ifUXDI.js.map} +1 -1
  38. package/dist/system/index.js +11 -11
  39. package/dist/system/index.js.map +1 -1
  40. package/dist/system/schema/typebox/gen.js +118 -0
  41. package/dist/system/schema/typebox/gen.js.map +1 -0
  42. package/dist/system/schema/typebox.js +2 -0
  43. package/dist/system/schema/typebox.js.map +1 -0
  44. package/dist/system/server.js +1 -1
  45. package/dist/system/server.js.map +1 -1
  46. package/dist/system/servers/jsdom.js +1 -1
  47. package/dist/system/servers/jsdom.js.map +1 -1
  48. package/dist/system/servers/node-fetch.js +1 -1
  49. package/dist/system/servers/ws.js +1 -1
  50. package/lib/index.js +2 -0
  51. package/lib/index.js.map +1 -1
  52. package/lib/langs/deepFreeze.js +16 -0
  53. package/lib/langs/deepFreeze.js.map +1 -0
  54. package/lib/maths/clamp.js +21 -0
  55. package/lib/maths/clamp.js.map +1 -0
  56. package/lib/objects/get.js.map +1 -1
  57. package/lib/objects/set.js.map +1 -1
  58. package/lib/schema/typebox/gen/codegen/common/encoder.js +74 -0
  59. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +1 -0
  60. package/lib/schema/typebox/gen/codegen/common/formatter.js +10 -0
  61. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +1 -0
  62. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +68 -0
  63. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +1 -0
  64. package/lib/schema/typebox/gen/codegen/expression/compiler.js +325 -0
  65. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +1 -0
  66. package/lib/schema/typebox/gen/codegen/expression/errors.js +237 -0
  67. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +1 -0
  68. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +241 -0
  69. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +1 -0
  70. package/lib/schema/typebox/gen/codegen/expression/expression.js +200 -0
  71. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +1 -0
  72. package/lib/schema/typebox/gen/codegen/index.js +19 -0
  73. package/lib/schema/typebox/gen/codegen/index.js.map +1 -0
  74. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +263 -0
  75. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +1 -0
  76. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +363 -0
  77. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +1 -0
  78. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +245 -0
  79. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +1 -0
  80. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +316 -0
  81. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +1 -0
  82. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +20 -0
  83. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +1 -0
  84. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +171 -0
  85. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +1 -0
  86. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +10 -0
  87. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +1 -0
  88. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +198 -0
  89. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +1 -0
  90. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +252 -0
  91. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +1 -0
  92. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +19 -0
  93. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +1 -0
  94. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +247 -0
  95. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +1 -0
  96. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +249 -0
  97. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +1 -0
  98. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +272 -0
  99. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +1 -0
  100. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +45 -0
  101. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +1 -0
  102. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +620 -0
  103. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +1 -0
  104. package/lib/schema/typebox/gen/index.js +3 -0
  105. package/lib/schema/typebox/gen/index.js.map +1 -0
  106. package/lib/schema/typebox/index.js +3 -0
  107. package/lib/schema/typebox/index.js.map +1 -0
  108. package/lib/schema/typebox/typebox.js +34 -0
  109. package/lib/schema/typebox/typebox.js.map +1 -0
  110. package/lib/servers/fetch/createFetchWithProxyByUndici.js +15 -0
  111. package/lib/servers/fetch/createFetchWithProxyByUndici.js.map +1 -1
  112. package/package.json +21 -6
  113. package/src/index.ts +3 -3
  114. package/src/langs/deepFreeze.ts +19 -0
  115. package/src/langs/hashCode.ignored.ts +125 -0
  116. package/src/libs/README.md +2 -0
  117. package/src/logging/slog.ts +121 -0
  118. package/src/maths/clamp.test.ts +19 -0
  119. package/src/maths/clamp.ts +23 -1
  120. package/src/objects/get.ts +14 -14
  121. package/src/objects/set.ts +5 -5
  122. package/src/schema/typebox/gen/codegen/common/encoder.ts +99 -0
  123. package/src/schema/typebox/gen/codegen/common/formatter.ts +31 -0
  124. package/src/schema/typebox/gen/codegen/common/index.ts +29 -0
  125. package/src/schema/typebox/gen/codegen/common/jsdoc.ts +93 -0
  126. package/src/schema/typebox/gen/codegen/expression/compiler.ts +377 -0
  127. package/src/schema/typebox/gen/codegen/expression/errors.ts +302 -0
  128. package/src/schema/typebox/gen/codegen/expression/evaluator.ts +268 -0
  129. package/src/schema/typebox/gen/codegen/expression/expression.ts +538 -0
  130. package/src/schema/typebox/gen/codegen/expression/index.ts +32 -0
  131. package/src/schema/typebox/gen/codegen/index.ts +29 -0
  132. package/src/schema/typebox/gen/codegen/model/index.ts +40 -0
  133. package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +266 -0
  134. package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +378 -0
  135. package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +244 -0
  136. package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +294 -0
  137. package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +42 -0
  138. package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +166 -0
  139. package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +32 -0
  140. package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +189 -0
  141. package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +236 -0
  142. package/src/schema/typebox/gen/codegen/model/model-to-value.ts +42 -0
  143. package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +232 -0
  144. package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +226 -0
  145. package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +251 -0
  146. package/src/schema/typebox/gen/codegen/model/model.ts +33 -0
  147. package/src/schema/typebox/gen/codegen/typescript/index.ts +28 -0
  148. package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +61 -0
  149. package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +647 -0
  150. package/src/schema/typebox/gen/gen.test.ts +12 -0
  151. package/src/schema/typebox/gen/index.ts +1 -0
  152. package/src/schema/typebox/index.ts +1 -0
  153. package/src/schema/typebox/typebox.ts +34 -0
  154. package/src/servers/fetch/createFetchWithProxyByUndici.ts +16 -1
  155. package/src/types/index.d.ts +1 -0
  156. package/src/langs/hashCode.ts +0 -37
@@ -0,0 +1,19 @@
1
+ import { expect, test } from 'vitest';
2
+ import { clamp } from './clamp';
3
+
4
+ test('clamp', () => {
5
+ for (const [a, b] of [
6
+ [[null, 0, 0], 0],
7
+ [[null, 1, 10], 1],
8
+ [[undefined, 1, 10], 1],
9
+ [[undefined, 1, 10, 5], 5],
10
+ [[2, 1, 10, 5], 2],
11
+ [[11, 1, 10, 5], 10],
12
+ [[11, { min: 1, max: 10 }], 10],
13
+ [[null, { min: 1, max: 10 }], 1],
14
+ [[null, { min: 1, max: 10, default: 5 }], 5],
15
+ [[2n, 1, 10, 5], 2n],
16
+ ]) {
17
+ expect(clamp.apply(null, a as any)).toBe(b);
18
+ }
19
+ });
@@ -1 +1,23 @@
1
- export function clamp() {}
1
+ // export function clamp<T>(value: T | null | undefined, o: { min: T; max: T; default?: T }): T;
2
+
3
+ export function clamp<T>(value: T | null | undefined, min: T, max: T, def?: T): T;
4
+ export function clamp<T>(value: T | null | undefined, ...o: any[]): T {
5
+ let min: T, max: T, def: T;
6
+ if (o.length === 1) {
7
+ ({ min, max, default: def = min! } = o[0]);
8
+ } else {
9
+ [min, max, def = min!] = o;
10
+ }
11
+ if (value === null || value === undefined) {
12
+ return def;
13
+ }
14
+ if (value < min) {
15
+ return min;
16
+ }
17
+ if (value > max) {
18
+ return max;
19
+ }
20
+ return value;
21
+ }
22
+
23
+ // type Comparable = number | string | Date | BigInt;
@@ -32,19 +32,19 @@ export type ResolveObjectPathType<
32
32
  > = Path extends keyof ObjectType
33
33
  ? ObjectType[Path]
34
34
  : Path extends `${infer LeftSide}.${infer RightSide}`
35
- ? LeftSide extends keyof ObjectType
36
- ? ResolveObjectPathType<ObjectType[LeftSide], RightSide, OrElse>
37
- : Path extends `${infer LeftSide}[${number}].${infer RightSide}`
38
35
  ? LeftSide extends keyof ObjectType
39
- ? ObjectType[LeftSide] extends Array<infer U>
40
- ? ResolveObjectPathType<U, RightSide, OrElse>
36
+ ? ResolveObjectPathType<ObjectType[LeftSide], RightSide, OrElse>
37
+ : Path extends `${infer LeftSide}[${number}].${infer RightSide}`
38
+ ? LeftSide extends keyof ObjectType
39
+ ? ObjectType[LeftSide] extends Array<infer U>
40
+ ? ResolveObjectPathType<U, RightSide, OrElse>
41
+ : OrElse
42
+ : OrElse
41
43
  : OrElse
42
- : OrElse
43
- : OrElse
44
- : Path extends `${infer LeftSide}[${number}]`
45
- ? LeftSide extends keyof ObjectType
46
- ? ObjectType[LeftSide] extends Array<infer U>
47
- ? U
48
- : OrElse
49
- : OrElse
50
- : OrElse;
44
+ : Path extends `${infer LeftSide}[${number}]`
45
+ ? LeftSide extends keyof ObjectType
46
+ ? ObjectType[LeftSide] extends Array<infer U>
47
+ ? U
48
+ : OrElse
49
+ : OrElse
50
+ : OrElse;
@@ -22,11 +22,11 @@ export function set<T extends object, V>(obj: T, key: ObjectKey | ObjectPath, va
22
22
  ? merge(current[k], val)
23
23
  : val
24
24
  : typeof (x = current[k]) === typeof path
25
- ? x
26
- : // @ts-expect-error hacky type check
27
- path[i] * 0 !== 0 || !!~('' + path[i]).indexOf('.') // eslint-disable-line
28
- ? {}
29
- : [];
25
+ ? x
26
+ : // @ts-expect-error hacky type check
27
+ path[i] * 0 !== 0 || !!~('' + path[i]).indexOf('.') // eslint-disable-line
28
+ ? {}
29
+ : [];
30
30
  }
31
31
  }
32
32
 
@@ -0,0 +1,99 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ // --------------------------------------------------------------------------
28
+ // Character
29
+ // --------------------------------------------------------------------------
30
+ namespace Character {
31
+ export function DollarSign(code: number) {
32
+ return code === 36;
33
+ }
34
+ export function IsUnderscore(code: number) {
35
+ return code === 95;
36
+ }
37
+ export function IsAlpha(code: number) {
38
+ return (code >= 64 && code <= 90) || (code >= 97 && code <= 122);
39
+ }
40
+ export function IsNumeric(code: number) {
41
+ return code >= 48 && code <= 57;
42
+ }
43
+ }
44
+ // --------------------------------------------------------------------------
45
+ // MemberExpressionEncoder
46
+ // --------------------------------------------------------------------------
47
+ export namespace MemberExpressionEncoder {
48
+ function IsFirstCharacterNumeric(value: string) {
49
+ if (value.length === 0) return false;
50
+ return Character.IsNumeric(value.charCodeAt(0));
51
+ }
52
+ function IsAccessor(value: string) {
53
+ if (IsFirstCharacterNumeric(value)) return false;
54
+ for (let i = 0; i < value.length; i++) {
55
+ const code = value.charCodeAt(i);
56
+ const check =
57
+ Character.IsAlpha(code) ||
58
+ Character.IsNumeric(code) ||
59
+ Character.DollarSign(code) ||
60
+ Character.IsUnderscore(code);
61
+ if (!check) return false;
62
+ }
63
+ return true;
64
+ }
65
+ function EscapeHyphen(key: string) {
66
+ return key.replace(/'/g, "\\'");
67
+ }
68
+ export function Encode(object: string, key: string) {
69
+ return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`;
70
+ }
71
+ }
72
+ // --------------------------------------------------------------------------
73
+ // PropertyEncoder
74
+ // --------------------------------------------------------------------------
75
+ export namespace PropertyEncoder {
76
+ function IsFirstCharacterNumeric(value: string) {
77
+ if (value.length === 0) return false;
78
+ return Character.IsNumeric(value.charCodeAt(0));
79
+ }
80
+ function IsSafeProperty(value: string) {
81
+ if (IsFirstCharacterNumeric(value)) return false;
82
+ for (let i = 0; i < value.length; i++) {
83
+ const code = value.charCodeAt(i);
84
+ const check =
85
+ Character.IsAlpha(code) ||
86
+ Character.IsNumeric(code) ||
87
+ Character.DollarSign(code) ||
88
+ Character.IsUnderscore(code);
89
+ if (!check) return false;
90
+ }
91
+ return true;
92
+ }
93
+ function Quoted(value: string) {
94
+ return `'${value}'`;
95
+ }
96
+ export function Encode(value: string) {
97
+ return IsSafeProperty(value) ? value : Quoted(value);
98
+ }
99
+ }
@@ -0,0 +1,31 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ export namespace Formatter {
28
+ export function Format(code: string): string {
29
+ return code;
30
+ }
31
+ }
@@ -0,0 +1,29 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ export * from './encoder';
28
+ export * from './formatter';
29
+ export * from './jsdoc';
@@ -0,0 +1,93 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ /** JSDoc Property Parser */
28
+ export namespace JsDoc {
29
+ // ----------------------------------------------------------------
30
+ // Quotes
31
+ // ----------------------------------------------------------------
32
+ function ConsumeQuote(content: string): [consumed: string, rest: string] {
33
+ if (!IsQuote(content)) return ['', content];
34
+ const quote = content[0];
35
+ for (let i = 1; i < content.length; i++) {
36
+ if (content[i] === quote) return [content.slice(1, i), content.slice(i)];
37
+ if (content[i] === '\n') return [content.slice(1, i), content.slice(i)];
38
+ }
39
+ return [content.slice(1), ''];
40
+ }
41
+ function IsQuote(content: string) {
42
+ return content[0] === '"' || content[0] === "'";
43
+ }
44
+ // ----------------------------------------------------------------
45
+ // Decode
46
+ // ----------------------------------------------------------------
47
+ function DecodeWithNonQuotedProperties(content: string): any {
48
+ const parseFunction = new Function(`return (${content});`);
49
+ return parseFunction();
50
+ }
51
+ function Decode(content: string) {
52
+ try {
53
+ return DecodeWithNonQuotedProperties(content);
54
+ } catch {
55
+ return content;
56
+ }
57
+ }
58
+ function* ParseValue(key: string, content: string): IterableIterator<[string, any]> {
59
+ if (IsQuote(content)) {
60
+ const [consumed, rest] = ConsumeQuote(content);
61
+ yield [key, Decode(consumed)];
62
+ return yield* ParseContent(rest);
63
+ }
64
+ for (let i = 0; i < content.length; i++) {
65
+ if (content[i] === '\n' || content[i] === '-') {
66
+ const value = content.slice(0, i).trim();
67
+ const rest = content.slice(i);
68
+ yield [key, Decode(value)];
69
+ return yield* ParseContent(rest);
70
+ }
71
+ }
72
+ }
73
+ function* ParseKey(content: string): IterableIterator<[string, any]> {
74
+ for (let i = 1; i < content.length; i++) {
75
+ if (content[i] === ' ') {
76
+ const key = content.slice(1, i);
77
+ const rest = content.slice(i).trimStart();
78
+ return yield* ParseValue(key, rest);
79
+ }
80
+ }
81
+ }
82
+ function* ParseContent(content: string): IterableIterator<[string, any]> {
83
+ for (let i = 0; i < content.length; i++) {
84
+ if (content[i] === '@') {
85
+ return yield* ParseKey(content.slice(i));
86
+ }
87
+ }
88
+ }
89
+ export function Parse(content: string): Record<string, any> {
90
+ const properties = [...ParseContent(content)];
91
+ return properties.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
92
+ }
93
+ }