struct-fakerator 2.8.9 → 3.0.0

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 (188) hide show
  1. package/LICENSE +21 -0
  2. package/dist/config.cjs +51 -0
  3. package/dist/config.js +2 -51
  4. package/dist/config_scheme.cjs +95 -0
  5. package/dist/config_scheme.js +24 -73
  6. package/dist/create_config.cjs +110 -0
  7. package/dist/create_config.js +9 -58
  8. package/dist/{create_generator_fn.mjs → create_generator_fn.cjs} +63 -11
  9. package/dist/create_generator_fn.js +11 -63
  10. package/dist/generator_fn.cjs +54 -0
  11. package/dist/generator_fn.js +2 -54
  12. package/dist/index.cjs +107 -0
  13. package/dist/index.js +6 -107
  14. package/dist/type.cjs +18 -0
  15. package/dist/type.js +0 -18
  16. package/dist/{type.d.ts → types/type.d.ts} +10 -1
  17. package/dist/types/utils/airline.d.ts +31 -0
  18. package/dist/{utils → types/utils}/animal.d.ts +2 -0
  19. package/dist/types/utils/book.d.ts +12 -0
  20. package/dist/{utils → types/utils}/color.d.ts +9 -9
  21. package/dist/{utils → types/utils}/finance.d.ts +7 -9
  22. package/dist/{utils → types/utils}/image.d.ts +0 -11
  23. package/dist/{utils → types/utils}/index.d.ts +2 -0
  24. package/dist/{utils → types/utils}/internet.d.ts +9 -7
  25. package/dist/{utils → types/utils}/location.d.ts +29 -16
  26. package/dist/{utils → types/utils}/number.d.ts +5 -0
  27. package/dist/{utils → types/utils}/person.d.ts +1 -1
  28. package/dist/{utils → types/utils}/science.d.ts +2 -2
  29. package/dist/{utils → types/utils}/string.d.ts +4 -0
  30. package/dist/utils/airline.cjs +62 -0
  31. package/dist/utils/airline.js +10 -46
  32. package/dist/utils/animal.cjs +98 -0
  33. package/dist/utils/animal.js +19 -94
  34. package/dist/utils/book.cjs +58 -0
  35. package/dist/utils/book.js +9 -0
  36. package/dist/utils/color.cjs +78 -0
  37. package/dist/utils/color.js +14 -78
  38. package/dist/utils/commerce.cjs +66 -0
  39. package/dist/utils/commerce.js +11 -66
  40. package/dist/utils/common.cjs +37 -0
  41. package/dist/utils/common.js +3 -37
  42. package/dist/utils/company.cjs +70 -0
  43. package/dist/utils/company.js +12 -70
  44. package/dist/utils/database.cjs +54 -0
  45. package/dist/utils/database.js +8 -54
  46. package/dist/utils/datatype.cjs +38 -0
  47. package/dist/utils/datatype.js +4 -38
  48. package/dist/utils/datetime.cjs +78 -0
  49. package/dist/utils/datetime.js +14 -78
  50. package/dist/utils/finance.cjs +114 -0
  51. package/dist/utils/finance.js +23 -110
  52. package/dist/utils/food.cjs +70 -0
  53. package/dist/utils/food.js +12 -70
  54. package/dist/utils/git.cjs +54 -0
  55. package/dist/utils/git.js +8 -54
  56. package/dist/utils/hacker.cjs +58 -0
  57. package/dist/utils/hacker.js +9 -58
  58. package/dist/utils/image.cjs +58 -0
  59. package/dist/utils/image.js +9 -66
  60. package/dist/utils/index.cjs +143 -0
  61. package/dist/utils/index.js +29 -136
  62. package/dist/utils/internet.cjs +118 -0
  63. package/dist/utils/internet.js +24 -114
  64. package/dist/utils/location.cjs +110 -0
  65. package/dist/utils/location.js +22 -98
  66. package/dist/utils/lorem.cjs +70 -0
  67. package/dist/utils/lorem.js +12 -70
  68. package/dist/utils/music.cjs +50 -0
  69. package/dist/utils/music.js +7 -50
  70. package/dist/utils/number.cjs +62 -0
  71. package/dist/utils/number.js +10 -58
  72. package/dist/utils/person.cjs +94 -0
  73. package/dist/utils/person.js +18 -94
  74. package/dist/utils/phone.cjs +42 -0
  75. package/dist/utils/phone.js +5 -42
  76. package/dist/utils/science.cjs +42 -0
  77. package/dist/utils/science.js +5 -42
  78. package/dist/utils/string.cjs +78 -0
  79. package/dist/utils/string.js +14 -74
  80. package/dist/utils/system.cjs +82 -0
  81. package/dist/utils/system.js +15 -82
  82. package/dist/utils/utils.cjs +39 -0
  83. package/dist/utils/utils.js +3 -37
  84. package/dist/utils/vehicle.cjs +70 -0
  85. package/dist/utils/vehicle.js +12 -70
  86. package/dist/utils/word.cjs +70 -0
  87. package/dist/utils/word.js +12 -70
  88. package/package.json +26 -36
  89. package/biome.json +0 -31
  90. package/dist/config.mjs +0 -2
  91. package/dist/config_scheme.mjs +0 -46
  92. package/dist/create_config.mjs +0 -61
  93. package/dist/generator_fn.mjs +0 -2
  94. package/dist/index.mjs +0 -6
  95. package/dist/type.mjs +0 -0
  96. package/dist/utils/airline.d.ts +0 -14
  97. package/dist/utils/airline.mjs +0 -6
  98. package/dist/utils/animal.mjs +0 -18
  99. package/dist/utils/color.mjs +0 -14
  100. package/dist/utils/commerce.mjs +0 -11
  101. package/dist/utils/common.mjs +0 -3
  102. package/dist/utils/company.mjs +0 -12
  103. package/dist/utils/database.mjs +0 -8
  104. package/dist/utils/datatype.mjs +0 -4
  105. package/dist/utils/datetime.mjs +0 -14
  106. package/dist/utils/finance.mjs +0 -22
  107. package/dist/utils/food.mjs +0 -12
  108. package/dist/utils/git.mjs +0 -8
  109. package/dist/utils/hacker.mjs +0 -9
  110. package/dist/utils/image.mjs +0 -11
  111. package/dist/utils/index.mjs +0 -27
  112. package/dist/utils/internet.mjs +0 -23
  113. package/dist/utils/location.mjs +0 -19
  114. package/dist/utils/lorem.mjs +0 -12
  115. package/dist/utils/music.mjs +0 -7
  116. package/dist/utils/number.mjs +0 -9
  117. package/dist/utils/person.mjs +0 -18
  118. package/dist/utils/phone.mjs +0 -5
  119. package/dist/utils/science.mjs +0 -5
  120. package/dist/utils/string.mjs +0 -13
  121. package/dist/utils/system.mjs +0 -15
  122. package/dist/utils/utils.mjs +0 -5
  123. package/dist/utils/vehicle.mjs +0 -12
  124. package/dist/utils/word.mjs +0 -12
  125. package/pnpm-workspace.yaml +0 -2
  126. package/rslib.config.ts +0 -26
  127. package/src/config.ts +0 -8
  128. package/src/config_scheme.ts +0 -41
  129. package/src/create_config.test.ts +0 -70
  130. package/src/create_config.ts +0 -170
  131. package/src/create_generator_fn.test.ts +0 -229
  132. package/src/create_generator_fn.ts +0 -342
  133. package/src/generator_fn.ts +0 -9
  134. package/src/index.ts +0 -5
  135. package/src/type.ts +0 -167
  136. package/src/utils/airline.ts +0 -11
  137. package/src/utils/animal.ts +0 -47
  138. package/src/utils/color.ts +0 -41
  139. package/src/utils/commerce.ts +0 -32
  140. package/src/utils/common.ts +0 -4
  141. package/src/utils/company.ts +0 -33
  142. package/src/utils/database.ts +0 -19
  143. package/src/utils/datatype.ts +0 -5
  144. package/src/utils/datetime.ts +0 -35
  145. package/src/utils/finance.ts +0 -65
  146. package/src/utils/food.ts +0 -29
  147. package/src/utils/git.ts +0 -17
  148. package/src/utils/hacker.ts +0 -20
  149. package/src/utils/image.ts +0 -26
  150. package/src/utils/index.ts +0 -26
  151. package/src/utils/internet.ts +0 -62
  152. package/src/utils/location.ts +0 -58
  153. package/src/utils/lorem.ts +0 -29
  154. package/src/utils/music.ts +0 -14
  155. package/src/utils/number.ts +0 -20
  156. package/src/utils/person.ts +0 -47
  157. package/src/utils/phone.ts +0 -8
  158. package/src/utils/science.ts +0 -8
  159. package/src/utils/string.ts +0 -32
  160. package/src/utils/system.ts +0 -40
  161. package/src/utils/utils.ts +0 -8
  162. package/src/utils/vehicle.ts +0 -29
  163. package/src/utils/word.ts +0 -29
  164. package/tsconfig.json +0 -18
  165. /package/dist/{config.d.ts → types/config.d.ts} +0 -0
  166. /package/dist/{config_scheme.d.ts → types/config_scheme.d.ts} +0 -0
  167. /package/dist/{create_config.d.ts → types/create_config.d.ts} +0 -0
  168. /package/dist/{create_config.test.d.ts → types/create_config.test.d.ts} +0 -0
  169. /package/dist/{create_generator_fn.d.ts → types/create_generator_fn.d.ts} +0 -0
  170. /package/dist/{create_generator_fn.test.d.ts → types/create_generator_fn.test.d.ts} +0 -0
  171. /package/dist/{generator_fn.d.ts → types/generator_fn.d.ts} +0 -0
  172. /package/dist/{index.d.ts → types/index.d.ts} +0 -0
  173. /package/dist/{utils → types/utils}/commerce.d.ts +0 -0
  174. /package/dist/{utils → types/utils}/common.d.ts +0 -0
  175. /package/dist/{utils → types/utils}/company.d.ts +0 -0
  176. /package/dist/{utils → types/utils}/database.d.ts +0 -0
  177. /package/dist/{utils → types/utils}/datatype.d.ts +0 -0
  178. /package/dist/{utils → types/utils}/datetime.d.ts +0 -0
  179. /package/dist/{utils → types/utils}/food.d.ts +0 -0
  180. /package/dist/{utils → types/utils}/git.d.ts +0 -0
  181. /package/dist/{utils → types/utils}/hacker.d.ts +0 -0
  182. /package/dist/{utils → types/utils}/lorem.d.ts +0 -0
  183. /package/dist/{utils → types/utils}/music.d.ts +0 -0
  184. /package/dist/{utils → types/utils}/phone.d.ts +0 -0
  185. /package/dist/{utils → types/utils}/system.d.ts +0 -0
  186. /package/dist/{utils → types/utils}/utils.d.ts +0 -0
  187. /package/dist/{utils → types/utils}/vehicle.d.ts +0 -0
  188. /package/dist/{utils → types/utils}/word.d.ts +0 -0
