Skip to content

Commit 81af9e9

Browse files
committed
chore: format with clang-format
1 parent 238a732 commit 81af9e9

File tree

3 files changed

+124
-74
lines changed

3 files changed

+124
-74
lines changed

contacts.mm

Lines changed: 89 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <napi.h>
21
#import <Contacts/Contacts.h>
2+
#include <napi.h>
33

44
/***** HELPERS *****/
55

@@ -8,9 +8,11 @@
88
int num_email_addresses = [[cncontact emailAddresses] count];
99

1010
Napi::Array email_addresses = Napi::Array::New(env, num_email_addresses);
11-
NSArray <CNLabeledValue<NSString*>*> *emailAddresses = [cncontact emailAddresses];
11+
NSArray<CNLabeledValue<NSString *> *> *emailAddresses =
12+
[cncontact emailAddresses];
1213
for (int i = 0; i < num_email_addresses; i++) {
13-
CNLabeledValue<NSString*> *email_address = [emailAddresses objectAtIndex:i];
14+
CNLabeledValue<NSString *> *email_address =
15+
[emailAddresses objectAtIndex:i];
1416
email_addresses[i] = std::string([[email_address value] UTF8String]);
1517
}
1618

@@ -22,9 +24,10 @@
2224
int num_phone_numbers = [[cncontact phoneNumbers] count];
2325

2426
Napi::Array phone_numbers = Napi::Array::New(env, num_phone_numbers);
25-
NSArray <CNLabeledValue<CNPhoneNumber*>*> *phoneNumbers = [cncontact phoneNumbers];
27+
NSArray<CNLabeledValue<CNPhoneNumber *> *> *phoneNumbers =
28+
[cncontact phoneNumbers];
2629
for (int i = 0; i < num_phone_numbers; i++) {
27-
CNLabeledValue<CNPhoneNumber*> *phone = [phoneNumbers objectAtIndex:i];
30+
CNLabeledValue<CNPhoneNumber *> *phone = [phoneNumbers objectAtIndex:i];
2831
CNPhoneNumber *number = [phone value];
2932
phone_numbers[i] = std::string([[number stringValue] UTF8String]);
3033
}
@@ -38,7 +41,8 @@
3841
Napi::Array postal_addresses = Napi::Array::New(env, num_postal_addresses);
3942

4043
CNPostalAddressFormatter *formatter = [[CNPostalAddressFormatter alloc] init];
41-
NSArray *postalAddresses = (NSArray*)[[cncontact postalAddresses] valueForKey:@"value"];
44+
NSArray *postalAddresses =
45+
(NSArray *)[[cncontact postalAddresses] valueForKey:@"value"];
4246
for (int i = 0; i < num_postal_addresses; i++) {
4347
CNPostalAddress *address = [postalAddresses objectAtIndex:i];
4448
NSString *addr_string = [formatter stringFromPostalAddress:address];
@@ -56,10 +60,10 @@
5660
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
5761
[formatter setDateFormat:@"yyyy-MM-dd"];
5862

59-
NSString *birthday = [formatter stringFromDate:birth_date];
63+
NSString *birthday = [formatter stringFromDate:birth_date];
6064
if (birthday)
6165
result = std::string([birthday UTF8String]);
62-
66+
6367
return result;
6468
}
6569

@@ -89,118 +93,133 @@
8993
return contact;
9094
}
9195

