-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomObject.ts
27 lines (24 loc) · 947 Bytes
/
randomObject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import MockFactory from "@/mocking/mock.factory";
import RandomNumber from "@/randomizers/randomNumber";
import RandomString from "@/randomizers/randomString";
import RandomBoolean from "@/randomizers/randomBoolean";
export default new MockFactory<object>(
({ options }: { options: { propCount?: number } } = { options: {} }) => {
const { propCount = 5 } = options;
return Array.from(new Array(propCount)).reduce((obj, _, i) => {
const fieldName = `field${i}_${RandomBoolean.getOne()}`;
const fieldType = [RandomString, RandomNumber, RandomBoolean][
RandomNumber.getOne({ options: { min: 0, max: 2 } })
];
const isArray = RandomBoolean.getOne();
obj[fieldName] = isArray
? fieldType.getArray({
options: {
array: RandomNumber.getOne({ options: { min: 0, max: 20 } }),
},
})
: fieldType.getOne();
return obj;
}, {});
}
);