@@ -1,170 +0,0 @@
1
- import {
2
- arrayConfigScheme,
3
- boundedSeriesScheme,
4
- objConfigScheme,
5
- selectionConfigScheme,
6
- tupleConfigScheme,
7
- valueConfigScheme,
8
- } from "./config_scheme";
9
- import type {
10
- ArrayConfig,
11
- BoundedSeriesConfig,
12
- ObjectConfig,
13
- ObjectConfigWithFn,
14
- Result,
15
- SelectionConfig,
16
- TupleConfig,
17
- TupleItems,
18
- ValueConfig,
19
- } from "./type";
20
-
21
- /**
22
- * value
23
- * @param {function} generateFn - The function used to generate the value.
24
- * @return {ValueConfig} The configuration object with the type "value" and the provided generate function.
25
- */
26
- export const createValueConfig = <T>(generateFn: () => T): ValueConfig<T> => {
27
- const config: ValueConfig<T> = {
28
- type: "value",
29
- generateFn,
30
- };
31
-
32
- valueConfigScheme.parse(config);
33
-
34
- return config;
35
- };
36
-
37
- /**
38
- * selection
39
- * @param {Array} items - The array of items to choose from.
40
- * @return {SelectionConfig} The configuration object with the type "select" and the provided items.
41
- */
42
- export const createSelectionConfig = <T>(items: T[]): SelectionConfig<T> => {
43
- const config: SelectionConfig<T> = { type: "select", items };
44
-
45
- selectionConfigScheme.parse(config);
46
-
47
- return config;
48
- };
49
-
50
- /**
51
- * object
52
- * @param {object} content
53
- * @param {function} transformer
54
- * @return {ObjectConfig}
55
- */
56
- export function createObjectConfig<T extends object>(
57
- content: T,
58
- ): ObjectConfig<T>;
59
- export function createObjectConfig<T extends object, R>(
60
- content: T,
61
- transformer: (v: { [K in keyof T]: Result<T[K]> }) => R,
62
- ): ObjectConfigWithFn<T, R>;
63
- export function createObjectConfig<T extends object, R>(
64
- content: T,
65
- transformer?: (v: { [K in keyof T]: Result<T[K]> }) => R,
66
- ) {
67
- if (transformer) {
68
- const config: ObjectConfigWithFn<T, R> = {
69
- type: "obj",
70
- content,
71
- transformer,
72
- };
73
- objConfigScheme.parse(config);
74
- return config;
75
- }
76
-
77
- const config: ObjectConfig<T> = { type: "obj", content };
78
-
79
- objConfigScheme.parse(config);
80
-
81
- return config;
82
- }
83
-
84
- /**
85
- * array
86
- * @param {object} item
87
- * @param {number} len
88
- * @param {function} next
89
- * @return {ArrayConfig}
90
- */
91
- export const createArrayConfig = <T>(
92
- item: T,
93
- len: number,
94
- next?: (prev: Result<T>, current: Result<T>) => Result<T>,
95
- ): ArrayConfig<T> => {
96
- const config: ArrayConfig<T> = { type: "arr", item, len, next };
97
-
98
- arrayConfigScheme.parse(config);
99
-
100
- return config;
101
- };
102
-
103
- /**
104
- * tuple
105
- * @param {Array} configItems
106
- * @return {TupleConfig}
107
- */
108
- interface CreateTupleConfig {
109
- <A, B, C, D, E, F, G, H, I, J>(
110
- configItems: [A, B, C, D, E, F, G, H, I, J],
111
- ): TupleConfig<A, B, C, D, E, F, G, H, I, J>;
112
- <A, B, C, D, E, F, G, H, I>(
113
- configItems: [A, B, C, D, E, F, G, H, I],
114
- ): TupleConfig<A, B, C, D, E, F, G, H, I>;
115
- <A, B, C, D, E, F, G, H>(
116
- configItems: [A, B, C, D, E, F, G, H],
117
- ): TupleConfig<A, B, C, D, E, F, G, H>;
118
- <A, B, C, D, E, F, G>(
119
- configItems: [A, B, C, D, E, F, G],
120
- ): TupleConfig<A, B, C, D, E, F, G>;
121
- <A, B, C, D, E, F>(
122
- configItems: [A, B, C, D, E, F],
123
- ): TupleConfig<A, B, C, D, E, F>;
124
- <A, B, C, D, E>(configItems: [A, B, C, D, E]): TupleConfig<A, B, C, D, E>;
125
- <A, B, C, D>(configItems: [A, B, C, D]): TupleConfig<A, B, C, D>;
126
- <A, B, C>(configItems: [A, B, C]): TupleConfig<A, B, C>;
127
- <A, B>(configItems: [A, B]): TupleConfig<A, B>;
128
- <A>(configItems: [A]): TupleConfig<A>;
129
- }
130
- export const createTupleConfig: CreateTupleConfig = <
131
- A,
132
- B,
133
- C,
134
- D,
135
- E,
136
- F,
137
- G,
138
- H,
139
- I,
140
- J,
141
- >(
142
- configItems: TupleItems<A, B, C, D, E, F, G, H, I, J>,
143
- ) => {
144
- const config: TupleConfig<A, B, C, D, E, F, G, H, I, J> = {
145
- type: "tuple",
146
- configItems,
147
- };
148
-
149
- tupleConfigScheme.parse(config);
150
-
151
- return config;
152
- };
153
-
154
- /**
155
- * bounded series
156
- * @param {{ upperLimit: number, lowerLimit: number, createInitValue: () => number, count: number }} config
157
- * @return {BoundedSeriesConfig}
158
- */
159
- export const createBoundedSeriesConfig = (
160
- config: Omit<BoundedSeriesConfig, "type">,
161
- ): BoundedSeriesConfig => {
162
- const newConfig: BoundedSeriesConfig = {
163
- type: "bounded_series",
164
- ...config,
165
- };
166
-
167
- boundedSeriesScheme.parse(newConfig);
168
-
169
- return newConfig;
170
- };
@@ -1,229 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import {
3
- createArrayConfig,
4
- createObjectConfig,
5
- createTupleConfig,
6
- createValueConfig,
7
- } from "./create_config";
8
- import {
9
- createArrayGenerator,
10
- createBoundedSeriesGenerator,
11
- createGeneratorByType,
12
- createObjectGenerator,
13
- createSelectionGenerator,
14
- createTupleGenerator,
15
- createValueGenerator,
16
- } from "./create_generator_fn";
17
- import type { ObjectConfig } from "./type";
18
-
19
- describe("createValueGenerator", () => {
20
- test("normal", () => {
21
- const value = createValueGenerator({
22
- type: "value",
23
- generateFn: () => 50,
24
- })();
25
-
26
- expect(value).toBe(50);
27
-
28
- const value2 = createValueGenerator({
29
- type: "value",
30
- generateFn: () => ({ age: 100, name: "hello" }),
31
- })();
32
-
33
- expect(value2).toEqual({ age: 100, name: "hello" });
34
- });
35
- });
36
-
37
- describe("createSelectionGenerator", () => {
38
- test("normal", () => {
39
- const value = createSelectionGenerator({
40
- type: "select",
41
- items: [1],
42
- })();
43
-
44
- expect(value).toBe(1);
45
-
46
- const value2 = createSelectionGenerator({
47
- type: "select",
48
- items: [30, 30, 30, 30],
49
- })();
50
-
51
- expect(value2).toBe(30);
52
- });
53
- });
54
-
55
- describe("createArrayGenerator", () => {
56
- test("normal", () => {
57
- const list = createArrayGenerator({
58
- type: "arr",
59
- len: 5,
60
- item: { type: "value", generateFn: () => ({ age: 42 }) },
61
- })();
62
-
63
- expect(list).toEqual([
64
- { age: 42 },
65
- { age: 42 },
66
- { age: 42 },
67
- { age: 42 },
68
- { age: 42 },
69
- ]);
70
- });
71
- test("with next function", () => {
72
- const list = createArrayGenerator(
73
- createArrayConfig(
74
- createValueConfig(() => 100),
75
- 5,
76
- (prev) => prev + 1,
77
- ),
78
- )();
79
-
80
- expect(list).toEqual([101, 102, 103, 104, 105]);
81
- });
82
- });
83
-
84
- describe("createTupleGenerator", () => {
85
- test("normal", () => {
86
- const tuple = createTupleGenerator(
87
- createTupleConfig([
88
- createValueConfig(() => 225),
89
- createValueConfig(() => "hello world"),
90
- ]),
91
- )();
92
-
93
- expect(tuple.length).toBe(2);
94
- const [num, str] = tuple;
95
- expect(num).toBe(225);
96
- expect(str).toBe("hello world");
97
- });
98
- });
99
-
100
- describe("createObjectGenerator", () => {
101
- test("normal", () => {
102
- const obj = createObjectGenerator({
103
- type: "obj",
104
- content: {
105
- name: { type: "value", generateFn: () => "John" },
106
- age: { type: "value", generateFn: () => 50 },
107
- location: { type: "value", generateFn: () => "Taiwan" },
108
- },
109
- })();
110
-
111
- expect(obj).toEqual({ name: "John", age: 50, location: "Taiwan" });
112
- });
113
-
114
- test("transformer function", () => {
115
- const obj = createObjectGenerator(
116
- createObjectConfig(
117
- {
118
- name: createValueConfig(() => "John"),
119
- age: createValueConfig(() => 50),
120
- location: createValueConfig(() => "Taiwan"),
121
- },
122
- ({ age }) => age + 50,
123
- ),
124
- )();
125
-
126
- expect(obj).toEqual(100);
127
- });
128
- });
129
-
130
- describe("createBoundedSeriesGenerator", () => {
131
- test("normal", () => {
132
- const upperLimit = 1.1;
133
- const lowerLimit = 0.9;
134
- const initValue = 100;
135
- const count = 100;
136
-
137
- const list = createBoundedSeriesGenerator({
138
- type: "bounded_series",
139
- upperLimit,
140
- lowerLimit,
141
- createInitValue: () => initValue,
142
- count,
143
- })();
144
-
145
- for (let i = 0; i < count; i++) {
146
- const value = list[i];
147
- if (i === 0) {
148
- const ratio = value / initValue;
149
-
150
- expect(ratio).toBeLessThanOrEqual(upperLimit);
151
- expect(ratio).toBeGreaterThanOrEqual(lowerLimit);
152
-
153
- continue;
154
- }
155
-
156
- const prevValue = list[i - 1];
157
-
158
- const ratio = value / prevValue;
159
-
160
- expect(ratio).toBeLessThanOrEqual(upperLimit);
161
- expect(ratio).toBeGreaterThanOrEqual(lowerLimit);
162
- }
163
- });
164
- });
165
-
166
- describe("createGeneratorByType", () => {
167
- test("normal", () => {
168
- const config = createObjectConfig({
169
- name: createValueConfig(() => "John"),
170
- age: createValueConfig(() => 50),
171
- locations: createArrayConfig(
172
- createValueConfig(() => "Taiwan"),
173
- 5,
174
- ),
175
- });
176
- const result = createGeneratorByType(config)();
177
-
178
- expect(result).toEqual({
179
- name: "John",
180
- age: 50,
181
- locations: ["Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan"],
182
- });
183
- });
184
-
185
- test("test error config", () => {
186
- const config = {
187
- type: "obj",
188
- content: {
189
- name: {
190
- type: "123",
191
- generateFn: "John",
192
- },
193
- },
194
- } as ObjectConfig<unknown>;
195
-
196
- expect(() => createGeneratorByType(config)).toThrowError();
197
- });
198
-
199
- test("with custom type match", () => {
200
- const createIntValueConfig = (_option) => createValueConfig(() => 50);
201
- const createEmailValueConfig = (_option) =>
202
- createValueConfig(() => "xxx@example.com");
203
-
204
- const customTypeMatch = (config) => {
205
- if (config.type === "int") {
206
- return createIntValueConfig(config.option);
207
- }
208
- if (config.type === "email") {
209
- return createEmailValueConfig(config.option);
210
- }
211
-
212
- throw Error("error");
213
- };
214
-
215
- const config = createObjectConfig({
216
- name: { type: "value", generateFn: () => "John" },
217
- age: { type: "int" },
218
- email: { type: "email" },
219
- });
220
-
221
- const result = createGeneratorByType(config, customTypeMatch)();
222
-
223
- expect(result).toEqual({
224
- name: "John",
225
- age: 50,
226
- email: "xxx@example.com",
227
- });
228
- });
229
- });