struct-fakerator 1.0.0 → 1.1.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.
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./src/create_config";
2
+ export * from "./src/create_generator_fn";
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "struct-fakerator",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "start": "node src/index.js",
9
+ "dev": "node",
9
10
  "test": "vitest"
10
11
  },
11
12
  "keywords": [],
@@ -6,7 +6,7 @@ import {
6
6
  tupleConfigScheme,
7
7
  objConfigScheme,
8
8
  boundedSeriesScheme,
9
- } from "./config_scheme";
9
+ } from "./config_scheme.js";
10
10
 
11
11
  /**
12
12
  * value
@@ -95,15 +95,3 @@ export const createBoundedSeriesConfig = (config) => {
95
95
 
96
96
  return newConfig;
97
97
  };
98
-
99
- // int value
100
- export const createIntValueConfig = (option) =>
101
- createValueConfig(() => faker.number.int(option));
102
-
103
- // float value
104
- export const createFloatValueConfig = (option) =>
105
- createValueConfig(() => faker.number.float(option));
106
-
107
- // email value
108
- export const createEmailValueConfig = (option) =>
109
- createValueConfig(() => faker.internet.email(option));
@@ -7,15 +7,14 @@ import {
7
7
  objConfigScheme,
8
8
  boundedSeriesScheme,
9
9
  tupleConfigScheme,
10
- } from "./config_scheme";
10
+ } from "./config_scheme.js";
11
11
 
12
12
  /**
13
13
  * value
14
14
  * @param {ValueConfig} config
15
- * @param {number=} level
16
15
  * @return {function}
17
16
  */
