Skip to content

Commit 08fd284

Browse files
Balashov NikitaOlegLustenko
authored andcommitted
refactored
1 parent ca4bd89 commit 08fd284

File tree

1 file changed

+131
-86
lines changed
  • js-core/homeworks/phoneApp/src

1 file changed

+131
-86
lines changed

js-core/homeworks/phoneApp/src/main.js

Lines changed: 131 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,154 @@
1-
function User(addArgument) {
2-
this.name = addArgument.name;
3-
this.surname = addArgument.surname;
4-
this.number = addArgument.number;
5-
this.city = addArgument.city;
6-
this.company = addArgument.company;
7-
}
8-
9-
function PhoneApp() {
10-
this.storage = [];
11-
}
12-
13-
PhoneApp.prototype.isNumber = function(stringToCheck) {
14-
const toStringTheArgument = stringToCheck.toString();
15-
const toArrayTheString = toStringTheArgument.split('');
16-
let flag;
17-
18-
toArrayTheString.some(elem => {
19-
const toNumberElem = Number(elem);
20-
if(isNaN(toNumberElem)) {
21-
flag = false;
22-
}
23-
});
1+
class User{
2+
constructor(object){
3+
const generatorId = () => '_' + Math.random().toString(36).substr(2, 20);
4+
5+
this.id = generatorId();
6+
this.name = object.name;
7+
this.surname = object.surname;
8+
this.number = this.createFormatedPhoneNumber(object.number);
9+
this.city = object.city;
10+
this.company = object.company;
11+
}
2412

25-
return flag !== false;
26-
}
27-
28-
PhoneApp.prototype.createFormatedPhoneNumber = function(phoneNumber) {
29-
const toStringTheNumber = phoneNumber.toString();
30-
const toArrayTheString = toStringTheNumber.split('');
31-
let rewritedArray = [];
32-
33-
toArrayTheString.some(elem => {
34-
if(elem !== '-') {
35-
rewritedArray.push(elem)
36-
}
37-
});
13+
isNumber(stringToCheck) {
14+
const toArrayTheString = stringToCheck.split('');
3815

39-
const joinRewritedArray = rewritedArray.join('')
16+
return toArrayTheString.every(elem => !isNaN(Number(elem)));
17+
}
4018

41-
if(this.isNumber(joinRewritedArray)) {
42-
const firstThreeNumbers = joinRewritedArray.slice(0, 3);
19+
createFormatedPhoneNumber(phoneNumber) {
20+
if(!this.isNumber(phoneNumber)) {
21+
return console.log('EROR you had typed incorect number')
22+
}
23+
24+
const firstThreeNumbers = phoneNumber.slice(0, 3);
4325
const bracketThisFirstThreeNumbers = `(${firstThreeNumbers}) `;
4426

45-
const secondTwoNumbers = joinRewritedArray.slice(3, 5);
27+
const secondTwoNumbers = phoneNumber.slice(3, 5);
4628
const bracketThisSecondTwoNumbers = `${secondTwoNumbers}-`;
4729

48-
const thirdTwoNumbers = joinRewritedArray.slice(5, 7);
30+
const thirdTwoNumbers = phoneNumber.slice(5, 7);
4931
const bracketThisThirdTwoNumbers = `${thirdTwoNumbers}-`;
5032

51-
let lastNumbers = joinRewritedArray.slice(7, joinRewritedArray.length);
33+
let lastNumbers = phoneNumber.slice(7, phoneNumber.length);
5234

5335
const formatedPhoneNumber = bracketThisFirstThreeNumbers +
54-
bracketThisSecondTwoNumbers +
55-
bracketThisThirdTwoNumbers +
56-
lastNumbers;
36+
bracketThisSecondTwoNumbers +
37+
bracketThisThirdTwoNumbers +
38+
lastNumbers;
5739

5840
return formatedPhoneNumber;
59-
} else {
60-
return console.log('EROR you had typed incorect number')
61-
}
62-
}
63-
64-
PhoneApp.prototype.add = function(object) {
65-
const idNumber = this.storage.length + 1;
66-
const contact = new User(object);
67-
contact.id = idNumber;
68-
contact.number = this.createFormatedPhoneNumber(contact.number)
69-
this.storage.push(contact);
70-
}
71-
72-
PhoneApp.prototype.filterByValue = function(key, value) {
73-
return this.storage.filter(user => {
74-
return user[key] === value;
75-
});
41+
}
42+
7643
}
7744

78-
PhoneApp.prototype.sortUsersByValue = function(key) {
79-
const sortFunction = function(value, nextValue) {
80-
if(value[key] > nextValue[key]) return 1;
81-
if(value[key] < nextValue[key]) return -1;
45+
class PhoneApp{
46+
constructor() {
47+
this.storage = [];
8248
}
8349

84-
let copyStorage = [...this.storage]
85-
return copyStorage.sort(sortFunction);
86-
}
87-
88-
PhoneApp.prototype.removeByIndex = function(index) {
89-
let firstHalf = [];
90-
let lastHalf = [];
50+
add(object) {
51+
const contact = new User(object);
52+
this.storage.push(contact);
53+
}
9154

92-
this.storage.forEach((elem, i) => {
93-
if(i < index) {
94-
firstHalf.push(elem);
95-
}
96-
if(i < this.storage.length && i > index) {
97-
lastHalf.push(elem)
55+
filterByValue(key, value) {
56+
return this.storage.filter(user => user[key] === value)
57+
}
58+
59+
sortUsersByValue(key) {
60+
const sortFunction = function(value, nextValue) {
61+
if(value[key] > nextValue[key]) return 1;
62+
if(value[key] < nextValue[key]) return -1;
9863
}
99-
});
10064

101-
this.storage = [...firstHalf, ...lastHalf];
102-
}
103-
104-
PhoneApp.prototype.changeValueByIndex = function(index, key, value) {
105-
const elem = this.storage[index];
106-
elem[key] = value;
65+
let copyStorage = [...this.storage]
66+
return copyStorage.sort(sortFunction);
67+
}
68+
69+
searchUserById(id) {
70+
return this.storage.findIndex(user => user.id === id)
71+
}
72+
73+
searchUserByName(name) {
74+
return this.storage.findIndex(user => user.name === name)
75+
}
76+
77+
searchUserBySurname(surname) {
78+
return this.storage.findIndex(user => user.surname === surname)
79+
}
80+
81+
removeById(id) {
82+
let firstHalf = [];
83+
let lastHalf = [];
84+
const userWithCorrectId = this.searchUserById(id);
85+
86+
this.storage.forEach((user, i) => {
87+
88+
if(i < userWithCorrectId) {
89+
firstHalf.push(user);
90+
}
91+
if(i < this.storage.length && i > userWithCorrectId) {
92+
lastHalf.push(user)
93+
}
94+
});
95+
96+
this.storage = [...firstHalf, ...lastHalf];
97+
}
98+
99+
removeByName(name) {
100+
let firstHalf = [];
101+
let lastHalf = [];
102+
const userWithCorrectName = this.searchUserByName(name);
103+
104+
this.storage.forEach((user, i) => {
105+
106+
if(i < userWithCorrectName) {
107+
firstHalf.push(user);
108+
}
109+
if(i < this.storage.length && i > userWithCorrectName) {
110+
lastHalf.push(user)
111+
}
112+
});
113+
114+
this.storage = [...firstHalf, ...lastHalf];
115+
}
116+
117+
removeBySurname(surname) {
118+
let firstHalf = [];
119+
let lastHalf = [];
120+
const userWithCorrectSurname = this.searchUserBySurname(surname);
121+
122+
this.storage.forEach((user, i) => {
123+
124+
if(i < userWithCorrectSurname) {
125+
firstHalf.push(user);
126+
}
127+
if(i < this.storage.length && i > userWithCorrectSurname) {
128+
lastHalf.push(user)
129+
}
130+
});
131+
132+
this.storage = [...firstHalf, ...lastHalf];
133+
}
134+
135+
editAnyUserValueByName(id, key, value) {
136+
const userWithCorrectName = this.searchUserById(id);
137+
const elem = this.storage[userWithCorrectName];
138+
elem[key] = value;
139+
}
140+
141+
editAnyUserValueByName(name, key, value) {
142+
const userWithCorrectName = this.searchUserByName(name);
143+
const elem = this.storage[userWithCorrectName];
144+
elem[key] = value;
145+
}
146+
147+
editAnyUserValueBySurname(surname, key, value) {
148+
const userWithCorrectName = this.searchUserBySurname(surname);
149+
const elem = this.storage[userWithCorrectName];
150+
elem[key] = value;
151+
}
107152
}
108153

109154
const test = new PhoneApp();

0 commit comments

Comments
 (0)