-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectMethods.js
More file actions
101 lines (80 loc) · 1.71 KB
/
objectMethods.js
File metadata and controls
101 lines (80 loc) · 1.71 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
let me = {
firstName: 'Franco',
lastName: 'Waisfeld',
};
let friend = {
firstName: 'Carson',
lastName: 'Fassett',
}
let mother = {
firstName: 'Daniela',
lastName: 'Slavin',
}
let father = {
firstName: 'Adrian',
lastName: 'Waisfeld',
}
let people = {
collection: [],
lastIndex: 0,
fullName(person) {
console.log(person.index + ': ' + person.firstName + ' ' + person.lastName);
},
rollCall() {
this.collection.forEach(this.fullName);
},
add(person) {
if (this.isInvalidPerson(person)) {
return;
}
person.index = this.lastIndex;
this.lastIndex += 1;
this.collection.push(person);
},
getIndex(person) {
let index = -1;
this.collection.forEach(function(comparator, i) {
if (comparator.firstName === person.firstName &&
comparator.lastName === person.lastName) {
index = i;
}
});
return index;
},
remove(person) {
if (this.isInvalidPerson(person)) {
return;
}
let index = this.getIndex(person);
if (index === -1) {
return;
}
this.collection.splice(index, 1);
},
isInvalidPerson(person) {
return typeof(person.firstName) !== 'string'
|| typeof(person.lastName) !== 'string';
},
get(person) {
if (this.isInvalidPerson(person)) {
return;
}
return this.collection[this.getIndex(person)];
},
update(person) {
if (this.isInvalidPerson(person)) {
return;
}
let existingPersonId = this.getIndex(person);
if (existingPersonId === -1) {
this.add(person);
} else {
this.collection[existingPersonId] = person;
}
},
};
people.add(me);
people.add(mother);
people.add(father);
people.add(friend);
people.rollCall();