UNPKG

8.34 kBJavaScriptView Raw
1/********************************************************************************
2 vCards-js, Eric J Nesser, November 2014
3********************************************************************************/
4/*jslint node: true */
5'use strict';
6
7/**
8 * Represents a contact that can be imported into Outlook, iOS, Mac OS, Android devices, and more
9 */
10var vCard = (function () {
11 /**
12 * Get photo object for storing photos in vCards
13 */
14 function getPhoto() {
15 return {
16 url: '',
17 mediaType: '',
18 base64: false,
19
20 /**
21 * Attach a photo from a URL
22 * @param {string} url URL where photo can be found
23 * @param {string} mediaType Media type of photo (JPEG, PNG, GIF)
24 */
25 attachFromUrl: function(url, mediaType) {
26 this.url = url;
27 this.mediaType = mediaType;
28 this.base64 = false;
29 },
30
31 /**
32 * Embed a photo from a file using base-64 encoding (not implemented yet)
33 * @param {string} filename
34 */
35 embedFromFile: function(fileLocation) {
36 var fs = require('fs');
37 var path = require('path');
38 this.mediaType = path.extname(fileLocation).toUpperCase().replace(/\./g, "");
39 var imgData = fs.readFileSync(fileLocation);
40 this.url = imgData.toString('base64');
41 this.base64 = true;
42 },
43
44 /**
45 * Embed a photo from a base-64 string
46 * @param {string} base64String
47 */
48 embedFromString: function(base64String, mediaType) {
49 this.mediaType = mediaType;
50 this.url = base64String;
51 this.base64 = true;
52 }
53 };
54 }
55
56 /**
57 * Get a mailing address to attach to a vCard.
58 */
59 function getMailingAddress() {
60 return {
61 /**
62 * Represents the actual text that should be put on the mailing label when delivering a physical package
63 * @type {String}
64 */
65 label: '',
66
67 /**
68 * Street address
69 * @type {String}
70 */
71 street: '',
72
73 /**
74 * City
75 * @type {String}
76 */
77 city: '',
78
79 /**
80 * State or province
81 * @type {String}
82 */
83 stateProvince: '',
84
85 /**
86 * Postal code
87 * @type {String}
88 */
89 postalCode: '',
90
91 /**
92 * Country or region
93 * @type {String}
94 */
95 countryRegion: ''
96 };
97 }
98
99 /**
100 * Get social media URLs
101 * @return {object} Social media URL hash group
102 */
103 function getSocialUrls() {
104 return {
105 'facebook': '',
106 'linkedIn': '',
107 'twitter': '',
108 'flickr': ''
109 };
110 }
111
112 /********************************************************************************
113 * Public interface for vCard
114 ********************************************************************************/
115 return {
116
117 /**
118 * Specifies a value that represents a persistent, globally unique identifier associated with the vCard
119 * @type {String}
120 */
121 uid: '',
122
123 /**
124 * Date of birth
125 * @type {Datetime}
126 */
127 birthday: '',
128
129 /**
130 * Cell phone number
131 * @type {String}
132 */
133 cellPhone: '',
134
135 /**
136 * Other cell phone number or pager
137 * @type {String}
138 */
139 pagerPhone: '',
140
141 /**
142 * The address for private electronic mail communication
143 * @type {String}
144 */
145 email: '',
146
147 /**
148 * The address for work-related electronic mail communication
149 * @type {String}
150 */
151 workEmail: '',
152
153 /**
154 * First name
155 * @type {String}
156 */
157 firstName: '',
158
159 /**
160 * Formatted name string associated with the vCard object (will automatically populate if not set)
161 * @type {String}
162 */
163 formattedName: '',
164
165 /**
166 * Gender.
167 * @type {String} Must be M or F for Male or Female
168 */
169 gender: '',
170
171 /**
172 * Home mailing address
173 * @type {object}
174 */
175 homeAddress: getMailingAddress(),
176
177 /**
178 * Home phone
179 * @type {String}
180 */
181 homePhone: '',
182
183 /**
184 * Home facsimile
185 * @type {String}
186 */
187 homeFax: '',
188
189 /**
190 * Last name
191 * @type {String}
192 */
193 lastName: '',
194
195 /**
196 * Logo
197 * @type {object}
198 */
199 logo: getPhoto(),
200
201 /**
202 * Middle name
203 * @type {String}
204 */
205 middleName: '',
206
207 /**
208 * Prefix for individual's name
209 * @type {String}
210 */
211 namePrefix: '',
212
213 /**
214 * Suffix for individual's name
215 * @type {String}
216 */
217 nameSuffix: '',
218
219 /**
220 * Nickname of individual
221 * @type {String}
222 */
223 nickname: '',
224
225 /**
226 * Specifies supplemental information or a comment that is associated with the vCard
227 * @type {String}
228 */
229 note: '',
230
231 /**
232 * The name and optionally the unit(s) of the organization associated with the vCard object
233 * @type {String}
234 */
235 organization: '',
236
237 /**
238 * Individual's photo
239 * @type {object}
240 */
241 photo: getPhoto(),
242
243 /**
244 * The role, occupation, or business category of the vCard object within an organization
245 * @type {String}
246 */
247 role: '',
248
249 /**
250 * Social URLs attached to the vCard object (ex: Facebook, Twitter, LinkedIn)
251 * @type {String}
252 */
253 socialUrls: getSocialUrls(),
254
255 /**
256 * A URL that can be used to get the latest version of this vCard
257 * @type {String}
258 */
259 source: '',
260
261 /**
262 * Specifies the job title, functional position or function of the individual within an organization
263 * @type {String}
264 */
265 title: '',
266
267 /**
268 * URL pointing to a website that represents the person in some way
269 * @type {String}
270 */
271 url: '',
272
273 /**
274 * URL pointing to a website that represents the person's work in some way
275 * @type {String}
276 */
277 workUrl: '',
278
279 /**
280 * Work mailing address
281 * @type {object}
282 */
283 workAddress: getMailingAddress(),
284
285 /**
286 * Work phone
287 * @type {String}
288 */
289 workPhone: '',
290
291 /**
292 * Work facsimile
293 * @type {String}
294 */
295 workFax: '',
296
297 /**
298 * vCard version
299 * @type {String}
300 */
301 version: '3.0',
302
303 /**
304 * Get major version of the vCard format
305 * @return {integer}
306 */
307 getMajorVersion: function() {
308 var majorVersionString = this.version ? this.version.split('.')[0] : '4';
309 if (!isNaN(majorVersionString)) {
310 return parseInt(majorVersionString);
311 }
312 return 4;
313 },
314
315 /**
316 * Get formatted vCard
317 * @return {String} Formatted vCard in VCF format
318 */
319 getFormattedString: function() {
320 var vCardFormatter = require('./lib/vCardFormatter');
321 return vCardFormatter.getFormattedString(this);
322 },
323
324 /**
325 * Save formatted vCard to file
326 * @param {String} filename
327 */
328 saveToFile: function(filename) {
329 var vCardFormatter = require('./lib/vCardFormatter');
330 var contents = vCardFormatter.getFormattedString(this);
331
332 var fs = require('fs');
333 fs.writeFileSync(filename, contents, { encoding: 'utf8' });
334 }
335 };
336});
337
338module.exports = vCard;