-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcardToJson.js
75 lines (70 loc) · 2.71 KB
/
vcardToJson.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
var vcard = require('vcard-json');
const fs = require('fs');
var file = './data/StullerContacts.json';
var arr = {customers:{}};
vcard.parseVcardFile('./data/StullerContacts.vcf', function(err, data){
// Add logic here to setup json fields correctly.
try {
data.forEach(element => {
// Setup the firstName and lastName from fullname.
$firstName = element.fullname.substr(0,element.fullname.indexOf(' '));
$lastName = element.fullname.substr(element.fullname.indexOf(' ')+1);
// If there's a billing address, create a location for it in Firestore.
if (element.billing[0]) {
billing = {
'address': element.billing[0].street,
'city': element.billing[0].city,
'state': element.billing[0].region,
'zipcode': element.billing[0].zip
}
}
// An empty object will not create an address in Firestore.
else billing = {};
$notes = element.note.split("\\n").join("\n").split("\\").join("");
// Create the new customer array.
arr.customers[element.fullname] = {
'firstName': $firstName,
'lastName': $lastName,
'email': element.email[0] ? element.email[0].value : '',
'main': element.phone[0] ? element.phone[0].value : '',
'mobile': element.cell,
'notes': $notes,
'payment': element.payment,
'locations': {
billing,
'primary': {
'name': 'home',
'address': element.addr[0] ? element.addr[0].street : '',
'city': element.addr[0] ? element.addr[0].city : '',
'state': element.addr[0] ? element.addr[0].region : '',
'zipcode': element.addr[0] ? element.addr[0].zip : '',
'area': element.addr[0] ? element.addr[0].locality : '',
'generators': {
'gen': {
'generator': element.generator ? element.generator : element.nickname,
'model': element.model,
'serial': element.serial,
'battery age': element.battery,
'xfer location': element.xferLocation,
'xfer serial': element.xferSerial,
'spark plugs': element.sparkPlugs,
'exercise time': element.exercise,
'oil filter': element.oilFilter,
'air filter': element.airFilter,
'warranty': element.warranty,
'location': element.location,
'wifi': element.wifi
}
}
}
}
};
});
// Write the array to a new json file.
fs.writeFileSync(file, JSON.stringify(arr))
$date = new Date();
console.log('finished', $date);
} catch (err) {
console.error(err)
}
});