typia 4.0.0 → 4.0.1-dev.20230604

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 (87) hide show
  1. package/lib/programmers/ApplicationProgrammer.js +1 -1
  2. package/lib/programmers/internal/application_array.js +19 -41
  3. package/lib/programmers/internal/application_array.js.map +1 -1
  4. package/lib/programmers/internal/application_definition.d.ts +1 -1
  5. package/lib/programmers/internal/application_definition.js +8 -8
  6. package/lib/programmers/internal/application_definition.js.map +1 -1
  7. package/lib/programmers/internal/application_native.js +3 -3
  8. package/lib/programmers/internal/application_object.js +4 -4
  9. package/lib/programmers/internal/application_schema.js +3 -3
  10. package/lib/programmers/internal/application_schema.js.map +1 -1
  11. package/lib/programmers/internal/application_tuple.js +8 -30
  12. package/lib/programmers/internal/application_tuple.js.map +1 -1
  13. package/lib/schemas/IJsonComponents.d.ts +1 -12
  14. package/lib/schemas/IJsonSchema.d.ts +1 -1
  15. package/package.json +1 -1
  16. package/src/IRandomGenerator.ts +34 -34
  17. package/src/factories/IdentifierFactory.ts +65 -65
  18. package/src/factories/MetadataCollection.ts +254 -254
  19. package/src/factories/MetadataFactory.ts +40 -40
  20. package/src/factories/MetadataTagFactory.ts +300 -300
  21. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  22. package/src/factories/internal/metadata/emplace_metadata_definition.ts +35 -35
  23. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -142
  24. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  25. package/src/factories/internal/metadata/explore_metadata.ts +66 -66
  26. package/src/factories/internal/metadata/iterate_metadata.ts +77 -77
  27. package/src/factories/internal/metadata/iterate_metadata_array.ts +25 -25
  28. package/src/factories/internal/metadata/iterate_metadata_collection.ts +130 -130
  29. package/src/factories/internal/metadata/iterate_metadata_definition.ts +30 -30
  30. package/src/factories/internal/metadata/iterate_metadata_object.ts +49 -49
  31. package/src/factories/internal/metadata/iterate_metadata_sort.ts +68 -68
  32. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  33. package/src/factories/internal/metadata/iterate_metadata_union.ts +68 -68
  34. package/src/metadata/IMetadata.ts +27 -27
  35. package/src/metadata/IMetadataAlias.ts +12 -12
  36. package/src/metadata/IMetadataApplication.ts +7 -7
  37. package/src/metadata/IMetadataArray.ts +10 -10
  38. package/src/metadata/IMetadataCollection.ts +11 -11
  39. package/src/metadata/IMetadataDictionary.ts +14 -14
  40. package/src/metadata/IMetadataObject.ts +18 -18
  41. package/src/metadata/IMetadataProperty.ts +11 -11
  42. package/src/metadata/IMetadataTuple.ts +10 -10
  43. package/src/metadata/Metadata.ts +585 -585
  44. package/src/metadata/MetadataAlias.ts +61 -61
  45. package/src/metadata/MetadataArray.ts +52 -52
  46. package/src/metadata/MetadataObject.ts +114 -114
  47. package/src/metadata/MetadataProperty.ts +64 -64
  48. package/src/metadata/MetadataTuple.ts +53 -53
  49. package/src/programmers/ApplicationProgrammer.ts +55 -55
  50. package/src/programmers/AssertProgrammer.ts +291 -291
  51. package/src/programmers/CheckerProgrammer.ts +1182 -1182
  52. package/src/programmers/CloneProgrammer.ts +595 -595
  53. package/src/programmers/FeatureProgrammer.ts +495 -495
  54. package/src/programmers/IsProgrammer.ts +250 -250
  55. package/src/programmers/LiteralsProgrammer.ts +66 -66
  56. package/src/programmers/PruneProgrammer.ts +550 -550
  57. package/src/programmers/RandomProgrammer.ts +589 -589
  58. package/src/programmers/StringifyProgrammer.ts +990 -990
  59. package/src/programmers/ValidateProgrammer.ts +313 -313
  60. package/src/programmers/helpers/FunctionImporeter.ts +78 -78
  61. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  62. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  63. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  64. package/src/programmers/helpers/disable_function_importer_declare.ts +26 -26
  65. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  66. package/src/programmers/internal/application_array.ts +30 -64
  67. package/src/programmers/internal/application_boolean.ts +15 -15
  68. package/src/programmers/internal/application_constant.ts +26 -26
  69. package/src/programmers/internal/application_default.ts +17 -17
  70. package/src/programmers/internal/application_definition.ts +45 -45
  71. package/src/programmers/internal/application_native.ts +39 -39
  72. package/src/programmers/internal/application_number.ts +74 -74
  73. package/src/programmers/internal/application_object.ts +167 -167
  74. package/src/programmers/internal/application_schema.ts +156 -164
  75. package/src/programmers/internal/application_tuple.ts +47 -77
  76. package/src/programmers/internal/check_union_array_like.ts +329 -329
  77. package/src/programmers/internal/feature_object_entries.ts +63 -63
  78. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  79. package/src/schemas/IJsonApplication.ts +8 -8
  80. package/src/schemas/IJsonComponents.ts +34 -45
  81. package/src/schemas/IJsonSchema.ts +134 -134
  82. package/src/transformers/CallExpressionTransformer.ts +179 -179
  83. package/src/transformers/FileTransformer.ts +47 -47
  84. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +104 -104
  85. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +53 -53
  86. package/src/utils/ArrayUtil.ts +45 -45
  87. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,104 +1,104 @@
