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 +2 -0
- package/package.json +2 -1
- package/src/create_config.js +1 -13
- package/src/create_generator_fn.js +29 -28
- package/src/create_generator_fn.test.js +60 -0
package/index.js
ADDED
package/package.json
CHANGED
package/src/create_config.js
CHANGED
|
@@ -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
|
|
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
|
|
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 {
|
|
39
|
+
* @param {(() => ValueConfig)=} customTypeMatch
|
|
42
40
|
* @return {() => object}
|
|
43
41
|
*/
|
|
44
|
-
export const createObjectGenerator = (config,
|
|
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,
|
|
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 {
|
|
62
|
+
* @param {(() => ValueConfig)=} customTypeMatch
|
|
65
63
|
* @return {() => Array}
|
|
66
64
|
*/
|
|
67
|
-
export const createArrayGenerator = (config,
|
|
65
|
+
export const createArrayGenerator = (config, customTypeMatch) => {
|
|
68
66
|
arrayConfigScheme.parse(config);
|
|
69
67
|
|
|
70
|
-
const itemGeneratorFn = createGeneratorByType(config.item,
|
|
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 {
|
|
76
|
+
* @param {(() => ValueConfig)=} customTypeMatch
|
|
79
77
|
* @return {() => Array}
|
|
80
78
|
*/
|
|
81
|
-
export const createTupleGenerator = (config,
|
|
79
|
+
export const createTupleGenerator = (config, customTypeMatch) => {
|
|
82
80
|
tupleConfigScheme.parse(config);
|
|
83
81
|
|
|
84
|
-
const itemsFns = config.configItems.map(
|
|
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
|
|
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 {
|
|
116
|
+
* @param {(() => ValueConfig)=} customTypeMatch
|
|
118
117
|
* @return {function}
|
|
119
118
|
*/
|
|
120
|
-
export const createGeneratorByType = (config,
|
|
119
|
+
export const createGeneratorByType = (config, customTypeMatch) => {
|
|
121
120
|
switch (config.type) {
|
|
122
121
|
case "obj":
|
|
123
|
-
return createObjectGenerator(config,
|
|
122
|
+
return createObjectGenerator(config, customTypeMatch);
|
|
124
123
|
case "arr":
|
|
125
|
-
return createArrayGenerator(config,
|
|
124
|
+
return createArrayGenerator(config, customTypeMatch);
|
|
126
125
|
case "select":
|
|
127
|
-
return createSelectionGenerator(config
|
|
126
|
+
return createSelectionGenerator(config);
|
|
128
127
|
case "tuple":
|
|
129
|
-
return createTupleGenerator(config,
|
|
128
|
+
return createTupleGenerator(config, customTypeMatch);
|
|
130
129
|
case "value":
|
|
131
|
-
return createValueGenerator(config
|
|
130
|
+
return createValueGenerator(config);
|
|
132
131
|
case "bounded_series":
|
|
133
|
-
return createBoundedSeriesGenerator(config
|
|
134
|
-
default:
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
});
|