-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
44 lines (37 loc) · 1.08 KB
/
model.js
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
export class RandomString {
constructor() {
this.value = this.generateRandomString();
this.id = Math.random().toString(36).substr(2, 9); // Unique ID
}
generateRandomString(length = 8) {
let result = '';
for (let i = 0; i < length; i++) {
const charCode = Math.floor(Math.random() * (122 - 97 + 1)) + 97; // a-z
result += String.fromCharCode(charCode);
}
return result;
}
}
export class RandomStringList {
constructor() {
this.strings = [];
this.deletedStrings = [];
}
addString() {
const randomString = new RandomString();
this.strings.push(randomString);
}
deleteRandomString() {
if (this.strings.length === 0) return null;
const index = Math.floor(Math.random() * this.strings.length);
const deletedString = this.strings.splice(index, 1)[0];
this.deletedStrings.push(deletedString);
return deletedString;
}
getStrings() {
return this.strings;
}
getDeletedStrings() {
return this.deletedStrings;
}
}