Skip to content

Commit 79e6a41

Browse files
refactor: args validation (#26)
1 parent e628175 commit 79e6a41

File tree

1 file changed

+23
-71
lines changed

1 file changed

+23
-71
lines changed

index.js

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -62,91 +62,43 @@ function getContactsByName(name, extraProperties = []) {
6262
return contacts.getContactsByName.call(this, name, extraProperties)
6363
}
6464

65-
function addNewContact(contact) {
65+
function validateContactArg(contact) {
6666
if (!contact || Object.keys(contact).length === 0) {
6767
throw new TypeError('contact must be a non-empty object')
68-
} else {
69-
const hasFirstName = contact.hasOwnProperty('firstName')
70-
const hasLastName = contact.hasOwnProperty('lastName')
71-
const hasNickname = contact.hasOwnProperty('nickname')
72-
const hasBirthday = contact.hasOwnProperty('birthday')
73-
const hasPhoneNumbers = contact.hasOwnProperty('phoneNumbers')
74-
const hasEmailAddresses = contact.hasOwnProperty('emailAddresses')
75-
76-
if (hasFirstName && typeof contact.firstName !== 'string') {
77-
throw new TypeError('firstName must be a string')
78-
}
79-
80-
if (hasLastName && typeof contact.lastName !== 'string') {
81-
throw new TypeError('lastName must be a string')
82-
}
68+
}
8369

84-
if (hasNickname && typeof contact.nickname !== 'string') {
85-
throw new TypeError('nickname must be a string')
70+
for (const prop of ['firstName', 'lastName', 'nickname']) {
71+
const hasProp = contact.hasOwnProperty(prop)
72+
if (hasProp && typeof contact[prop] !== 'string') {
73+
throw new TypeError(`${prop} must be a string`)
8674
}
87-
88-
if (hasPhoneNumbers && !Array.isArray(contact.phoneNumbers)) {
89-
throw new TypeError('phoneNumbers must be an array')
75+
}
76+
for (const prop of ['phoneNumbers', 'emailAddresses']) {
77+
const hasProp = contact.hasOwnProperty(prop)
78+
if (hasProp && !Array.isArray(contact[prop])) {
79+
throw new TypeError(`${prop} must be an array`)
9080
}
81+
}
9182

92-
if (hasEmailAddresses && !Array.isArray(contact.emailAddresses)) {
93-
throw new TypeError('emailAddresses must be an array')
94-
}
83+
const hasBirthday = contact.hasOwnProperty('birthday')
9584

96-
if (hasBirthday) {
97-
const datePattern = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/
98-
if (typeof contact.birthday !== 'string') {
99-
throw new TypeError('birthday must be a string')
100-
} else if (!contact.birthday.match(datePattern)) {
101-
throw new Error('birthday must use YYYY-MM-DD format')
102-
}
85+
if (hasBirthday) {
86+
const datePattern = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/
87+
if (typeof contact.birthday !== 'string') {
88+
throw new TypeError('birthday must be a string')
89+
} else if (!contact.birthday.match(datePattern)) {
90+
throw new Error('birthday must use YYYY-MM-DD format')
10391
}
10492
}
93+
}
10594

95+
function addNewContact(contact) {
96+
validateContactArg(contact)
10697
return contacts.addNewContact.call(this, contact)
10798
}
10899

109100
function updateContact(contact) {
110-
if (!contact || Object.keys(contact).length === 0) {
111-
throw new TypeError('contact must be a non-empty object')
112-
} else {
113-
const hasFirstName = contact.hasOwnProperty('firstName')
114-
const hasLastName = contact.hasOwnProperty('lastName')
115-
const hasNickname = contact.hasOwnProperty('nickname')
116-
const hasBirthday = contact.hasOwnProperty('birthday')
117-
const hasPhoneNumbers = contact.hasOwnProperty('phoneNumbers')
118-
const hasEmailAddresses = contact.hasOwnProperty('emailAddresses')
119-
120-
if (hasFirstName && typeof contact.firstName !== 'string') {
121-
throw new TypeError('firstName must be a string')
122-
}
123-
124-
if (hasLastName && typeof contact.lastName !== 'string') {
125-
throw new TypeError('lastName must be a string')
126-
}
127-
128-
if (hasNickname && typeof contact.nickname !== 'string') {
129-
throw new TypeError('nickname must be a string')
130-
}
131-
132-
if (hasPhoneNumbers && !Array.isArray(contact.phoneNumbers)) {
133-
throw new TypeError('phoneNumbers must be an array')
134-
}
135-
136-
if (hasEmailAddresses && !Array.isArray(contact.emailAddresses)) {
137-
throw new TypeError('emailAddresses must be an array')
138-
}
139-
140-
if (hasBirthday) {
141-
const datePattern = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/
142-
if (typeof contact.birthday !== 'string') {
143-
throw new TypeError('birthday must be a string')
144-
} else if (!contact.birthday.match(datePattern)) {
145-
throw new Error('birthday must use YYYY-MM-DD format')
146-
}
147-
}
148-
}
149-
101+
validateContactArg(contact)
150102
return contacts.updateContact.call(this, contact)
151103
}
152104

0 commit comments

Comments
 (0)