-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanonymizer.js
More file actions
91 lines (77 loc) · 2.77 KB
/
anonymizer.js
File metadata and controls
91 lines (77 loc) · 2.77 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
let Account = (function() {
function isValidPassword(userPassword, testPassword) {
return userPassword === testPassword;
}
function getRandomLetterNumber() {
let randomIndex = Math.floor(Math.random() * 62);
return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ1234567890'[randomIndex];
}
function anonymize() {
let result = '';
for (let i = 0; i < 16; i += 1) {
result += getRandomLetterNumber();
}
return result;
}
return {
init: function(email, password, firstName, lastName) {
this.userEmail = email;
this.userPassword = password;
this.userFirstName = firstName;
this.userLastName = lastName;
this.displayName = anonymize();
return this;
},
reanonymize: function(password) {
if (isValidPassword(this.userPassword, password)) {
this.displayName = anonymize();
return true
} else {
return 'Invalid Password';
}
},
resetPassword: function(currentPassword, newPassword) {
if (isValidPassword(this.userPassword, currentPassword)) {
this.userPassword = newPassword;
return true;
} else {
return 'Invalid Password';
}
},
firstName: function(password) {
if (isValidPassword(this.userPassword, password)) {
return this.userFirstName;
} else {
return 'Invalid Password';
}
},
lastName: function(password) {
if (isValidPassword(this.userPassword, password)) {
return this.userLastName;
} else {
return 'Invalid Password';
}
},
email: function(password) {
if (isValidPassword(this.userPassword, password)) {
return this.userEmail;
} else {
return 'Invalid Password';
}
},
};
})();
let fooBar = Object.create(Account).init('foo@bar.com', '123456', 'foo', 'bar');
console.log(fooBar.firstName); // returns the firstName function
console.log(fooBar.email); // returns the email function
console.log(fooBar.firstName('123456')); // logs 'foo'
console.log(fooBar.firstName('abc')); // logs 'Invalid Password'
console.log(fooBar.displayName); // logs 16 character sequence
console.log(fooBar.resetPassword('123', 'abc')) // logs 'Invalid Password'
console.log(fooBar.resetPassword('123456', 'abc')) // logs true
let displayName = fooBar.displayName;
fooBar.reanonymize('abc'); // returns true
console.log(displayName === fooBar.displayName); // logs false
let bazQux = Object.create(Account).init('baz@qux.com', '123456', 'baz', 'qux');
console.log(bazQux.firstName('123456')); // logs 'Invalid Password'
console.log(fooBar.email('abc')); // logs 'Invalid Password'