1
- import ts from "typescript";
2
-
3
- import { LiteralFactory } from "../../../factories/LiteralFactory";
4
- import { MetadataCollection } from "../../../factories/MetadataCollection";
5
- import { MetadataFactory } from "../../../factories/MetadataFactory";
6
-
7
- import { Metadata } from "../../../metadata/Metadata";
8
- import { IJsonApplication } from "../../../schemas/IJsonApplication";
9
-
10
- import { ApplicationProgrammer } from "../../../programmers/ApplicationProgrammer";
11
-
12
- import { IProject } from "../../IProject";
13
-
14
- export namespace ApplicationTransformer {
15
- export const transform =
16
- ({ checker }: IProject) =>
17
- (expression: ts.CallExpression): ts.Expression => {
18
- if (!expression.typeArguments?.length)
19
- throw new Error(NO_GENERIC_ARGUMENT);
20
-
21
- //----
22
- // GET ARGUMENTS
23
- //----
24
- // VALIDATE TUPLE ARGUMENTS
25
- const top: ts.Node = expression.typeArguments[0]!;
26
- if (!ts.isTupleTypeNode(top)) return expression;
27
- else if (top.elements.some((child) => !ts.isTypeNode(child)))
28
- return expression;
29
-
30
- // GET TYPES
31
- const types: ts.Type[] = top.elements.map((child) =>
32
- checker.getTypeFromTypeNode(child as ts.TypeNode),
33
- );
34
- if (types.some((t) => t.isTypeParameter()))
35
- throw new Error(GENERIC_ARGUMENT);
36
-
37
- // ADDITIONAL PARAMETERS
38
- const purpose: "swagger" | "ajv" = get_parameter(
39
- checker,
40
- "Purpose",
41
- expression.typeArguments[1],
42
- (str) => str === "swagger" || str === "ajv",
43
- () => "swagger",
44
- );
45
-
46
- //----
47
- // GENERATORS
48
- //----
49
- // METADATA
50
- const collection: MetadataCollection = new MetadataCollection({
51
- replace: MetadataCollection.replace,
52
- });
53
- const metadatas: Array<Metadata> = types.map((type) =>
54
- MetadataFactory.analyze(checker)({
55
- resolve: true,
56
- constant: true,
57
- absorb: false,
58
- validate: (meta) => {
59
- if (meta.atomics.find((str) => str === "bigint"))
60
- throw new Error(NO_BIGIT);
61
- },
62
- })(collection)(type),
63
- );
64
-
65
- // APPLICATION
66
- const app: IJsonApplication = ApplicationProgrammer.write({
67
- purpose,
68
- })(metadatas);
69
-
70
- // RETURNS WITH LITERAL EXPRESSION
71
- return LiteralFactory.generate(app);
72
- };
73
-
74
- const get_parameter = <T extends string>(
75
- checker: ts.TypeChecker,
76
- name: string,
77
- node: ts.TypeNode | undefined,
78
- predicator: (value: string) => boolean,
79
- defaulter: () => T,
80
- ): T => {
81
- if (!node) return defaulter();
82
-
83
- // CHECK LITERAL TYPE
84
- const type: ts.Type = checker.getTypeFromTypeNode(node);
85
- if (!type.isLiteral())
86
- throw new Error(
87
- `Error on typia.application(): generic argument "${name}" must be constant.`,
88
- );
89
-
90
- // GET VALUE AND VALIDATE IT
91
- const value = type.value;
92
- if (typeof value !== "string" || predicator(value) === false)
93
- throw new Error(
94
- `Error on typia.application(): invalid value on generic argument "${name}".`,
95
- );
96
- return value as T;
97
- };
98
- }
99
-
100
- const NO_GENERIC_ARGUMENT =
101
- "Error on typia.application(): no generic argument.";
102
- const GENERIC_ARGUMENT =
103
- "Error on typia.application(): non-specified generic argument(s).";
104
- const NO_BIGIT = "Error on typia.application(): does not allow bigint type.";
1
+ import ts from "typescript";
2
+
3
+ import { LiteralFactory } from "../../../factories/LiteralFactory";
4
+ import { MetadataCollection } from "../../../factories/MetadataCollection";
5
+ import { MetadataFactory } from "../../../factories/MetadataFactory";
6
+
7
+ import { Metadata } from "../../../metadata/Metadata";
8
+ import { IJsonApplication } from "../../../schemas/IJsonApplication";
9
+
10
+ import { ApplicationProgrammer } from "../../../programmers/ApplicationProgrammer";
11
+
12
+ import { IProject } from "../../IProject";
13
+
14
+ export namespace ApplicationTransformer {
15
+ export const transform =
16
+ ({ checker }: IProject) =>
17
+ (expression: ts.CallExpression): ts.Expression => {
18
+ if (!expression.typeArguments?.length)
19
+ throw new Error(NO_GENERIC_ARGUMENT);
20
+
21
+ //----
22
+ // GET ARGUMENTS
23
+ //----
24
+ // VALIDATE TUPLE ARGUMENTS
25
+ const top: ts.Node = expression.typeArguments[0]!;
26
+ if (!ts.isTupleTypeNode(top)) return expression;
27
+ else if (top.elements.some((child) => !ts.isTypeNode(child)))
28
+ return expression;
29
+
30
+ // GET TYPES
31
+ const types: ts.Type[] = top.elements.map((child) =>
32
+ checker.getTypeFromTypeNode(child as ts.TypeNode),
33
+ );
34
+ if (types.some((t) => t.isTypeParameter()))
35
+ throw new Error(GENERIC_ARGUMENT);
36
+
37
+ // ADDITIONAL PARAMETERS
38
+ const purpose: "swagger" | "ajv" = get_parameter(
39
+ checker,
40
+ "Purpose",
41
+ expression.typeArguments[1],
42
+ (str) => str === "swagger" || str === "ajv",
43
+ () => "swagger",
44
+ );
45
+
46
+ //----
47
+ // GENERATORS
48
+ //----
49
+ // METADATA
50
+ const collection: MetadataCollection = new MetadataCollection({
51
+ replace: MetadataCollection.replace,
52
+ });
53
+ const metadatas: Array<Metadata> = types.map((type) =>
54
+ MetadataFactory.analyze(checker)({
55
+ resolve: true,
56
+ constant: true,
57
+ absorb: false,
58
+ validate: (meta) => {
59
+ if (meta.atomics.find((str) => str === "bigint"))
60
+ throw new Error(NO_BIGIT);
61
+ },
62
+ })(collection)(type),
63
+ );
64
+
65
+ // APPLICATION
66
+ const app: IJsonApplication = ApplicationProgrammer.write({
67
+ purpose,
68
+ })(metadatas);
69
+
70
+ // RETURNS WITH LITERAL EXPRESSION
71
+ return LiteralFactory.generate(app);
72
+ };
73
+
74
+ const get_parameter = <T extends string>(
75
+ checker: ts.TypeChecker,
76
+ name: string,
77
+ node: ts.TypeNode | undefined,
78
+ predicator: (value: string) => boolean,
79
+ defaulter: () => T,
80
+ ): T => {
81
+ if (!node) return defaulter();
82
+
83
+ // CHECK LITERAL TYPE
84
+ const type: ts.Type = checker.getTypeFromTypeNode(node);
85
+ if (!type.isLiteral())
86
+ throw new Error(
87
+ `Error on typia.application(): generic argument "${name}" must be constant.`,
88
+ );
89
+
90
+ // GET VALUE AND VALIDATE IT
91
+ const value = type.value;
92
+ if (typeof value !== "string" || predicator(value) === false)
93
+ throw new Error(
94
+ `Error on typia.application(): invalid value on generic argument "${name}".`,
95
+ );
96
+ return value as T;
97
+ };
98
+ }
99
+
100
+ const NO_GENERIC_ARGUMENT =
101
+ "Error on typia.application(): no generic argument.";
102
+ const GENERIC_ARGUMENT =
103
+ "Error on typia.application(): non-specified generic argument(s).";
104
+ const NO_BIGIT = "Error on typia.application(): does not allow bigint type.";
@@ -1,53 +1,53 @@
1
- import ts from "typescript";
2
-
3
- import { LiteralFactory } from "../../../factories/LiteralFactory";
4
- import { MetadataCollection } from "../../../factories/MetadataCollection";
5
- import { MetadataFactory } from "../../../factories/MetadataFactory";
6
-
7
- import { IMetadataApplication } from "../../../metadata/IMetadataApplication";
8
- import { Metadata } from "../../../metadata/Metadata";
9
-
10
- import { IProject } from "../../IProject";
11
-
12
- export namespace MetadataTransformer {
13
- export const transform =
14
- ({ checker }: IProject) =>
15
- (expression: ts.CallExpression): ts.Expression => {
16
- if (!expression.typeArguments?.length)
17
- throw new Error(NO_GENERIC_ARGUMENT);
18
-
19
- // VALIDATE TUPLE ARGUMENTS
20
- const top: ts.Node = expression.typeArguments[0]!;
21
- if (!ts.isTupleTypeNode(top)) return expression;
22
- else if (top.elements.some((child) => !ts.isTypeNode(child)))
23
- return expression;
24
-
25
- // GET TYPES
26
- const types: ts.Type[] = top.elements.map((child) =>
27
- checker.getTypeFromTypeNode(child as ts.TypeNode),
28
- );
29
- if (types.some((t) => t.isTypeParameter()))
30
- throw new Error(GENERIC_ARGUMENT);
31
-
32
- // METADATA
33
- const collection: MetadataCollection = new MetadataCollection();
34
- const metadatas: Array<Metadata> = types.map((type) =>
35
- MetadataFactory.analyze(checker)({
36
- resolve: false,
37
- constant: true,
38
- absorb: true,
39
- })(collection)(type),
40
- );
41
-
42
- // CONVERT TO PRIMITIVE TYPE
43
- const app: IMetadataApplication = {
44
- metadatas: metadatas.map((metadata) => metadata.toJSON()),
45
- collection: collection.toJSON(),
46
- };
47
- return LiteralFactory.generate(app);
48
- };
49
- }
50
-
51
- const NO_GENERIC_ARGUMENT = "Error on typia.metadata(): no generic argument.";
52
- const GENERIC_ARGUMENT =
53
- "Error on typia.metadata(): non-specified generic argument(s).";
1
+ import ts from "typescript";
2
+
3
+ import { LiteralFactory } from "../../../factories/LiteralFactory";
4
+ import { MetadataCollection } from "../../../factories/MetadataCollection";
5
+ import { MetadataFactory } from "../../../factories/MetadataFactory";
6
+
7
+ import { IMetadataApplication } from "../../../metadata/IMetadataApplication";
8
+ import { Metadata } from "../../../metadata/Metadata";
9
+
10
+ import { IProject } from "../../IProject";
11
+
12
+ export namespace MetadataTransformer {
13
+ export const transform =
14
+ ({ checker }: IProject) =>
15
+ (expression: ts.CallExpression): ts.Expression => {
16
+ if (!expression.typeArguments?.length)
17
+ throw new Error(NO_GENERIC_ARGUMENT);
18
+
19
+ // VALIDATE TUPLE ARGUMENTS
20
+ const top: ts.Node = expression.typeArguments[0]!;
21
+ if (!ts.isTupleTypeNode(top)) return expression;
22
+ else if (top.elements.some((child) => !ts.isTypeNode(child)))
23
+ return expression;
24
+
25
+ // GET TYPES
26
+ const types: ts.Type[] = top.elements.map((child) =>
27
+ checker.getTypeFromTypeNode(child as ts.TypeNode),
28
+ );
29
+ if (types.some((t) => t.isTypeParameter()))
30
+ throw new Error(GENERIC_ARGUMENT);
31
+
32
+ // METADATA
33
+ const collection: MetadataCollection = new MetadataCollection();
34
+ const metadatas: Array<Metadata> = types.map((type) =>
35
+ MetadataFactory.analyze(checker)({
36
+ resolve: false,
37
+ constant: true,
38
+ absorb: true,
39
+ })(collection)(type),
40
+ );
41
+
42
+ // CONVERT TO PRIMITIVE TYPE
43
+ const app: IMetadataApplication = {
44
+ metadatas: metadatas.map((metadata) => metadata.toJSON()),
45
+ collection: collection.toJSON(),
46
+ };
47
+ return LiteralFactory.generate(app);
48
+ };
49
+ }
50
+
51
+ const NO_GENERIC_ARGUMENT = "Error on typia.metadata(): no generic argument.";
52
+ const GENERIC_ARGUMENT =
53
+ "Error on typia.metadata(): non-specified generic argument(s).";
@@ -1,45 +1,45 @@
1
- export namespace ArrayUtil {
2
- export const has = <T>(array: T[], pred: (elem: T) => boolean): boolean =>
3
- array.some(pred);
4
-
5
- export const add = <T>(
6
- array: T[],
7
- value: T,
8
- pred: (x: T, y: T) => boolean = (x, y) => x === y,
9
- ): boolean => {
10
- if (array.some((elem) => pred(elem, value))) return false;
11
- array.push(value);
12
- return true;
13
- };
14
-
15
- export const set = <Key, T>(
16
- array: T[],
17
- value: T,
18
- key: (elem: T) => Key,
19
- ): void => {
20
- if (array.some((elem) => key(elem) === key(value))) return;
21
- array.push(value);
22
- };
23
-
24
- export const take = <T>(
25
- array: T[],
26
- pred: (elem: T) => boolean,
27
- init: () => T,
28
- ): T => {
29
- const index: number = array.findIndex(pred);
30
- if (index !== -1) return array[index]!;
31
-
32
- const elem: T = init();
33
- array.push(elem);
34
- return elem;
35
- };
36
-
37
- export const repeat = <T>(
38
- count: number,
39
- closure: (index: number, count: number) => T,
40
- ): T[] =>
41
- new Array(count).fill("").map((_, index) => closure(index, count));
42
-
43
- export const flat = <T>(matrix: T[][]): T[] =>
44
- ([] as T[]).concat(...matrix);
45
- }
1
+ export namespace ArrayUtil {
2
+ export const has = <T>(array: T[], pred: (elem: T) => boolean): boolean =>
3
+ array.some(pred);
4
+
5
+ export const add = <T>(
6
+ array: T[],
7
+ value: T,
8
+ pred: (x: T, y: T) => boolean = (x, y) => x === y,
9
+ ): boolean => {
10
+ if (array.some((elem) => pred(elem, value))) return false;
11
+ array.push(value);
12
+ return true;
13
+ };
14
+
15
+ export const set = <Key, T>(
16
+ array: T[],
17
+ value: T,
18
+ key: (elem: T) => Key,
19
+ ): void => {
20
+ if (array.some((elem) => key(elem) === key(value))) return;
21
+ array.push(value);
22
+ };
23
+
24
+ export const take = <T>(
25
+ array: T[],
26
+ pred: (elem: T) => boolean,
27
+ init: () => T,
28
+ ): T => {
29
+ const index: number = array.findIndex(pred);
30
+ if (index !== -1) return array[index]!;
31
+
32
+ const elem: T = init();
33
+ array.push(elem);
34
+ return elem;
35
+ };
36
+
37
+ export const repeat = <T>(
38
+ count: number,
39
+ closure: (index: number, count: number) => T,
40
+ ): T[] =>
41
+ new Array(count).fill("").map((_, index) => closure(index, count));
42
+
43
+ export const flat = <T>(matrix: T[][]): T[] =>
44
+ ([] as T[]).concat(...matrix);
45
+ }
@@ -1,81 +1,81 @@
1
- import RandExp from "randexp";
2
-
3
- /**
4
- * @internal
5
- */
6
- export namespace RandomGenerator {
7
- const ALPHABETS = "abcdefghijklmnopqrstuvwxyz";
8
-
9
- /* -----------------------------------------------------------
10
- REGULAR
11
- ----------------------------------------------------------- */
12
- export const boolean = () => Math.random() < 0.5;
13
-
14
- export const integer = (min?: number, max?: number) => {
15
- min ??= 0;
16
- max ??= 100;
17
- return Math.floor(Math.random() * (max - min + 1)) + min;
18
- };
19
-
20
- export const bigint = (min?: bigint, max?: bigint) => {
21
- min ??= BigInt(0);
22
- max ??= BigInt(100);
23
- return BigInt(integer(Number(min), Number(max)));
24
- };
25
-
26
- export const number = (min?: number, max?: number) => {
27
- min ??= 0;
28
- max ??= 100;
29
- return Math.random() * (max - min) + min;
30
- };
31
-
32
- export const string = (length?: number): string =>
33
- new Array(length ?? integer(5, 10))
34
- .fill(0)
35
- .map(() => ALPHABETS[integer(0, ALPHABETS.length - 1)])
36
- .join("");
37
-
38
- export const array = <T>(
39
- closure: (index: number) => T,
40
- count?: number,
41
- ): T[] =>
42
- new Array(count ?? length()).fill(0).map((_e, index) => closure(index));
43
-
44
- export const pick = <T>(array: T[]): T =>
45
- array[integer(0, array.length - 1)]!;
46
-
47
- export const length = () => integer(0, 3);
48
-
49
- /* -----------------------------------------------------------
50
- SECIAL FORMATS
51
- ----------------------------------------------------------- */
52
- export const uuid = () =>
53
- "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
54
- const r = (Math.random() * 16) | 0;
55
- const v = c === "x" ? r : (r & 0x3) | 0x8;
56
- return v.toString(16);
57
- });
58
-
59
- export const email = () => `${string(10)}@${string(10)}.${string(3)}`;
60
-
61
- export const url = () => `https://${string(10)}.${string(3)}`;
62
-
63
- export const ipv4 = () => array(() => integer(0, 255), 4).join(".");
64
-
65
- export const ipv6 = (): string =>
66
- array(() => integer(0, 65535).toString(16), 8).join(":");
67
-
68
- export const pattern = (regex: RegExp): string => new RandExp(regex).gen();
69
-
70
- export const date = (min?: number, max?: number) => {
71
- min ??= 0;
72
- max ??= Date.now() * 2;
73
- return new Date(number(min, max)).toISOString().substring(0, 10);
74
- };
75
-
76
- export const datetime = (min?: number, max?: number) => {
77
- min ??= 0;
78
- max ??= Date.now() * 2;
79
- return new Date(number(min, max)).toISOString();
80
- };
81
- }
1
+ import RandExp from "randexp";
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ export namespace RandomGenerator {
7
+ const ALPHABETS = "abcdefghijklmnopqrstuvwxyz";
8
+
9
+ /* -----------------------------------------------------------
10
+ REGULAR
11
+ ----------------------------------------------------------- */
12
+ export const boolean = () => Math.random() < 0.5;
13
+
14
+ export const integer = (min?: number, max?: number) => {
15
+ min ??= 0;
16
+ max ??= 100;
17
+ return Math.floor(Math.random() * (max - min + 1)) + min;
18
+ };
19
+
20
+ export const bigint = (min?: bigint, max?: bigint) => {
21
+ min ??= BigInt(0);
22
+ max ??= BigInt(100);
23
+ return BigInt(integer(Number(min), Number(max)));
24
+ };
25
+
26
+ export const number = (min?: number, max?: number) => {
27
+ min ??= 0;
28
+ max ??= 100;
29
+ return Math.random() * (max - min) + min;
30
+ };
31
+
32
+ export const string = (length?: number): string =>
33
+ new Array(length ?? integer(5, 10))
34
+ .fill(0)
35
+ .map(() => ALPHABETS[integer(0, ALPHABETS.length - 1)])
36
+ .join("");
37
+
38
+ export const array = <T>(
39
+ closure: (index: number) => T,
40
+ count?: number,
41
+ ): T[] =>
42
+ new Array(count ?? length()).fill(0).map((_e, index) => closure(index));
43
+
44
+ export const pick = <T>(array: T[]): T =>
45
+ array[integer(0, array.length - 1)]!;
46
+
47
+ export const length = () => integer(0, 3);
48
+
49
+ /* -----------------------------------------------------------
50
+ SECIAL FORMATS
51
+ ----------------------------------------------------------- */
52
+ export const uuid = () =>
53
+ "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
54
+ const r = (Math.random() * 16) | 0;
55
+ const v = c === "x" ? r : (r & 0x3) | 0x8;
56
+ return v.toString(16);
57
+ });
58
+
59
+ export const email = () => `${string(10)}@${string(10)}.${string(3)}`;
60
+
61
+ export const url = () => `https://${string(10)}.${string(3)}`;
62
+
63
+ export const ipv4 = () => array(() => integer(0, 255), 4).join(".");
64
+
65
+ export const ipv6 = (): string =>
66
+ array(() => integer(0, 65535).toString(16), 8).join(":");
67
+
68
+ export const pattern = (regex: RegExp): string => new RandExp(regex).gen();
69
+
70
+ export const date = (min?: number, max?: number) => {
71
+ min ??= 0;
72
+ max ??= Date.now() * 2;
73
+ return new Date(number(min, max)).toISOString().substring(0, 10);
74
+ };
75
+
76
+ export const datetime = (min?: number, max?: number) => {
77
+ min ??= 0;
78
+ max ??= Date.now() * 2;
79
+ return new Date(number(min, max)).toISOString();
80
+ };
81
+ }