-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGridLiteDataService.ts
More file actions
136 lines (119 loc) · 4.24 KB
/
GridLiteDataService.ts
File metadata and controls
136 lines (119 loc) · 4.24 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
export type UserSimple = {
id: string;
username: string;
email: string;
subscribed: boolean;
};
export type ProductInfo = {
id: string;
name: string;
price: number;
sold: number;
rating: number;
total: number;
};
export type User = {
id: string;
firstName: string;
lastName: string;
age: number;
email: string;
avatar: string;
active: boolean;
priority: 'Low' | 'Standard' | 'High';
satisfaction: number;
registeredAt: Date;
};
export class GridLiteDataService {
private counter = 0;
private namesMen = ['John', 'Bob', 'Mark', 'Charlie', 'Martin', 'Bill', 'Frank', 'Larry', 'Henry', 'Steve', 'Mike', 'Andrew'];
private namesWomen = ['Jane', 'Alice', 'Diana', 'Eve', 'Grace' , 'Katie', 'Irene', 'Liz', 'Fiona', 'Pam', 'Val', 'Mindy'];
private lastNames = ['Smith', 'Johnson', 'Mendoza', 'Brown', 'Spencer', 'Stone', 'Stark', 'Rooney'];
private productNames = ['Widget', 'Gadget', 'Gizmo', 'Device', 'Tool', 'Instrument', 'Machine', 'Equipment'];
private productModels = ['Pro', 'Plus', 'Max', 'Ultra', 'Mini', 'Lite'];
private priorities: ('Low' | 'Standard' | 'High')[] = ['Low', 'Standard', 'High'];
private randomInt(min: number, max: number): number {
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
const random01 = array[0] / 2 ** 32;
return Math.floor(random01 * (max - min + 1)) + min;
}
private randomFloat(min: number, max: number, precision = 2): number {
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
const random01 = array[0] / 2 ** 32;
return parseFloat((random01 * (max - min) + min).toFixed(precision));
}
private randomElement<T>(array: T[]): T {
return array[this.randomInt(0, array.length - 1)];
}
private randomBoolean(): boolean {
const array = new Uint8Array(1);
window.crypto.getRandomValues(array);
return (array[0] & 1) === 0;
}
private generateId(): string {
return `1000-${this.counter++}-${this.randomInt(1000, 9999)}`;
}
createProductInfo(): ProductInfo {
const price = this.randomFloat(50, 500, 2);
const sold = this.randomInt(10, 100);
const total = parseFloat((price * sold).toFixed(2));
const product = this.randomElement(this.productNames) + ' ' + this.randomElement(this.productModels);
return {
price,
sold,
total,
id: this.generateId(),
name: product,
rating: this.randomFloat(0, 5, 1)
};
}
createUserSimple(): UserSimple {
const firstName = this.randomElement(this.namesMen.concat(this.namesWomen)).toLowerCase();
const lastName = this.randomElement(this.lastNames).toLowerCase();
const email = firstName + '.' + lastName + '@example.com';
const username = firstName + '.' + lastName + this.randomInt(1, 99);
return {
id: this.generateId(),
username: username,
email: email,
subscribed: this.randomBoolean()
};
}
createUser(): User {
let imagePath: string = "";
let firstName: string = "";
const gender = this.randomInt(0, 1);
if (gender === 0) {
imagePath = "https://dl.infragistics.com/x/img/people/men/" + this.randomInt(10, 40) + ".png";
firstName = this.randomElement(this.namesMen);
} else {
imagePath = "https://dl.infragistics.com/x/img/people/women/" + this.randomInt(10, 40) + ".png";
firstName = this.randomElement(this.namesWomen);
}
const lastName = this.randomElement(this.lastNames);
const email = firstName.toLowerCase() + '.' + lastName.toLowerCase() + '@example.com';
return {
id: this.generateId(),
firstName,
lastName,
age: this.randomInt(18, 90),
email,
avatar: imagePath,
active: this.randomBoolean(),
priority: this.randomElement(this.priorities),
satisfaction: this.randomInt(0, 5),
registeredAt: new Date(Date.now() - this.randomInt(0, 365 * 24 * 60 * 60 * 1000))
};
}
generateUsers(count: number): User[] {
return Array.from({ length: count }, () => this.createUser());
}
generateProducts(count: number): ProductInfo[] {
return Array.from({ length: count }, () => this.createProductInfo());
}
generateSimpleUsers(count: number): UserSimple[] {
return Array.from({ length: count }, () => this.createUserSimple());
}
}