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.
- package/dist/index.d.mts +87 -0
- package/dist/index.d.ts +87 -0
- package/dist/index.js +5820 -0
- package/dist/index.mjs +5782 -0
- package/package.json +12 -17
- package/src/{create_config.test.mjs → create_config.test.ts} +2 -3
- package/src/create_config.ts +131 -0
- package/src/{create_generator_fn.test.mjs → create_generator_fn.test.ts} +26 -30
- package/src/create_generator_fn.ts +160 -0
- package/src/index.ts +3 -0
- package/src/type.ts +69 -0
- package/tsconfig.json +16 -0
- package/cjs/config_scheme.js +0 -61
- package/cjs/create_config.js +0 -98
- package/cjs/create_config.test.js +0 -69
- package/cjs/create_generator_fn.js +0 -161
- package/cjs/create_generator_fn.test.js +0 -244
- package/cjs/index.js +0 -27
- package/cjs/type.js +0 -24
- package/esm/create_config.d.ts +0 -96
- package/esm/create_config.mjs +0 -97
- package/esm/create_config.test.mjs +0 -72
- package/esm/create_generator_fn.d.ts +0 -35
- package/esm/create_generator_fn.mjs +0 -140
- package/esm/create_generator_fn.test.mjs +0 -192
- package/esm/index.mjs +0 -2
- package/esm/type.mjs +0 -23
- package/jsconfig.json +0 -20
- package/src/config_scheme.mjs +0 -45
- package/src/create_config.d.ts +0 -96
- package/src/create_config.mjs +0 -97
- package/src/create_generator_fn.d.ts +0 -35
- package/src/create_generator_fn.mjs +0 -140
- package/src/index.mjs +0 -2
- package/src/type.mjs +0 -23
- /package/{esm/config_scheme.mjs → src/config_scheme.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,34 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "struct-fakerator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "
|
|
6
|
-
"module": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
12
13
|
},
|
|
13
|
-
"type": "module",
|
|
14
14
|
"scripts": {
|
|
15
|
-
"
|
|
16
|
-
"dev": "node",
|
|
17
|
-
"build:cjs": "babel src --out-dir cjs --presets=@babel/preset-env --plugins=@babel/plugin-transform-modules-commonjs",
|
|
18
|
-
"build:esm": "cp -r src/* esm",
|
|
19
|
-
"build": "pnpm build:cjs && pnpm build:esm",
|
|
15
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
20
16
|
"test": "vitest src"
|
|
21
17
|
},
|
|
22
18
|
"keywords": [],
|
|
23
19
|
"author": "FizzyElt",
|
|
24
|
-
"homepage": "https://github.com/FizzyElt/
|
|
20
|
+
"homepage": "https://github.com/FizzyElt/fakerator",
|
|
25
21
|
"license": "ISC",
|
|
26
22
|
"devDependencies": {
|
|
27
|
-
"@babel/cli": "^7.24.7",
|
|
28
|
-
"@babel/core": "^7.24.7",
|
|
29
|
-
"@babel/preset-env": "^7.24.7",
|
|
30
23
|
"@biomejs/biome": "^1.8.3",
|
|
31
24
|
"@faker-js/faker": "^8.4.1",
|
|
25
|
+
"tsup": "^8.1.0",
|
|
26
|
+
"typescript": "^5.5.3",
|
|
32
27
|
"vitest": "^1.6.0"
|
|
33
28
|
},
|
|
34
29
|
"dependencies": {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { expect,
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
createValueConfig,
|
|
@@ -7,8 +7,7 @@ import {
|
|
|
7
7
|
createTupleConfig,
|
|
8
8
|
createObjectConfig,
|
|
9
9
|
createBoundedSeriesConfig,
|
|
10
|
-
} from "./create_config
|
|
11
|
-
import { createGeneratorByType } from "./create_generator_fn.mjs";
|
|
10
|
+
} from "./create_config";
|
|
12
11
|
|
|
13
12
|
test("createValueConfig", () => {
|
|
14
13
|
const valueConfig = createValueConfig(() => 44);
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
valueConfigScheme,
|
|
3
|
+
arrayConfigScheme,
|
|
4
|
+
selectionConfigScheme,
|
|
5
|
+
tupleConfigScheme,
|
|
6
|
+
objConfigScheme,
|
|
7
|
+
boundedSeriesScheme,
|
|
8
|
+
} from "./config_scheme";
|
|
9
|
+
import type {
|
|
10
|
+
ValueConfig,
|
|
11
|
+
SelectionConfig,
|
|
12
|
+
ObjectConfig,
|
|
13
|
+
ArrayConfig,
|
|
14
|
+
TupleConfig,
|
|
15
|
+
BoundedSeriesConfig,
|
|
16
|
+
} from "./type";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* value
|
|
20
|
+
* @param {function} generateFn - The function used to generate the value.
|
|
21
|
+
* @return {ValueConfig} The configuration object with the type "value" and the provided generate function.
|
|
22
|
+
*/
|
|
23
|
+
export const createValueConfig = <T>(generateFn: () => T): ValueConfig<T> => {
|
|
24
|
+
const config: ValueConfig<T> = {
|
|
25
|
+
type: "value",
|
|
26
|
+
generateFn,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
valueConfigScheme.parse(config);
|
|
30
|
+
|
|
31
|
+
return config;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* selection
|
|
36
|
+
* @param {Array} items - The array of items to choose from.
|
|
37
|
+
* @return {SelectionConfig} The configuration object with the type "select" and the provided items.
|
|
38
|
+
*/
|
|
39
|
+
export const createSelectionConfig = <T>(items: T[]): SelectionConfig<T> => {
|
|
40
|
+
const config: SelectionConfig<T> = { type: "select", items };
|
|
41
|
+
|
|
42
|
+
selectionConfigScheme.parse(config);
|
|
43
|
+
|
|
44
|
+
return config;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* object
|
|
49
|
+
* @param {object} content
|
|
50
|
+
* @return {ObjectConfig}
|
|
51
|
+
*/
|
|
52
|
+
export const createObjectConfig = <T extends object>(
|
|
53
|
+
content: T,
|
|
54
|
+
): ObjectConfig<T> => {
|
|
55
|
+
const config: ObjectConfig<T> = { type: "obj", content };
|
|
56
|
+
|
|
57
|
+
objConfigScheme.parse(config);
|
|
58
|
+
|
|
59
|
+
return config;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* array
|
|
64
|
+
* @param {object} item
|
|
65
|
+
* @param {number} len
|
|
66
|
+
* @return {ArrayConfig}
|
|
67
|
+
*/
|
|
68
|
+
export const createArrayConfig = <T>(item: T, len: number): ArrayConfig<T> => {
|
|
69
|
+
const config: ArrayConfig<T> = { type: "arr", item, len };
|
|
70
|
+
|
|
71
|
+
arrayConfigScheme.parse(config);
|
|
72
|
+
|
|
73
|
+
return config;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* tuple
|
|
78
|
+
* @param {Array} configItems
|
|
79
|
+
* @return {TupleConfig}
|
|
80
|
+
*/
|
|
81
|
+
interface CreateTupleConfig {
|
|
82
|
+
<A, B, C, D, E>(configItems: [A, B, C, D, E]): TupleConfig<A, B, C, D, E>;
|
|
83
|
+
<A, B, C, D>(configItems: [A, B, C, D]): TupleConfig<A, B, C, D>;
|
|
84
|
+
<A, B, C>(configItems: [A, B, C]): TupleConfig<A, B, C>;
|
|
85
|
+
<A, B>(configItems: [A, B]): TupleConfig<A, B>;
|
|
86
|
+
<A>(configItems: [A]): TupleConfig<A>;
|
|
87
|
+
}
|
|
88
|
+
export const createTupleConfig: CreateTupleConfig = <
|
|
89
|
+
A,
|
|
90
|
+
B = undefined,
|
|
91
|
+
C = undefined,
|
|
92
|
+
D = undefined,
|
|
93
|
+
E = undefined,
|
|
94
|
+
>(
|
|
95
|
+
configItems: E extends undefined
|
|
96
|
+
? D extends undefined
|
|
97
|
+
? C extends undefined
|
|
98
|
+
? B extends undefined
|
|
99
|
+
? [A]
|
|
100
|
+
: [A, B]
|
|
101
|
+
: [A, B, C]
|
|
102
|
+
: [A, B, C, D]
|
|
103
|
+
: [A, B, C, D, E],
|
|
104
|
+
) => {
|
|
105
|
+
const config: TupleConfig<A, B, C, D, E> = {
|
|
106
|
+
type: "tuple",
|
|
107
|
+
configItems,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
tupleConfigScheme.parse(config);
|
|
111
|
+
|
|
112
|
+
return config;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* bounded series
|
|
117
|
+
* @param {{ upperLimit: number, lowerLimit: number, createInitValue: () => number, count: number }} config
|
|
118
|
+
* @return {BoundedSeriesConfig}
|
|
119
|
+
*/
|
|
120
|
+
export const createBoundedSeriesConfig = (
|
|
121
|
+
config: Omit<BoundedSeriesConfig, "type">,
|
|
122
|
+
): BoundedSeriesConfig => {
|
|
123
|
+
const newConfig: BoundedSeriesConfig = {
|
|
124
|
+
type: "bounded_series",
|
|
125
|
+
...config,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
boundedSeriesScheme.parse(newConfig);
|
|
129
|
+
|
|
130
|
+
return newConfig;
|
|
131
|
+
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { describe, test, expect } from "vitest";
|
|
2
|
-
import { ZodError } from "zod";
|
|
3
2
|
import {
|
|
4
3
|
createValueGenerator,
|
|
5
4
|
createSelectionGenerator,
|
|
@@ -8,8 +7,13 @@ import {
|
|
|
8
7
|
createObjectGenerator,
|
|
9
8
|
createBoundedSeriesGenerator,
|
|
10
9
|
createGeneratorByType,
|
|
11
|
-
} from "./create_generator_fn
|
|
12
|
-
import {
|
|
10
|
+
} from "./create_generator_fn";
|
|
11
|
+
import {
|
|
12
|
+
createValueConfig,
|
|
13
|
+
createTupleConfig,
|
|
14
|
+
createObjectConfig,
|
|
15
|
+
createArrayConfig,
|
|
16
|
+
} from "./create_config";
|
|
13
17
|
|
|
14
18
|
describe("createValueGenerator", () => {
|
|
15
19
|
test("normal", () => {
|
|
@@ -67,13 +71,12 @@ describe("createArrayGenerator", () => {
|
|
|
67
71
|
|
|
68
72
|
describe("createTupleGenerator", () => {
|
|
69
73
|
test("normal", () => {
|
|
70
|
-
const tuple = createTupleGenerator(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
})();
|
|
74
|
+
const tuple = createTupleGenerator(
|
|
75
|
+
createTupleConfig([
|
|
76
|
+
createValueConfig(() => 225),
|
|
77
|
+
createValueConfig(() => "hello world"),
|
|
78
|
+
]),
|
|
79
|
+
)();
|
|
77
80
|
|
|
78
81
|
expect(tuple.length).toBe(2);
|
|
79
82
|
const [num, str] = tuple;
|
|
@@ -135,18 +138,14 @@ describe("createBoundedSeriesGenerator", () => {
|
|
|
135
138
|
|
|
136
139
|
describe("createGeneratorByType", () => {
|
|
137
140
|
test("normal", () => {
|
|
138
|
-
const config = {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
len: 5,
|
|
147
|
-
},
|
|
148
|
-
},
|
|
149
|
-
};
|
|
141
|
+
const config = createObjectConfig({
|
|
142
|
+
name: createValueConfig(() => "John"),
|
|
143
|
+
age: createValueConfig(() => 50),
|
|
144
|
+
locations: createArrayConfig(
|
|
145
|
+
createValueConfig(() => "Taiwan"),
|
|
146
|
+
5,
|
|
147
|
+
),
|
|
148
|
+
});
|
|
150
149
|
const result = createGeneratorByType(config)();
|
|
151
150
|
|
|
152
151
|
expect(result).toEqual({
|
|
@@ -172,14 +171,11 @@ describe("createGeneratorByType", () => {
|
|
|
172
171
|
throw Error("error");
|
|
173
172
|
};
|
|
174
173
|
|
|
175
|
-
const config = {
|
|
176
|
-
type: "
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
email: { type: "email" },
|
|
181
|
-
},
|
|
182
|
-
};
|
|
174
|
+
const config = createObjectConfig({
|
|
175
|
+
name: { type: "value", generateFn: () => "John" },
|
|
176
|
+
age: { type: "int" },
|
|
177
|
+
email: { type: "email" },
|
|
178
|
+
});
|
|
183
179
|
|
|
184
180
|
const result = createGeneratorByType(config, customTypeMatch)();
|
|
185
181
|
|
|
@@ -0,0 +1,160 @@
|
|
|
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";
|
|
11
|
+
import type {
|
|
12
|
+
ValueConfig,
|
|
13
|
+
SelectionConfig,
|
|
14
|
+
ObjectConfig,
|
|
15
|
+
TupleConfig,
|
|
16
|
+
ArrayConfig,
|
|
17
|
+
Result,
|
|
18
|
+
BoundedSeriesConfig,
|
|
19
|
+
} from "./type";
|
|
20
|
+
import { createObjectConfig, createValueConfig } from "./create_config";
|
|
21
|
+
|
|
22
|
+
type AllConfig<T> =
|
|
23
|
+
| ValueConfig<T>
|
|
24
|
+
| SelectionConfig<T>
|
|
25
|
+
| ArrayConfig<T>
|
|
26
|
+
| ObjectConfig<T>
|
|
27
|
+
| TupleConfig<T>
|
|
28
|
+
| TupleConfig<T, T>
|
|
29
|
+
| TupleConfig<T, T, T>
|
|
30
|
+
| TupleConfig<T, T, T, T>
|
|
31
|
+
| TupleConfig<T, T, T, T, T>
|
|
32
|
+
| BoundedSeriesConfig;
|
|
33
|
+
|
|
34
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
35
|
+
export const createValueGenerator = <R = unknown>(config: any): (() => R) => {
|
|
36
|
+
valueConfigScheme.parse(config);
|
|
37
|
+
|
|
38
|
+
return config.generateFn as () => R;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const createSelectionGenerator = <T extends SelectionConfig<unknown>>(
|
|
42
|
+
config: T,
|
|
43
|
+
): (() => Result<T>) => {
|
|
44
|
+
selectionConfigScheme.parse(config);
|
|
45
|
+
|
|
46
|
+
const { items } = config;
|
|
47
|
+
|
|
48
|
+
return (() => items[faker.number.int(items.length - 1)]) as () => Result<T>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const createObjectGenerator = <T extends ObjectConfig<unknown>>(
|
|
52
|
+
config: T,
|
|
53
|
+
customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
|
|
54
|
+
): (() => Result<T>) => {
|
|
55
|
+
objConfigScheme.parse(config);
|
|
56
|
+
|
|
57
|
+
const keyWithFns: [string, () => Result<AllConfig<unknown>>][] =
|
|
58
|
+
Object.entries(config.content as object).map(([key, subConfig]) => [
|
|
59
|
+
key,
|
|
60
|
+
createGeneratorByType(subConfig, customTypeMatch),
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
return () => {
|
|
64
|
+
const result: Record<string, unknown> = {};
|
|
65
|
+
for (const [key, generateFn] of keyWithFns) {
|
|
66
|
+
result[key] = generateFn();
|
|
67
|
+
}
|
|
68
|
+
return result as Result<T>;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const createArrayGenerator = <T extends ArrayConfig<unknown>>(
|
|
73
|
+
config: T,
|
|
74
|
+
customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
|
|
75
|
+
): (() => Result<T>) => {
|
|
76
|
+
arrayConfigScheme.parse(config);
|
|
77
|
+
|
|
78
|
+
const itemGeneratorFn = createGeneratorByType(
|
|
79
|
+
config.item as AllConfig<unknown>,
|
|
80
|
+
customTypeMatch,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
return () =>
|
|
84
|
+
Array.from({ length: config.len ?? 0 }, itemGeneratorFn) as Result<T>;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const createTupleGenerator = <
|
|
88
|
+
T extends
|
|
89
|
+
| TupleConfig<unknown, unknown, unknown, unknown, unknown>
|
|
90
|
+
| TupleConfig<unknown, unknown, unknown, unknown>
|
|
91
|
+
| TupleConfig<unknown, unknown, unknown>
|
|
92
|
+
| TupleConfig<unknown, unknown>
|
|
93
|
+
| TupleConfig<unknown>,
|
|
94
|
+
>(
|
|
95
|
+
config: T,
|
|
96
|
+
customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
|
|
97
|
+
): (() => Result<T>) => {
|
|
98
|
+
tupleConfigScheme.parse(config);
|
|
99
|
+
|
|
100
|
+
const itemsFns = config.configItems.map((configItem) =>
|
|
101
|
+
createGeneratorByType(configItem as AllConfig<unknown>, customTypeMatch),
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
return () => itemsFns.map((generateFn) => generateFn()) as Result<T>;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const createBoundedSeriesGenerator = <T extends BoundedSeriesConfig>(
|
|
108
|
+
config: T,
|
|
109
|
+
): (() => Result<T>) => {
|
|
110
|
+
boundedSeriesScheme.parse(config);
|
|
111
|
+
|
|
112
|
+
const { upperLimit, lowerLimit, createInitValue, count } = config;
|
|
113
|
+
|
|
114
|
+
return () => {
|
|
115
|
+
let value = createInitValue();
|
|
116
|
+
|
|
117
|
+
const boundedSeries = [];
|
|
118
|
+
|
|
119
|
+
for (let i = 0; i < count; i++) {
|
|
120
|
+
value = faker.number.float({ max: upperLimit, min: lowerLimit }) * value;
|
|
121
|
+
boundedSeries.push(value);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return boundedSeries as Result<T>;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export const createGeneratorByType = <T extends AllConfig<unknown>>(
|
|
129
|
+
config: T,
|
|
130
|
+
customTypeMatch?: (config: unknown) => ValueConfig<unknown>,
|
|
131
|
+
): (() => Result<T>) => {
|
|
132
|
+
switch (config.type) {
|
|
133
|
+
case "obj":
|
|
134
|
+
return createObjectGenerator(config, customTypeMatch) as () => Result<T>;
|
|
135
|
+
case "arr":
|
|
136
|
+
return createArrayGenerator(config, customTypeMatch) as () => Result<T>;
|
|
137
|
+
case "tuple":
|
|
138
|
+
return createTupleGenerator(config, customTypeMatch) as () => Result<T>;
|
|
139
|
+
case "select":
|
|
140
|
+
return createSelectionGenerator(config) as () => Result<T>;
|
|
141
|
+
case "value":
|
|
142
|
+
return createValueGenerator(config);
|
|
143
|
+
case "bounded_series":
|
|
144
|
+
return createBoundedSeriesGenerator(config) as () => Result<T>;
|
|
145
|
+
default: {
|
|
146
|
+
if (customTypeMatch) {
|
|
147
|
+
return createValueGenerator(customTypeMatch(config));
|
|
148
|
+
}
|
|
149
|
+
throw Error(`config type "${config}" is not supported`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const config = createObjectConfig({
|
|
155
|
+
a: createObjectConfig({
|
|
156
|
+
b: createValueConfig(() => 1),
|
|
157
|
+
}),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const fn = createGeneratorByType(config);
|
package/src/index.ts
ADDED
package/src/type.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type ValueConfig<T> = {
|
|
2
|
+
type: "value";
|
|
3
|
+
generateFn: () => T;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type SelectionConfig<T> = {
|
|
7
|
+
type: "select";
|
|
8
|
+
items: T[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type BoundedSeriesConfig = {
|
|
12
|
+
type: "bounded_series";
|
|
13
|
+
upperLimit: number;
|
|
14
|
+
lowerLimit: number;
|
|
15
|
+
createInitValue: () => number;
|
|
16
|
+
count: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type ArrayConfig<T> = {
|
|
20
|
+
type: "arr";
|
|
21
|
+
item: T;
|
|
22
|
+
len: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type ObjectConfig<T> = {
|
|
26
|
+
type: "obj";
|
|
27
|
+
content: T;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type TupleConfig<
|
|
31
|
+
A,
|
|
32
|
+
B = undefined,
|
|
33
|
+
C = undefined,
|
|
34
|
+
D = undefined,
|
|
35
|
+
E = undefined,
|
|
36
|
+
> = {
|
|
37
|
+
type: "tuple";
|
|
38
|
+
configItems: E extends undefined
|
|
39
|
+
? D extends undefined
|
|
40
|
+
? C extends undefined
|
|
41
|
+
? B extends undefined
|
|
42
|
+
? [A]
|
|
43
|
+
: [A, B]
|
|
44
|
+
: [A, B, C]
|
|
45
|
+
: [A, B, C, D]
|
|
46
|
+
: [A, B, C, D, E];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type Result<T> = T extends ValueConfig<infer U>
|
|
50
|
+
? U
|
|
51
|
+
: T extends SelectionConfig<infer S>
|
|
52
|
+
? S
|
|
53
|
+
: T extends BoundedSeriesConfig
|
|
54
|
+
? number[]
|
|
55
|
+
: T extends ArrayConfig<infer W>
|
|
56
|
+
? Array<Result<W>>
|
|
57
|
+
: T extends ObjectConfig<infer O>
|
|
58
|
+
? { [K in keyof O]: Result<O[K]> }
|
|
59
|
+
: T extends TupleConfig<infer A, infer B, infer C, infer D, infer E>
|
|
60
|
+
? E extends undefined
|
|
61
|
+
? D extends undefined
|
|
62
|
+
? C extends undefined
|
|
63
|
+
? B extends undefined
|
|
64
|
+
? [Result<A>]
|
|
65
|
+
: [Result<A>, Result<B>]
|
|
66
|
+
: [Result<A>, Result<B>, Result<C>]
|
|
67
|
+
: [Result<A>, Result<B>, Result<C>, Result<D>]
|
|
68
|
+
: [Result<A>, Result<B>, Result<C>, Result<D>, Result<E>]
|
|
69
|
+
: never;
|
package/tsconfig.json
ADDED
package/cjs/config_scheme.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.valueConfigScheme = exports.tupleConfigScheme = exports.selectionConfigScheme = exports.objConfigScheme = exports.boundedSeriesScheme = exports.arrayConfigScheme = void 0;
|
|
7
|
-
var _zod = require("zod");
|
|
8
|
-
var valueConfigScheme = exports.valueConfigScheme = _zod.z.object({
|
|
9
|
-
type: _zod.z.string().regex(/^value$/, {
|
|
10
|
-
message: "invalid type string"
|
|
11
|
-
}),
|
|
12
|
-
generateFn: _zod.z["function"]()
|
|
13
|
-
});
|
|
14
|
-
var selectionConfigScheme = exports.selectionConfigScheme = _zod.z.object({
|
|
15
|
-
type: _zod.z.string().regex(/^select$/, {
|
|
16
|
-
message: "invalid type string"
|
|
17
|
-
}),
|
|
18
|
-
items: _zod.z.any().array().nonempty({
|
|
19
|
-
message: "items can not be empty"
|
|
20
|
-
})
|
|
21
|
-
});
|
|
22
|
-
var arrayConfigScheme = exports.arrayConfigScheme = _zod.z.object({
|
|
23
|
-
type: _zod.z.string().regex(/^arr$/, {
|
|
24
|
-
message: "invalid type string"
|
|
25
|
-
}),
|
|
26
|
-
item: _zod.z.object({}),
|
|
27
|
-
len: _zod.z.number().nonnegative()
|
|
28
|
-
});
|
|
29
|
-
var tupleConfigScheme = exports.tupleConfigScheme = _zod.z.object({
|
|
30
|
-
type: _zod.z.string().regex(/^tuple$/, {
|
|
31
|
-
message: "invalid type string"
|
|
32
|
-
}),
|
|
33
|
-
configItems: _zod.z.any().array()
|
|
34
|
-
});
|
|
35
|
-
var objConfigScheme = exports.objConfigScheme = _zod.z.object({
|
|
36
|
-
type: _zod.z.string().regex(/^obj$/, {
|
|
37
|
-
message: "invalid type string"
|
|
38
|
-
}),
|
|
39
|
-
content: _zod.z.object({})
|
|
40
|
-
});
|
|
41
|
-
var boundedSeriesScheme = exports.boundedSeriesScheme = _zod.z.object({
|
|
42
|
-
type: _zod.z.string().regex(/^bounded_series$/, {
|
|
43
|
-
message: "invalid type string"
|
|
44
|
-
}),
|
|
45
|
-
upperLimit: _zod.z.number().nonnegative(),
|
|
46
|
-
lowerLimit: _zod.z.number().nonnegative(),
|
|
47
|
-
createInitValue: _zod.z["function"]().args().returns(_zod.z.number()),
|
|
48
|
-
count: _zod.z.number().nonnegative()
|
|
49
|
-
}).refine(function (_ref) {
|
|
50
|
-
var upperLimit = _ref.upperLimit,
|
|
51
|
-
lowerLimit = _ref.lowerLimit;
|
|
52
|
-
return upperLimit >= lowerLimit;
|
|
53
|
-
}, {
|
|
54
|
-
message: "lowerLimit can not greater then upperLimit"
|
|
55
|
-
}).refine(function (_ref2) {
|
|
56
|
-
var createInitValue = _ref2.createInitValue;
|
|
57
|
-
return typeof createInitValue() === "number";
|
|
58
|
-
}, {
|
|
59
|
-
message: "createInitValue is not return number",
|
|
60
|
-
path: ["createInitValue"]
|
|
61
|
-
});
|
package/cjs/create_config.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createValueConfig = exports.createTupleConfig = exports.createSelectionConfig = exports.createObjectConfig = exports.createBoundedSeriesConfig = exports.createArrayConfig = void 0;
|
|
7
|
-
var _faker = require("@faker-js/faker");
|
|
8
|
-
var _config_scheme = require("./config_scheme.mjs");
|
|
9
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
10
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
11
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
12
|
-
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
13
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
14
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
15
|
-
/**
|
|
16
|
-
* value
|
|
17
|
-
* @param {function} generateFn - The function used to generate the value.
|
|
18
|
-
* @return {ValueConfig} The configuration object with the type "value" and the provided generate function.
|
|
19
|
-
*/
|
|
20
|
-
var createValueConfig = exports.createValueConfig = function createValueConfig(generateFn) {
|
|
21
|
-
var config = {
|
|
22
|
-
type: "value",
|
|
23
|
-
generateFn: generateFn
|
|
24
|
-
};
|
|
25
|
-
_config_scheme.valueConfigScheme.parse(config);
|
|
26
|
-
return config;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* selection
|
|
31
|
-
* @param {Array} items - The array of items to choose from.
|
|
32
|
-
* @return {SelectionConfig} The configuration object with the type "select" and the provided items.
|
|
33
|
-
*/
|
|
34
|
-
var createSelectionConfig = exports.createSelectionConfig = function createSelectionConfig(items) {
|
|
35
|
-
var config = {
|
|
36
|
-
type: "select",
|
|
37
|
-
items: items
|
|
38
|
-
};
|
|
39
|
-
_config_scheme.selectionConfigScheme.parse(config);
|
|
40
|
-
return config;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* object
|
|
45
|
-
* @param {object} content
|
|
46
|
-
* @return {ObjectConfig}
|
|
47
|
-
*/
|
|
48
|
-
var createObjectConfig = exports.createObjectConfig = function createObjectConfig(content) {
|
|
49
|
-
var config = {
|
|
50
|
-
type: "obj",
|
|
51
|
-
content: content
|
|
52
|
-
};
|
|
53
|
-
_config_scheme.objConfigScheme.parse(config);
|
|
54
|
-
return config;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* array
|
|
59
|
-
* @param {object} item
|
|
60
|
-
* @param {number} len
|
|
61
|
-
* @return {ArrayConfig}
|
|
62
|
-
*/
|
|
63
|
-
var createArrayConfig = exports.createArrayConfig = function createArrayConfig(item, len) {
|
|
64
|
-
var config = {
|
|
65
|
-
type: "arr",
|
|
66
|
-
item: item,
|
|
67
|
-
len: len
|
|
68
|
-
};
|
|
69
|
-
_config_scheme.arrayConfigScheme.parse(config);
|
|
70
|
-
return config;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* tuple
|
|
75
|
-
* @param {Array} configItems
|
|
76
|
-
* @return {TupleConfig}
|
|
77
|
-
*/
|
|
78
|
-
var createTupleConfig = exports.createTupleConfig = function createTupleConfig(configItems) {
|
|
79
|
-
var config = {
|
|
80
|
-
type: "tuple",
|
|
81
|
-
configItems: configItems
|
|
82
|
-
};
|
|
83
|
-
_config_scheme.tupleConfigScheme.parse(config);
|
|
84
|
-
return config;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* bounded series
|
|
89
|
-
* @param {{ upperLimit: number, lowerLimit: number, createInitValue: () => number, count: number }} config
|
|
90
|
-
* @return {BoundedSeriesConfig}
|
|
91
|
-
*/
|
|
92
|
-
var createBoundedSeriesConfig = exports.createBoundedSeriesConfig = function createBoundedSeriesConfig(config) {
|
|
93
|
-
var newConfig = _objectSpread({
|
|
94
|
-
type: "bounded_series"
|
|
95
|
-
}, config);
|
|
96
|
-
_config_scheme.boundedSeriesScheme.parse(newConfig);
|
|
97
|
-
return newConfig;
|
|
98
|
-
};
|