18
- export const createValueGenerator = (config, level = 0) => {
17
+ export const createValueGenerator = (config) => {
19
18
  valueConfigScheme.parse(config);
20
19
 
21
20
  return config.generateFn;
@@ -24,10 +23,9 @@ export const createValueGenerator = (config, level = 0) => {
24
23
  /**
25
24
  * selection
26
25
  * @param {SelectionConfig} config
27
- * @param {number=} level
28
26
  * @return {function} The configuration object with the type "select" and the provided items.
29
27
  */
30
- export const createSelectionGenerator = (config, level = 0) => {
28
+ export const createSelectionGenerator = (config) => {
31
29
  selectionConfigScheme.parse(config);
32
30
 
33
31
  const { items } = config;
@@ -38,15 +36,15 @@ export const createSelectionGenerator = (config, level = 0) => {
38
36
  /**
39
37
  * object
40
38
  * @param {ObjectConfig} config
41
- * @param {number=} level
39
+ * @param {(() => ValueConfig)=} customTypeMatch
42
40
  * @return {() => object}
43
41
  */
44
- export const createObjectGenerator = (config, level = 0) => {
42
+ export const createObjectGenerator = (config, customTypeMatch) => {
45
43
  objConfigScheme.parse(config);
46
44
 
47
45
  const keyWithFns = Object.entries(config.content).map(([key, subConfig]) => [
48
46
  key,
49
- createGeneratorByType(subConfig, level + 1),
47
+ createGeneratorByType(subConfig, customTypeMatch),
50
48
  ]);
51
49
 
52
50
  return () => {
@@ -61,13 +59,13 @@ export const createObjectGenerator = (config, level = 0) => {
61
59
  /**
62
60
  * array
63
61
  * @param {ArrayConfig} config
64
- * @param {number=} level
62
+ * @param {(() => ValueConfig)=} customTypeMatch
65
63
  * @return {() => Array}
66
64
  */
67
- export const createArrayGenerator = (config, level = 0) => {
65
+ export const createArrayGenerator = (config, customTypeMatch) => {
68
66
  arrayConfigScheme.parse(config);
69
67
 
70
- const itemGeneratorFn = createGeneratorByType(config.item, level + 1);
68
+ const itemGeneratorFn = createGeneratorByType(config.item, customTypeMatch);
71
69
 
72
70
  return () => Array.from({ length: config.len ?? 0 }, itemGeneratorFn);
73
71
  };
@@ -75,13 +73,15 @@ export const createArrayGenerator = (config, level = 0) => {
75
73
  /**
76
74
  * tuple
77
75
  * @param {TupleConfig} config
78
- * @param {number=} level
76
+ * @param {(() => ValueConfig)=} customTypeMatch
79
77
  * @return {() => Array}
80
78
  */
81
- export const createTupleGenerator = (config, level = 0) => {
79
+ export const createTupleGenerator = (config, customTypeMatch) => {
82
80
  tupleConfigScheme.parse(config);
83
81
 
84
- const itemsFns = config.configItems.map(createGeneratorByType);
82
+ const itemsFns = config.configItems.map((configItem) =>
83
+ createGeneratorByType(configItem, customTypeMatch),
84
+ );
85
85
 
86
86
  return () => itemsFns.map((generateFn) => generateFn());
87
87
  };
@@ -89,10 +89,9 @@ export const createTupleGenerator = (config, level = 0) => {
89
89
  /**
90
90
  * bounded series
91
91
  * @param {BoundedSeriesConfig} config
92
- * @param {number} level
93
92
  * @return {() => Array<number>}
94
93
  */
95
- export const createBoundedSeriesGenerator = (config, level = 0) => {
94
+ export const createBoundedSeriesGenerator = (config) => {
96
95
  boundedSeriesScheme.parse(config);
97
96
 
98
97
  const { upperLimit, lowerLimit, createInitValue, count } = config;
@@ -114,26 +113,28 @@ export const createBoundedSeriesGenerator = (config, level = 0) => {
114
113
  /**
115
114
  *
116
115
  * @param {ValueConfig | SelectionConfig | ArrayConfig | ObjectConfig | TupleConfig | BoundedSeriesConfig} config
117
- * @param {number=} level
116
+ * @param {(() => ValueConfig)=} customTypeMatch
118
117
  * @return {function}
119
118
  */
120
- export const createGeneratorByType = (config, level = 0) => {
119
+ export const createGeneratorByType = (config, customTypeMatch) => {
121
120
  switch (config.type) {
122
121
  case "obj":
123
- return createObjectGenerator(config, level);
122
+ return createObjectGenerator(config, customTypeMatch);
124
123
  case "arr":
125
- return createArrayGenerator(config, level);
124
+ return createArrayGenerator(config, customTypeMatch);
126
125
  case "select":
127
- return createSelectionGenerator(config, level);
126
+ return createSelectionGenerator(config);
128
127
  case "tuple":
129
- return createTupleGenerator(config, level);
128
+ return createTupleGenerator(config, customTypeMatch);
130
129
  case "value":
131
- return createValueGenerator(config, level);
130
+ return createValueGenerator(config);
132
131
  case "bounded_series":
133
- return createBoundedSeriesGenerator(config, level);
134
- default:
135
- throw Error(
136
- `level ${level} config type "${config.type}" is not supported`,
137
- );
132
+ return createBoundedSeriesGenerator(config);
133
+ default: {
134
+ if (customTypeMatch) {
135
+ return createValueGenerator(customTypeMatch(config));
136
+ }
137
+ throw Error(`config type "${config.type}" is not supported`);
138
+ }
138
139
  }
139
140
  };
@@ -7,7 +7,9 @@ import {
7
7
  createTupleGenerator,
8
8
  createObjectGenerator,
9
9
  createBoundedSeriesGenerator,
10
+ createGeneratorByType,
10
11
  } from "./create_generator_fn";
12
+ import { createValueConfig } from "./create_config";
11
13
 
12
14
  describe("createValueGenerator", () => {
13
15
  test("normal", () => {
@@ -130,3 +132,61 @@ describe("createBoundedSeriesGenerator", () => {
130
132
  }
131
133
  });
132
134
  });
135
+
136
+ describe("createGeneratorByType", () => {
137
+ test("normal", () => {
138
+ const config = {
139
+ type: "obj",
140
+ content: {
141
+ name: { type: "value", generateFn: () => "John" },
142
+ age: { type: "value", generateFn: () => 50 },
143
+ locations: {
144
+ type: "arr",
145
+ item: { type: "value", generateFn: () => "Taiwan" },
146
+ len: 5,
147
+ },
148
+ },
149
+ };
150
+ const result = createGeneratorByType(config)();
151
+
152
+ expect(result).toEqual({
153
+ name: "John",
154
+ age: 50,
155
+ locations: ["Taiwan", "Taiwan", "Taiwan", "Taiwan", "Taiwan"],
156
+ });
157
+ });
158
+
159
+ test("with custom type match", () => {
160
+ const createIntValueConfig = (option) => createValueConfig(() => 50);
161
+ const createEmailValueConfig = (option) =>
162
+ createValueConfig(() => "xxx@example.com");
163
+
164
+ const customTypeMatch = (config) => {
165
+ if (config.type === "int") {
166
+ return createIntValueConfig(config.option);
167
+ }
168
+ if (config.type === "email") {
169
+ return createEmailValueConfig(config.option);
170
+ }
171
+
172
+ throw Error("error");
173
+ };
174
+
175
+ const config = {
176
+ type: "obj",
177
+ content: {
178
+ name: { type: "value", generateFn: () => "John" },
179
+ age: { type: "int" },
180
+ email: { type: "email" },
181
+ },
182
+ };
183
+
184
+ const result = createGeneratorByType(config, customTypeMatch)();
185
+
186
+ expect(result).toEqual({
187
+ name: "John",
188
+ age: 50,
189
+ email: "xxx@example.com",
190
+ });
191
+ });
192
+ });