@@ -8,11 +8,12 @@ function getContactsByName(name) {
88
99function addNewContact ( contact ) {
1010 if ( ! contact || Object . keys ( contact ) . length === 0 ) {
11- throw new TypeError ( 'contact must be a nonempty object' )
11+ throw new TypeError ( 'contact must be a non-empty object' )
1212 } else {
1313 const hasFirstName = contact . hasOwnProperty ( 'firstName' )
1414 const hasLastName = contact . hasOwnProperty ( 'lastName' )
1515 const hasNickname = contact . hasOwnProperty ( 'nickname' )
16+ const hasBirthday = contact . hasOwnProperty ( 'birthday' )
1617 const hasPhoneNumbers = contact . hasOwnProperty ( 'phoneNumbers' )
1718 const hasPostalAddresses = contact . hasOwnProperty ( 'postalAddresses' )
1819 const hasEmailAddresses = contact . hasOwnProperty ( 'emailAddresses' )
@@ -23,6 +24,15 @@ function addNewContact(contact) {
2324 if ( hasPhoneNumbers && ! Array . isArray ( contact . phoneNumbers ) ) throw new TypeError ( 'phoneNumbers must be an array' )
2425 if ( hasPostalAddresses && ! Array . isArray ( contact . postalAddresses ) ) throw new TypeError ( 'postalAddresses must be an array' )
2526 if ( hasEmailAddresses && ! Array . isArray ( contact . emailAddresses ) ) throw new TypeError ( 'emailAddresses must be an array' )
27+
28+ if ( hasBirthday ) {
29+ const datePattern = / ^ \d { 4 } \- ( 0 [ 1 - 9 ] | 1 [ 0 1 2 ] ) \- ( 0 [ 1 - 9 ] | [ 1 2 ] [ 0 - 9 ] | 3 [ 0 1 ] ) $ /
30+ if ( typeof contact . birthday !== 'string' ) {
31+ throw new TypeError ( 'birthday must be a string' )
32+ } else if ( ! contact . birthday . match ( datePattern ) ) {
33+ throw new Error ( 'birthday must use YYYY-MM-DD format' )
34+ }
35+ }
2636 }
2737
2838 return contacts . addNewContact . call ( this , contact )
0 commit comments