-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
core.ts
65 lines (55 loc) 路 1.53 KB
/
core.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { random } from '../random';
import { isString } from '../utils/validators.utils';
export interface FakeOptions {
length?: number;
locale?: any | string[];
maxCharCount?: number;
}
export type markRequired<Type, Key extends keyof Type> = Type & {
[Property in Key]-?: Type[Property];
};
export type Return<T, O extends FakeOptions> = [O] extends [never]
? T
: O['length'] extends number
? T[]
: T;
type FactoryFunction<T> = (i: number) => T;
export function fake<T, Options extends FakeOptions>(
data: Readonly<T[]> | FactoryFunction<T>,
options?: Options
): Return<T, Options> {
let dataSource = data as FactoryFunction<T>;
if (Array.isArray(data)) {
let resolvedData = data;
if (options?.maxCharCount && isString(data[0])) {
resolvedData = data.filter(
(item) => item.length <= options.maxCharCount!
);
}
dataSource = () => randElement(resolvedData);
}
if (options?.length === undefined) {
return dataSource(0) as any;
}
return Array.from({ length: options.length }, (_, index) =>
dataSource(index)
) as any;
}
export function randElement<T>(arr: Readonly<T[]>) {
return arr[Math.floor(random() * arr.length)];
}
export interface RandomInRangeOptions {
min?: number;
max?: number;
fraction?: number;
}
export function getRandomInRange({
min = 1.0,
max = 9999.99,
fraction = 0,
}: RandomInRangeOptions = {}) {
if (max < min) {
throw new Error('Max must be bigger than min');
}
return Number((random() * (max - min) + min).toFixed(fraction));
}