forked from ertant/vCard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
243 lines (183 loc) · 6.89 KB
/
parser.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var vCardParser = (function () {
var fieldPropertyMapping = {
"TITLE": "title",
"TEL": "telephone",
"FN": "displayName",
"N": "name",
"EMAIL": "email",
"CATEGORIES": "categories",
"ADR": "address",
"URL": "url",
"NOTE": "notes",
"ORG": "organization",
"BDAY": "birthday",
"PHOTO": "photo"
};
var fieldTypeDefaults = {
"telephone": {name: "type", value: "voice"},
"email": {name: "type", value: "internet"},
"address": {name: "type", value: "work"}
};
function lookupField(context, fieldName) {
var propertyName = fieldPropertyMapping[fieldName];
if (!propertyName && fieldName !== 'BEGIN' && fieldName !== 'END') {
context.info('define property name for ' + fieldName);
propertyName = fieldName;
}
return propertyName;
}
function removeWeirdItemPrefix(line) {
// sometimes lines are prefixed by "item" keyword like "item1.ADR;type=WORK:....."
return line.substring(0, 4) === "item" ? line.match(/item\d\.(.*)/)[1] : line;
}
function singleLine(context, fieldValue, fieldName) {
// convert escaped new lines to real new lines.
fieldValue = fieldValue.replace('\\n', '\n');
// append value if previously specified
if (context.currentCard[fieldName]) {
context.currentCard[fieldName] += '\n' + fieldValue;
} else {
context.currentCard[fieldName] = fieldValue;
}
}
function typedLine(context, fieldValue, fieldName, typeInfo, valueFormatter) {
var isDefault = false;
// strip type info and find out is that preferred value
typeInfo = typeInfo.filter(function (type) {
isDefault = isDefault || type.name === 'PREF';
return type.name !== 'PREF';
});
typeInfo = typeInfo.reduce(function (p, c) {
p[c.name] = c.value;
return p;
}, fieldTypeDefaults[fieldName] || {});
context.currentCard[fieldName] = context.currentCard[fieldName] || [];
context.currentCard[fieldName].push({
isDefault: isDefault,
valueInfo: typeInfo,
value: valueFormatter ? valueFormatter(fieldValue) : fieldValue
});
}
function commaSeparatedLine(context, fieldValue, fieldName) {
context.currentCard[fieldName] = fieldValue.split(',');
}
function dateLine(context, fieldValue, fieldName) {
// if value is in "19531015T231000Z" format strip time field and use date value.
fieldValue = fieldValue.length === 16 ? fieldValue.substr(0, 8) : fieldValue;
var dateValue;
if (fieldValue.length === 8) { // "19960415" format ?
dateValue = new Date(fieldValue.substr(0, 4), fieldValue.substr(4, 2), fieldValue.substr(6, 2));
} else {
// last chance to try as date.
dateValue = new Date(fieldValue);
}
if (!dateValue || isNaN(dateValue.getDate())) {
dateValue = null;
context.error('invalid date format ' + fieldValue);
}
context.currentCard[fieldName] = dateValue && dateValue.toJSON(); // always return the ISO date format
}
function structured(fields) {
return function (context, fieldValue, fieldName) {
var values = fieldValue.split(';');
context.currentCard[fieldName] = fields.reduce(function (p, c, i) {
p[c] = values[i] || '';
return p;
}, {});
}
}
function addressLine(context, fieldValue, fieldName, typeInfo) {
typedLine(context, fieldValue, fieldName, typeInfo, function (value) {
var names = value.split(';');
return {
// ADR field sequence
postOfficeBox: names[0],
number: names[1],
street: names[2] || '',
city: names[3] || '',
region: names[4] || '',
postalCode: names[5] || '',
country: names[6] || ''
};
});
}
function noop() {
}
function endCard(context) {
// store card in context and create a new card.
context.cards.push(context.currentCard);
context.currentCard = {};
}
var fieldParsers = {
"BEGIN": noop,
"VERSION": noop,
"N": structured(['surname', 'name', 'additionalName', 'prefix', 'suffix']),
"TITLE": singleLine,
"TEL": typedLine,
"EMAIL": typedLine,
"ADR": addressLine,
"NOTE": singleLine,
"NICKNAME": commaSeparatedLine,
"BDAY": dateLine,
"URL": singleLine,
"CATEGORIES": commaSeparatedLine,
"END": endCard,
"FN": singleLine,
"ORG": singleLine,
"UID": singleLine,
"PHOTO": singleLine
};
function feedData(context) {
for (var i = 0; i < context.data.length; i++) {
var line = removeWeirdItemPrefix(context.data[i]);
var pairs = line.split(':'),
fieldName = pairs[0],
fieldTypeInfo = [],
fieldValue = pairs.slice(1).join(':');
// is additional type info provided ?
if (fieldName.indexOf(';') >= 0 && line.indexOf(';') < line.indexOf(':')) {
var typeInfo = fieldName.split(';');
fieldName = typeInfo[0];
fieldTypeInfo = typeInfo.slice(1).map(function (type) {
var info = type.split('=');
return {
name: info[0].toLowerCase(),
value: info[1].replace(/"(.*)"/, '$1')
}
});
}
// ensure fieldType is in upper case
fieldName = fieldName.toUpperCase();
var fieldHandler = fieldParsers[fieldName];
if (fieldHandler) {
fieldHandler(context, fieldValue, lookupField(context, fieldName), fieldTypeInfo);
} else if (fieldName.substring(0, 2) != 'X-') {
// ignore X- prefixed extension fields.
context.info('unknown field ' + fieldName + ' with value ' + fieldValue)
}
}
}
function parse(data) {
var lines = data
// replace escaped new lines
.replace(/\n\s{1}/g, '')
// split if a character is directly after a newline
.split(/\r\n(?=\S)|\r(?=\S)|\n(?=\S)/);
var context = {
info: function (desc) {
console.info(desc);
},
error: function (err) {
console.error(err);
},
data: lines,
currentCard: {},
cards: []
};
feedData(context);
return context.cards;
}
return {
parse: parse
};
})();