92-
// Parses an array of phone number strings and converts them to an NSArray of CNPhoneNumbers
93-
NSArray* ParsePhoneNumbers(Napi::Array phone_number_data) {
96+
// Parses an array of phone number strings and converts them to an NSArray of
97+
// CNPhoneNumbers
98+
NSArray *ParsePhoneNumbers(Napi::Array phone_number_data) {
9499
NSMutableArray *phone_numbers = [[NSMutableArray alloc] init];
95100

96101
int data_length = static_cast<int>(phone_number_data.Length());
97102
for (int i = 0; i < data_length; i++) {
98-
std::string number_str = phone_number_data.Get(i).As<Napi::String>().Utf8Value();
103+
std::string number_str =
104+
phone_number_data.Get(i).As<Napi::String>().Utf8Value();
99105
NSString *number = [NSString stringWithUTF8String:number_str.c_str()];
100-
CNPhoneNumber *phone_number = [CNPhoneNumber phoneNumberWithStringValue:number];
101-
CNLabeledValue *labeled_value = [CNLabeledValue labeledValueWithLabel:@"Home" value:phone_number];
106+
CNPhoneNumber *phone_number =
107+
[CNPhoneNumber phoneNumberWithStringValue:number];
108+
CNLabeledValue *labeled_value =
109+
[CNLabeledValue labeledValueWithLabel:@"Home" value:phone_number];
102110
[phone_numbers addObject:labeled_value];
103111
}
104112

105113
return phone_numbers;
106114
}
107115

108-
// Parses an array of email address strings and converts them to an NSArray of NSStrings
109-
NSArray* ParseEmailAddresses(Napi::Array email_address_data) {
116+
// Parses an array of email address strings and converts them to an NSArray of
117+
// NSStrings
118+
NSArray *ParseEmailAddresses(Napi::Array email_address_data) {
110119
NSMutableArray *email_addresses = [[NSMutableArray alloc] init];
111120

112121
int data_length = static_cast<int>(email_address_data.Length());
113122
for (int i = 0; i < data_length; i++) {
114-
std::string email_str = email_address_data.Get(i).As<Napi::String>().Utf8Value();
123+
std::string email_str =
124+
email_address_data.Get(i).As<Napi::String>().Utf8Value();
115125
NSString *email = [NSString stringWithUTF8String:email_str.c_str()];
116-
CNLabeledValue *labeled_value = [CNLabeledValue labeledValueWithLabel:@"Home" value:email];
126+
CNLabeledValue *labeled_value =
127+
[CNLabeledValue labeledValueWithLabel:@"Home" value:email];
117128
[email_addresses addObject:labeled_value];
118129
}
119130

120131
return email_addresses;
121132
}
122133

123-
// Parses a string in YYYY-MM-DD format and converts it to an NSDatComponents object
124-
NSDateComponents* ParseBirthday(std::string birth_day) {
134+
// Parses a string in YYYY-MM-DD format and converts it to an NSDatComponents
135+
// object
136+
NSDateComponents *ParseBirthday(std::string birth_day) {
125137
NSString *bday = [NSString stringWithUTF8String:birth_day.c_str()];
126138
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
127139
[formatter setDateFormat:@"yyyy-MM-dd"];
128140

129141
NSDate *bday_date = [formatter dateFromString:bday];
130142

131143
NSCalendar *cal = [NSCalendar currentCalendar];
132-
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
133-
NSDateComponents *birthday_components = [cal components:unitFlags fromDate:bday_date];
144+
unsigned unitFlags =
145+
NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
146+
NSDateComponents *birthday_components = [cal components:unitFlags
147+
fromDate:bday_date];
134148

135149
return birthday_components;
136150
}
137151

138-
// Returns a status indicating whether or not the user has authorized Contacts access
152+
// Returns a status indicating whether or not the user has authorized Contacts
153+
// access
139154
CNAuthorizationStatus AuthStatus() {
140155
CNEntityType entityType = CNEntityTypeContacts;
141156
return [CNContactStore authorizationStatusForEntityType:entityType];
142157
}
143158

144159
// Returns the set of Contacts properties to retrieve from the CNContactStore
145-
NSArray* GetContactKeys() {
160+
NSArray *GetContactKeys() {
146161
NSArray *keys = @[
147-
CNContactGivenNameKey,
148-
CNContactFamilyNameKey,
149-
CNContactPhoneNumbersKey,
150-
CNContactEmailAddressesKey,
151-
CNContactNicknameKey,
152-
CNContactPostalAddressesKey,
153-
CNContactBirthdayKey
162+
CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey,
163+
CNContactEmailAddressesKey, CNContactNicknameKey,
164+
CNContactPostalAddressesKey, CNContactBirthdayKey
154165
];
155166

156167
return keys;
157168
}
158169

159-
// Returns all contacts in the CNContactStore matching a specified name string predicate
160-
NSArray* FindContacts(const std::string& name_string) {
170+
// Returns all contacts in the CNContactStore matching a specified name string
171+
// predicate
172+
NSArray *FindContacts(const std::string &name_string) {
161173
CNContactStore *addressBook = [[CNContactStore alloc] init];
162174

163175
NSString *name = [NSString stringWithUTF8String:name_string.c_str()];
164176
NSPredicate *predicate = [CNContact predicateForContactsMatchingName:name];
165177

166178
return [addressBook unifiedContactsMatchingPredicate:predicate
167-
keysToFetch:GetContactKeys()
168-
error:nil];
179+
keysToFetch:GetContactKeys()
180+
error:nil];
169181
}
170182

171-
// Creates a new CNContact in order to update, delete, or add it to the CNContactStore
172-
CNMutableContact* CreateCNMutableContact(Napi::Object contact_data) {
183+
// Creates a new CNContact in order to update, delete, or add it to the
184+
// CNContactStore
185+
CNMutableContact *CreateCNMutableContact(Napi::Object contact_data) {
173186
CNMutableContact *contact = [[CNMutableContact alloc] init];
174-
187+
175188
if (contact_data.Has("firstName")) {
176-
std::string first_name = contact_data.Get("firstName").As<Napi::String>().Utf8Value();
189+
std::string first_name =
190+
contact_data.Get("firstName").As<Napi::String>().Utf8Value();
177191
[contact setGivenName:[NSString stringWithUTF8String:first_name.c_str()]];
178192
}
179193

180194
if (contact_data.Has("lastName")) {
181-
std::string last_name = contact_data.Get("lastName").As<Napi::String>().Utf8Value();
195+
std::string last_name =
196+
contact_data.Get("lastName").As<Napi::String>().Utf8Value();
182197
[contact setFamilyName:[NSString stringWithUTF8String:last_name.c_str()]];
183198
}
184199

185200
if (contact_data.Has("nickname")) {
186-
std::string nick_name = contact_data.Get("nickname").As<Napi::String>().Utf8Value();
201+
std::string nick_name =
202+
contact_data.Get("nickname").As<Napi::String>().Utf8Value();
187203
[contact setNickname:[NSString stringWithUTF8String:nick_name.c_str()]];
188204
}
189205

190206
if (contact_data.Has("birthday")) {
191-
std::string birth_day = contact_data.Get("birthday").As<Napi::String>().Utf8Value();
207+
std::string birth_day =
208+
contact_data.Get("birthday").As<Napi::String>().Utf8Value();
192209
NSDateComponents *birthday_components = ParseBirthday(birth_day);
193210
[contact setBirthday:birthday_components];
194211
}
195212

196213
if (contact_data.Has("phoneNumbers")) {
197-
Napi::Array phone_number_data = contact_data.Get("phoneNumbers").As<Napi::Array>();
214+
Napi::Array phone_number_data =
215+
contact_data.Get("phoneNumbers").As<Napi::Array>();
198216
NSArray *phone_numbers = ParsePhoneNumbers(phone_number_data);
199217
[contact setPhoneNumbers:[NSArray arrayWithArray:phone_numbers]];
200218
}
201219

202220
if (contact_data.Has("emailAddresses")) {
203-
Napi::Array email_address_data = contact_data.Get("emailAddresses").As<Napi::Array>();
221+
Napi::Array email_address_data =
222+
contact_data.Get("emailAddresses").As<Napi::Array>();
204223
NSArray *email_addresses = ParseEmailAddresses(email_address_data);
205224
[contact setEmailAddresses:[NSArray arrayWithArray:email_addresses]];
206225
}
@@ -232,15 +251,18 @@ CNAuthorizationStatus AuthStatus() {
232251
Napi::Env env = info.Env();
233252
Napi::Array contacts = Napi::Array::New(env);
234253
CNContactStore *addressBook = [[CNContactStore alloc] init];
235-
254+
236255
if (AuthStatus() != CNAuthorizationStatusAuthorized)
237256
return contacts;
238257

239-
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:addressBook.defaultContainerIdentifier];
240-
NSArray *cncontacts = [addressBook unifiedContactsMatchingPredicate:predicate
241-
keysToFetch:GetContactKeys()
242-
error:nil];
243-
258+
NSPredicate *predicate =
259+
[CNContact predicateForContactsInContainerWithIdentifier:
260+
addressBook.defaultContainerIdentifier];
261+
NSArray *cncontacts =
262+
[addressBook unifiedContactsMatchingPredicate:predicate
263+
keysToFetch:GetContactKeys()
264+
error:nil];
265+
244266
int num_contacts = [cncontacts count];
245267
for (int i = 0; i < num_contacts; i++) {
246268
CNContact *cncontact = [cncontacts objectAtIndex:i];
@@ -260,7 +282,7 @@ CNAuthorizationStatus AuthStatus() {
260282

261283
const std::string name_string = info[0].As<Napi::String>().Utf8Value();
262284
NSArray *cncontacts = FindContacts(name_string);
263-
285+
264286
int num_contacts = [cncontacts count];
265287
for (int i = 0; i < num_contacts; i++) {
266288
CNContact *cncontact = [cncontacts objectAtIndex:i];
@@ -297,11 +319,11 @@ CNAuthorizationStatus AuthStatus() {
297319

298320
const std::string name_string = info[0].As<Napi::String>().Utf8Value();
299321
NSArray *cncontacts = FindContacts(name_string);
300-
301-
CNContact *contact = (CNContact*)[cncontacts objectAtIndex:0];
322+
323+
CNContact *contact = (CNContact *)[cncontacts objectAtIndex:0];
302324
CNSaveRequest *request = [[CNSaveRequest alloc] init];
303325
[request deleteContact:[contact mutableCopy]];
304-
326+
305327
CNContactStore *addressBook = [[CNContactStore alloc] init];
306328
bool success = [addressBook executeSaveRequest:request error:nil];
307329

@@ -316,11 +338,11 @@ CNAuthorizationStatus AuthStatus() {
316338
return Napi::Boolean::New(env, false);
317339

318340
Napi::Object contact_data = info[0].As<Napi::Object>();
319-
341+
320342
CNMutableContact *contact = CreateCNMutableContact(contact_data);
321343
CNSaveRequest *request = [[CNSaveRequest alloc] init];
322344
[request updateContact:contact];
323-
345+
324346
CNContactStore *addressBook = [[CNContactStore alloc] init];
325347
bool success = [addressBook executeSaveRequest:request error:nil];
326348

@@ -329,24 +351,18 @@ CNAuthorizationStatus AuthStatus() {
329351

330352
// Initializes all functions exposed to JS
331353
Napi::Object Init(Napi::Env env, Napi::Object exports) {
332-
exports.Set(
333-
Napi::String::New(env, "getAuthStatus"), Napi::Function::New(env, GetAuthStatus)
334-
);
335-
exports.Set(
336-
Napi::String::New(env, "getAllContacts"), Napi::Function::New(env, GetAllContacts)
337-
);
338-
exports.Set(
339-
Napi::String::New(env, "getContactsByName"), Napi::Function::New(env, GetContactsByName)
340-
);
341-
exports.Set(
342-
Napi::String::New(env, "addNewContact"), Napi::Function::New(env, AddNewContact)
343-
);
344-
exports.Set(
345-
Napi::String::New(env, "deleteContact"), Napi::Function::New(env, DeleteContact)
346-
);
347-
exports.Set(
348-
Napi::String::New(env, "updateContact"), Napi::Function::New(env, UpdateContact)
349-
);
354+
exports.Set(Napi::String::New(env, "getAuthStatus"),
355+
Napi::Function::New(env, GetAuthStatus));
356+
exports.Set(Napi::String::New(env, "getAllContacts"),
357+
Napi::Function::New(env, GetAllContacts));
358+
exports.Set(Napi::String::New(env, "getContactsByName"),
359+
Napi::Function::New(env, GetContactsByName));
360+
exports.Set(Napi::String::New(env, "addNewContact"),
361+
Napi::Function::New(env, AddNewContact));
362+
exports.Set(Napi::String::New(env, "deleteContact"),
363+
Napi::Function::New(env, DeleteContact));
364+
exports.Set(Napi::String::New(env, "updateContact"),
365+
Napi::Function::New(env, UpdateContact));
350366

351367
return exports;
352368
}

package-lock.json

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)