-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·142 lines (117 loc) · 3.55 KB
/
index.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const contacts = require('bindings')('contacts.node')
const { EventEmitter } = require('events')
const listener = new EventEmitter()
listener.setup = () => {
contacts.setupListener(listener.emit.bind(listener))
}
listener.remove = () => {
contacts.removeListener()
}
listener.isListening = () => contacts.isListening()
const optionalProperties = [
'jobTitle',
'departmentName',
'organizationName',
'middleName',
'note',
'contactImage',
'contactThumbnailImage',
'instantMessageAddresses',
'socialProfiles',
'urlAddresses',
]
function getAllContacts(extraProperties = []) {
if (!Array.isArray(extraProperties)) {
throw new TypeError('extraProperties must be an array')
}
if (!extraProperties.every((p) => optionalProperties.includes(p))) {
throw new TypeError(
`properties in extraProperties must be one of ${optionalProperties.join(
', ',
)}`,
)
}
return contacts.getAllContacts.call(this, extraProperties)
}
function getContactsByName(name, extraProperties = []) {
if (typeof name !== 'string') {
throw new TypeError('name must be a string')
}
if (!Array.isArray(extraProperties)) {
throw new TypeError('extraProperties must be an array')
}
if (!extraProperties.every((p) => optionalProperties.includes(p))) {
throw new TypeError(
`properties in extraProperties must be one of ${optionalProperties.join(
', ',
)}`,
)
}
return contacts.getContactsByName.call(this, name, extraProperties)
}
function validateContactArg(contact) {
if (!contact || Object.keys(contact).length === 0) {
throw new TypeError('contact must be a non-empty object')
}
for (const prop of [
'firstName',
'middleName',
'lastName',
'nickname',
'jobTitle',
'departmentName',
'organizationName',
]) {
const hasProp = contact.hasOwnProperty(prop)
if (hasProp && typeof contact[prop] !== 'string') {
throw new TypeError(`${prop} must be a string`)
}
}
for (const prop of ['phoneNumbers', 'emailAddresses', 'urlAddresses']) {
const hasProp = contact.hasOwnProperty(prop)
if (hasProp && !Array.isArray(contact[prop])) {
throw new TypeError(`${prop} must be an array`)
}
}
const hasBirthday = contact.hasOwnProperty('birthday')
if (hasBirthday) {
const datePattern = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/
if (typeof contact.birthday !== 'string') {
throw new TypeError('birthday must be a string')
} else if (!contact.birthday.match(datePattern)) {
throw new Error('birthday must use YYYY-MM-DD format')
}
}
}
function addNewContact(contact) {
validateContactArg(contact)
return contacts.addNewContact.call(this, contact)
}
function updateContact(contact) {
validateContactArg(contact)
return contacts.updateContact.call(this, contact)
}
function deleteContact(contact) {
if (!contact || Object.keys(contact).length === 0) {
throw new TypeError('contact must be a non-empty object')
}
const hasIdentifier = contact.hasOwnProperty('identifier')
const hasName = contact.hasOwnProperty('name')
if (hasIdentifier && typeof contact.identifier !== 'string') {
throw new TypeError('identifier must be a string')
}
if (hasName && typeof contact.name !== 'string') {
throw new TypeError('name must be a string')
}
return contacts.deleteContact.call(this, contact)
}
module.exports = {
listener,
requestAccess: contacts.requestAccess,
getAuthStatus: contacts.getAuthStatus,
getAllContacts,
getContactsByName,
addNewContact,
deleteContact,
updateContact,
}