struct-fakerator 1.2.6 → 2.0.1

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.
@@ -1,97 +0,0 @@
1
- import { faker } from "@faker-js/faker";
2
- import {
3
- valueConfigScheme,
4
- arrayConfigScheme,
5
- selectionConfigScheme,
6
- tupleConfigScheme,
7
- objConfigScheme,
8
- boundedSeriesScheme,
9
- } from "./config_scheme.mjs";
10
-
11
- /**
12
- * value
13
- * @param {function} generateFn - The function used to generate the value.
14
- * @return {ValueConfig} The configuration object with the type "value" and the provided generate function.
15
- */
16
- export const createValueConfig = (generateFn) => {
17
- const config = {
18
- type: "value",
19
- generateFn,
20
- };
21
-
22
- valueConfigScheme.parse(config);
23
-
24
- return config;
25
- };
26
-
27
- /**
28
- * selection
29
- * @param {Array} items - The array of items to choose from.
30
- * @return {SelectionConfig} The configuration object with the type "select" and the provided items.
31
- */
32
- export const createSelectionConfig = (items) => {
33
- const config = { type: "select", items };
34
-
35
- selectionConfigScheme.parse(config);
36
-
37
- return config;
38
- };
39
-
40
- /**
41
- * object
42
- * @param {object} content
43
- * @return {ObjectConfig}
44
- */
45
- export const createObjectConfig = (content) => {
46
- const config = { type: "obj", content };
47
-
48
- objConfigScheme.parse(config);
49
-
50
- return config;
51
- };
52
-
53
- /**
54
- * array
55
- * @param {object} item
56
- * @param {number} len
57
- * @return {ArrayConfig}
58
- */
59
- export const createArrayConfig = (item, len) => {
60
- const config = { type: "arr", item, len };
61
-
62
- arrayConfigScheme.parse(config);
63
-
64
- return config;
65
- };
66
-
67
- /**
68
- * tuple
69
- * @param {Array} configItems
70
- * @return {TupleConfig}
71
- */
72
- export const createTupleConfig = (configItems) => {
73
- const config = {
74
- type: "tuple",
75
- configItems,
76
- };
77
-
78
- tupleConfigScheme.parse(config);
79
-
80
- return config;
81
- };
82
-
83
- /**
84
- * bounded series
85
- * @param {{ upperLimit: number, lowerLimit: number, createInitValue: () => number, count: number }} config
86
- * @return {BoundedSeriesConfig}
87
- */
88
- export const createBoundedSeriesConfig = (config) => {
89
- const newConfig = {
90
- type: "bounded_series",
91
- ...config,
92
- };
93
-
94
- boundedSeriesScheme.parse(newConfig);
95
-
96
- return newConfig;
97
- };
@@ -1,72 +0,0 @@
1
- import { expect, expectTypeOf, test } from "vitest";
2
-
3
- import {
4
- createValueConfig,
5
- createSelectionConfig,
6
- createArrayConfig,
7
- createTupleConfig,
8
- createObjectConfig,
9
- createBoundedSeriesConfig,
10
- } from "./create_config.mjs";
11
- import { createGeneratorByType } from "./create_generator_fn.mjs";
12
-
13
- test("createValueConfig", () => {
14
- const valueConfig = createValueConfig(() => 44);
15
-
16
- expect(valueConfig.type).toBe("value");
17
- expect(valueConfig.generateFn).toBeTypeOf("function");
18
- });
19
-
20
- test("createSelectionConfig", () => {
21
- const options = [1, 2, 3, 4];
22
- const selectionConfig = createSelectionConfig([1, 2, 3, 4]);
23
-
24
- expect(selectionConfig.type).toBe("select");
25
- expect(selectionConfig.items).toEqual(options);
26
- });
27
-
28
- test("createArrayConfig", () => {
29
- const valueConfig = createValueConfig(() => 44);
30
- const arrConfig = createArrayConfig(valueConfig, 20);
31
-
32
- expect(arrConfig.type).toBe("arr");
33
- expect(arrConfig.len).toBe(20);
34
- expect(arrConfig.item).toEqual(valueConfig);
35
- });
36
-
37
- test("createTupleConfig", () => {
38
- const value1Config = createValueConfig(() => 123);
39
- const value2Config = createValueConfig(() => "hello");
40
-
41
- const tupleConfig = createTupleConfig([value1Config, value2Config]);
42
-
43
- expect(tupleConfig.type).toBe("tuple");
44
- expect(tupleConfig.configItems).toEqual([value1Config, value2Config]);
45
- });
46
-
47
- test("createObjConfig", () => {
48
- const value1Config = createValueConfig(() => 32);
49
- const value2Config = createValueConfig(() => "frank");
50
-
51
- const objConfig = createObjectConfig({
52
- name: value2Config,
53
- age: value1Config,
54
- });
55
-
56
- expect(objConfig.type).toBe("obj");
57
- expect(objConfig.content).toEqual({ name: value2Config, age: value1Config });
58
- });
59
-
60
- test("createBoundedSeriesConfig", () => {
61
- const boundedSeriesConfig = createBoundedSeriesConfig({
62
- count: 1,
63
- upperLimit: 1.2,
64
- lowerLimit: 1.0,
65
- createInitValue: () => 40,
66
- });
67
-
68
- expect(boundedSeriesConfig.type).toBe("bounded_series");
69
- expect(boundedSeriesConfig.upperLimit).toBe(1.2);
70
- expect(boundedSeriesConfig.lowerLimit).toBe(1.0);
71
- expect(boundedSeriesConfig.createInitValue).toBeTypeOf("function");
72
- });
@@ -1,35 +0,0 @@
1
- declare function createValueGenerator<T>(config: ValueConfig<T>): () => T;
2
-
3
- declare function createSelectionGenerator<T>(
4
- config: SelectionConfig<T>,
5
- ): () => T;
6
-
7
- declare function createObjectGenerator(
8
- config: ObjectConfig,
9
- customTypeMatch?: (config: object) => ValueConfig<unknown>,
10
- ): () => Record<string, unknown>;
11
-
12
- declare function createArrayGenerator(
13
- config: ArrayConfig,
14
- customTypeMatch?: (config: object) => ValueConfig<unknown>,
15
- ): () => unknown[];
16
-
17
- declare function createTupleGenerator(
18
- config: TupleConfig,
19
- customTypeMatch?: (config: object) => ValueConfig<unknown>,
20
- ): () => unknown[];
21
-
22
- declare function createBoundedSeriesGenerator(
23
- config: BoundedSeriesConfig,
24
- ): () => number[];
25
-
26
- declare function createGeneratorByType(
27
- config:
28
- | ValueConfig<unknown>
29
- | SelectionConfig<unknown>
30
- | ArrayConfig
31
- | ObjectConfig
32
- | TupleConfig
33
- | BoundedSeriesConfig,
34
- customTypeMatch?: (config: object) => ValueConfig<unknown>,
35
- ): () => unknown;
@@ -1,140 +0,0 @@
1
- import { faker } from "@faker-js/faker";
2
-
3
- import {
4
- valueConfigScheme,
5
- selectionConfigScheme,
6
- arrayConfigScheme,
7
- objConfigScheme,
8
- boundedSeriesScheme,
9
- tupleConfigScheme,
10
- } from "./config_scheme.mjs";
11
-
12
- /**
13
- * value
14
- * @param {ValueConfig} config
15
- * @return {function}
16
- */
17
- export const createValueGenerator = (config) => {
18
- valueConfigScheme.parse(config);
19
-
20
- return config.generateFn;
21
- };
22
-
23
- /**
24
- * selection
25
- * @param {SelectionConfig} config
26
- * @return {function} The configuration object with the type "select" and the provided items.
27
- */
28
- export const createSelectionGenerator = (config) => {
29
- selectionConfigScheme.parse(config);
30
-
31
- const { items } = config;
32
-
33
- return () => items[faker.number.int(items.length - 1)];
34
- };
35
-
36
- /**
37
- * object
38
- * @param {ObjectConfig} config
39
- * @param {(() => ValueConfig)=} customTypeMatch
40
- * @return {() => object}
41
- */
42
- export const createObjectGenerator = (config, customTypeMatch) => {
43
- objConfigScheme.parse(config);
44
-
45
- const keyWithFns = Object.entries(config.content).map(([key, subConfig]) => [
46
- key,
47
- createGeneratorByType(subConfig, customTypeMatch),
48
- ]);
49
-
50
- return () => {
51
- const result = {};
52
- for (const [key, generateFn] of keyWithFns) {
53
- result[key] = generateFn();
54
- }
55
- return result;
56
- };
57
- };
58
-
59
- /**
60
- * array
61
- * @param {ArrayConfig} config
62
- * @param {(() => ValueConfig)=} customTypeMatch
63
- * @return {() => Array}
64
- */
65
- export const createArrayGenerator = (config, customTypeMatch) => {
66
- arrayConfigScheme.parse(config);
67
-
68
- const itemGeneratorFn = createGeneratorByType(config.item, customTypeMatch);
69
-
70
- return () => Array.from({ length: config.len ?? 0 }, itemGeneratorFn);
71
- };
72
-
73
- /**
74
- * tuple
75
- * @param {TupleConfig} config
76
- * @param {(() => ValueConfig)=} customTypeMatch
77
- * @return {() => Array}
78
- */
79
- export const createTupleGenerator = (config, customTypeMatch) => {
80
- tupleConfigScheme.parse(config);
81
-
82
- const itemsFns = config.configItems.map((configItem) =>
83
- createGeneratorByType(configItem, customTypeMatch),
84
- );
85
-
86
- return () => itemsFns.map((generateFn) => generateFn());
87
- };
88
-
89
- /**
90
- * bounded series
91
- * @param {BoundedSeriesConfig} config
92
- * @return {() => Array<number>}
93
- */
94
- export const createBoundedSeriesGenerator = (config) => {
95
- boundedSeriesScheme.parse(config);
96
-
97
- const { upperLimit, lowerLimit, createInitValue, count } = config;
98
-
99
- return () => {
100
- let value = createInitValue();
101
-
102
- const boundedSeries = [];
103
-
104
- for (let i = 0; i < count; i++) {
105
- value = faker.number.float({ max: upperLimit, min: lowerLimit }) * value;
106
- boundedSeries.push(value);
107
- }
108
-
109
- return boundedSeries;
110
- };
111
- };
112
-
113
- /**
114
- *
115
- * @param {ValueConfig | SelectionConfig | ArrayConfig | ObjectConfig | TupleConfig | BoundedSeriesConfig} config
116
- * @param {(() => ValueConfig)=} customTypeMatch
117
- * @return {function}
118
- */
119
- export const createGeneratorByType = (config, customTypeMatch) => {
120
- switch (config.type) {
121
- case "obj":
122
- return createObjectGenerator(config, customTypeMatch);
123
- case "arr":
124
- return createArrayGenerator(config, customTypeMatch);
125
- case "select":
126
- return createSelectionGenerator(config);
127
- case "tuple":
128
- return createTupleGenerator(config, customTypeMatch);
129
- case "value":
130
- return createValueGenerator(config);
131
- case "bounded_series":
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
- }
139
- }
140
- };
@@ -1,192 +0,0 @@
1
- import { describe, test, expect } from "vitest";
2
- import { ZodError } from "zod";
3
- import {
4
- createValueGenerator,
5
- createSelectionGenerator,
6
- createArrayGenerator,
7
- createTupleGenerator,
8
- createObjectGenerator,
9
- createBoundedSeriesGenerator,
10
- createGeneratorByType,
11
- } from "./create_generator_fn.mjs";
12
- import { createValueConfig } from "./create_config.mjs";
13
-
14
- describe("createValueGenerator", () => {
15
- test("normal", () => {
16
- const value = createValueGenerator({
17
- type: "value",
18
- generateFn: () => 50,
19
- })();
20
-
21
- expect(value).toBe(50);
22
-
23
- const value2 = createValueGenerator({
24
- type: "value",
25
- generateFn: () => ({ age: 100, name: "hello" }),
26
- })();
27
-
28
- expect(value2).toEqual({ age: 100, name: "hello" });
29
- });
30
- });
31
-
32
- describe("createSelectionGenerator", () => {
33
- test("normal", () => {
34
- const value = createSelectionGenerator({
35
- type: "select",
36
- items: [1],
37
- })();
38
-
39
- expect(value).toBe(1);
40
-
41
- const value2 = createSelectionGenerator({
42
- type: "select",
43
- items: [30, 30, 30, 30],
44
- })();
45
-
46
- expect(value2).toBe(30);
47
- });
48
- });
49
-
50
- describe("createArrayGenerator", () => {
51
- test("normal", () => {
52
- const list = createArrayGenerator({
53
- type: "arr",
54
- len: 5,
55
- item: { type: "value", generateFn: () => ({ age: 42 }) },
56
- })();
57
-
58
- expect(list).toEqual([
59
- { age: 42 },
60
- { age: 42 },
61
- { age: 42 },
62
- { age: 42 },
63
- { age: 42 },
64
- ]);
65
- });
66
- });
67
-
68
- describe("createTupleGenerator", () => {
69
- test("normal", () => {
70
- const tuple = createTupleGenerator({
71
- type: "tuple",
72
- configItems: [
73
- { type: "value", generateFn: () => 225 },
74
- { type: "value", generateFn: () => "hello world" },
75
- ],
76
- })();
77
-
78
- expect(tuple.length).toBe(2);
79
- const [num, str] = tuple;
80
- expect(num).toBe(225);
81
- expect(str).toBe("hello world");
82
- });
83
- });
84
-
85
- describe("createObjectGenerator", () => {
86
- test("normal", () => {
87
- const obj = createObjectGenerator({
88
- type: "obj",
89
- content: {
90
- name: { type: "value", generateFn: () => "John" },
91
- age: { type: "value", generateFn: () => 50 },
92
- location: { type: "value", generateFn: () => "Taiwan" },
93
- },
94
- })();
95
-
96
- expect(obj).toEqual({ name: "John", age: 50, location: "Taiwan" });
97
- });
98
- });
99
-
100
- describe("createBoundedSeriesGenerator", () => {
101
- test("normal", () => {
102
- const upperLimit = 1.1;
103
- const lowerLimit = 0.9;
104
- const initValue = 100;
105
- const count = 100;
106
-
107
- const list = createBoundedSeriesGenerator({
108
- type: "bounded_series",
109
- upperLimit,
110
- lowerLimit,
111
- createInitValue: () => initValue,
112
- count,
113
- })();
114
-
115
- for (let i = 0; i < count; i++) {
116
- const value = list[i];
117
- if (i === 0) {
118
- const ratio = value / initValue;
119
-
120
- expect(ratio).toBeLessThanOrEqual(upperLimit);
121
- expect(ratio).toBeGreaterThanOrEqual(lowerLimit);
122
-
123
- continue;
124
- }
125
-
126
- const prevValue = list[i - 1];
127
-
128
- const ratio = value / prevValue;
129
-
130
- expect(ratio).toBeLessThanOrEqual(upperLimit);
131
- expect(ratio).toBeGreaterThanOrEqual(lowerLimit);
132
- }
133
- });
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
- });
package/esm/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- export * from "./create_config.mjs";
2
- export * from "./create_generator_fn.mjs";
package/esm/type.mjs DELETED
@@ -1,23 +0,0 @@
1
- /**
2
- * @typedef {{ type: 'value', generateFn: Function}} ValueConfig
3
- */
4
-
5
- /**
6
- * @typedef {{ type: 'select', items: Array }} SelectionConfig
7
- */
8
-
9
- /**
10
- * @typedef {{ type: 'arr', item: Object, len: number }} ArrayConfig
11
- */
12
-
13
- /**
14
- * @typedef {{ type: 'tuple', configItems: Array }} TupleConfig
15
- */
16
-
17
- /**
18
- * @typedef {{ type: 'obj', content: Object }} ObjectConfig
19
- */
20
-
21
- /**
22
- * @typedef {{ type: 'bounded_series', upperLimit: number, lowerLimit: number, createInitValue: function, count: number }} BoundedSeriesConfig
23
- */
package/jsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es6",
4
- "module": "CommonJS",
5
- "baseUrl": ".",
6
- "paths": {
7
- "*": [
8
- "node_modules/*"
9
- ]
10
- },
11
- "checkJs": true,
12
- },
13
- "include": [
14
- "./**/*"
15
- ],
16
- "exclude": [
17
- "node_modules",
18
- "**/node_modules/*"
19
- ]
20
- }
@@ -1,45 +0,0 @@
1
- import { z } from "zod";
2
-
3
- export const valueConfigScheme = z.object({
4
- type: z.string().regex(/^value$/, { message: "invalid type string" }),
5
- generateFn: z.function(),
6
- });
7
-
8
- export const selectionConfigScheme = z.object({
9
- type: z.string().regex(/^select$/, { message: "invalid type string" }),
10
- items: z.any().array().nonempty({ message: "items can not be empty" }),
11
- });
12
-
13
- export const arrayConfigScheme = z.object({
14
- type: z.string().regex(/^arr$/, { message: "invalid type string" }),
15
- item: z.object({}),
16
- len: z.number().nonnegative(),
17
- });
18
-
19
- export const tupleConfigScheme = z.object({
20
- type: z.string().regex(/^tuple$/, { message: "invalid type string" }),
21
- configItems: z.any().array(),
22
- });
23
-
24
- export const objConfigScheme = z.object({
25
- type: z.string().regex(/^obj$/, { message: "invalid type string" }),
26
- content: z.object({}),
27
- });
28
-
29
- export const boundedSeriesScheme = z
30
- .object({
31
- type: z
32
- .string()
33
- .regex(/^bounded_series$/, { message: "invalid type string" }),
34
- upperLimit: z.number().nonnegative(),
35
- lowerLimit: z.number().nonnegative(),
36
- createInitValue: z.function().args().returns(z.number()),
37
- count: z.number().nonnegative(),
38
- })
39
- .refine(({ upperLimit, lowerLimit }) => upperLimit >= lowerLimit, {
40
- message: "lowerLimit can not greater then upperLimit",
41
- })
42
- .refine(({ createInitValue }) => typeof createInitValue() === "number", {
43
- message: "createInitValue is not return number",
44
- path: ["createInitValue"],
45
- });