-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
165 lines (149 loc) · 3.65 KB
/
Copy pathmain.js
File metadata and controls
165 lines (149 loc) · 3.65 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class PhoneApp{
constructor(){
this.database = [];
}
addUser (user) {
let currentUser = {};
currentUser.id = this.database.length + 1;
currentUser.avatar = user.avatar || "avatar-2"
if (user.name) {
currentUser.name = user.name;
}
if (user.phone) {
if (!this.checkPhoneNumber(user.phone)) {
currentUser.phone = user.phone;
} else {
console.log(
"не удалось сохранить номер телефона, Телефон должен состоять только из цифр"
);
}
}
if (user.homePhone) {
if (!this.checkPhoneNumber(user.homePhone)) {
currentUser.homePhone = user.homePhone;
} else {
console.log(
"не удалось сохранить номер телефона, Телефон должен состоять только из цифр"
);
}
}
this.database.push(currentUser);
};
checkPhoneNumber(phone){
isNaN(+phone)
};
deleteUser(id) {
this.database.filter((elem, index) => {
if (elem.id == id) {
this.database.splice(index, 1);
}
});
};
searchUserByName(name) {
return this.database.filter(function(elem) {
if (elem.name == name) {
return elem;
}
});
};
editUser(id, options) {
this.database.map(elem => {
if (elem.id == id) {
if (options.name) {
elem.name = options.name;
}
if (options.phone) {
elem.phone = options.phone;
}
if (options.homePhone) {
elem.homePhone = options.homePhone;
}
}
});
};
filterUser(param) {
return this.database.filter(elem => {
if (elem[param]) {
return elem;
}
});
};
sortUser(param, direction) {
return this.database.sort((a, b) => {
if (direction) {
//from small value to big value
if (direction == "up") {
return a[param] < b[param];
}
//from big value to small value
if (direction == "down") {
return a[param] > b[param];
}
//default
} else {
return a.id > b.id;
}
});
};
}
class User{
constructor(options){
this.name = options.name
this.phone = options.phone
this.homePhone = options.homePhone
this.avatar = options.avatar
}
}
let vasya = new User({
name: "Vasya",
phone: "123456789",
homePhone: "11111"
})
let petja = new User({ name: "Petja", phone: "123456798" })
let brigitte = new User({ name: "Brigitte", phone: "123457689", avatar:"girl-1" })
let tracer = new User({ name: "Tracer", phone: "123546789" })
let anduin = new User({
name: "Anduin",
phone: "113456789",
homePhone: "535353"
})
let torgrim = new User({ name: "Torgrim", phone: "321456789", avatar:"man-2" })
let anduin2 = new User({
name: "Anduin",
phone: "113451189",
homePhone: "222222"
})
let user = new User({
name: "User",
phone: "113451189",
homePhone: "222222"
})
let name = new User({
name: "Name",
phone: "113451189",
homePhone: "222222"
})
let someUaser = new User({
name: "someUaser",
phone: "113451189",
homePhone: "222222"
})
let blabla = new User({
name: "blabla",
phone: "113451189",
homePhone: "222222"
})
const myPhoneApp = new PhoneApp();
myPhoneApp.addUser(vasya);
myPhoneApp.addUser(petja);
myPhoneApp.addUser(brigitte);
myPhoneApp.addUser(tracer);
myPhoneApp.addUser(anduin);
myPhoneApp.addUser(torgrim);
myPhoneApp.addUser(anduin2);
myPhoneApp.addUser(user);
myPhoneApp.addUser(name);
myPhoneApp.addUser(someUaser);
myPhoneApp.addUser(blabla);
console.log(myPhoneApp)
console.log(myPhoneApp.searchUserByName('Anduin'));