-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalstorage.js
42 lines (38 loc) · 968 Bytes
/
localstorage.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
class Storage{
constructor(user = 'nparoski'){
this.user = user;
}
store(user){
const regexp = /^(?=.{1,39}$)[a-zA-Z\d]+(?:-[a-zA-Z\d]+)*$/;
if (!regexp.test(user)) {
throw new Error('Cannot store username: Invalid username');
}else{
this.user = user;
localStorage.setItem('user', this.user);
}
}
load(){
// return localStorage.getItem('user') === null ? this.user : localStorage.getItem('user');
if(localStorage.getItem('user') !== null){
return {
user:localStorage.getItem('user'),
msg:'Success: Loaded user from storage.'
}
}
else{
return{
user: this.user,
msg:'Success: Loaded default user from Storage constructor.'
}
}
}
clear(){
if(localStorage.getItem('user') !== null){
localStorage.clear();
Ui.createSuccess('Success: Cleared storage.');
}
else{
Ui.createWarning('Storage is empty.');
}
}
}