-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path4-data-clump.js
More file actions
66 lines (54 loc) · 1.44 KB
/
4-data-clump.js
File metadata and controls
66 lines (54 loc) · 1.44 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
'use strict';
// Antipattern: Data clump
const countries = {
379: 'Vatican',
380: 'Ukraine',
381: 'Serbia',
};
const areas = {
43: 'Vinnitsa',
44: 'Kiev',
62: 'Donetsk',
};
const getCountryCode = (name) => Object
.keys(countries)
.find((key) => countries[key] === name);
const getAreaCode = (name) => Object
.keys(areas)
.find((key) => areas[key] === name);
const prepareCommand = (country, area, number) => {
const countryCode = getCountryCode(country);
const areaCode = getAreaCode(area);
return `ATDP ${countryCode}${areaCode}${number}`;
};
class Person {
constructor(name, phoneNumber) {
this.name = name;
this.phone = phoneNumber;
}
parsePhone() {
const phone = this.phone;
const country = countries[phone.substring(1, 4)];
const area = areas[phone.substring(4, 6)];
const number = phone.substring(6, 13);
return [country, area, number];
}
isValid(country, area, number) {
if (!getCountryCode(country)) return false;
if (!getAreaCode(area)) return false;
if (number === '') return false;
return true;
}
call() {
const [country, area, number] = this.parsePhone();
if (!this.isValid(country, area, number)) {
throw new Error('Invalid phone number');
}
const command = prepareCommand(country, area, number);
console.log(command);
}
}
// Usage
const person = new Person('Marcus Aurelius', '+380441234567');
console.dir({ person });
person.call();