diff --git a/README.md b/README.md index 10570cc..077fc1c 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,13 @@ vCard.email = [ 'e.nesser@emailhost3.tld' ]; +//multiple custom labelled email entry +vCard.customEmails = [ + ['type' => 'Friend', 'email' => 'e.nesser@emailhost.tld'], + ['type' => 'Father', 'email' => 'e.nesser@emailhost2.tld'], + ['type' => 'Mother', 'email' => 'e.nesser@emailhost3.tld'] +]; + //multiple cellphone vCard.cellPhone = [ '312-555-1414', @@ -219,6 +226,13 @@ vCard.cellPhone = [ '312-555-1416' ]; +//multiple custom labelled phone entry +vCard.customPhones = [ + ['type' => 'Friend', 'phone' => '312-555-1417'], + ['type' => 'Father', 'phone' => '312-555-1418'], + ['type' => 'Mother', 'phone' => '312-555-1419'] +]; + ``` ### Apple AddressBook Extensions diff --git a/lib/vCardFormatter.js b/lib/vCardFormatter.js index 903eb8b..9e4a20f 100644 --- a/lib/vCardFormatter.js +++ b/lib/vCardFormatter.js @@ -218,6 +218,66 @@ ); } + if (vCard.customEmails) { + if(!Array.isArray(vCard.customEmails)){ + formattedVCardString += ''; + } + vCard.customEmails.forEach( + function(customEmails) { + if(!Array.isArray(customEmails)){ + formattedVCardString += ''; + } else { + customEmails.forEach(function(customEmail) { + let customType = ''; + let customEmailVal = e(customEmail.email); + if(!customEmail.type) { + formattedVCardString += ''; + } else { + if (majorVersion >= 4) { + customType = 'type='+e(customEmail.type); + } else if (majorVersion >= 3 && majorVersion < 4) { + customType = 'type='+e(customEmail.type); + } else { + customType = e(customEmail.type)+',INTERNET'; + } + formattedVCardString += 'EMAIL' + encodingPrefix + ';' + customType + ':' + customEmailVal + nl(); + } + }); + } + } + ); + } + + if (vCard.customAddresses) { + if(!Array.isArray(vCard.customAddresses)){ + formattedVCardString += ''; + } + vCard.customAddresses.forEach( + function(customAddresses) { + if(!Array.isArray(customAddresses)){ + formattedVCardString += ''; + } else { + customAddresses.forEach(function(customAddress) { + let customType = e(customAddress.type ?? 'HOME'); + const customTypeVal = customType.toString().toLocaleUpperCase(); + if(!customAddress.type) { + formattedVCardString += ''; + } else { + [{ + details: customAddress.address, + type: customTypeVal + }].forEach( + function(customAddress) { + formattedVCardString += getFormattedAddress(encodingPrefix, customAddress); + } + ); + } + }); + } + } + ); + } + if (vCard.logo.url) { formattedVCardString += getFormattedPhoto('LOGO', vCard.logo.url, vCard.logo.mediaType, vCard.logo.base64); } @@ -256,6 +316,30 @@ ); } + if (vCard.customPhones) { + if(!Array.isArray(vCard.customPhones)){ + formattedVCardString += ''; + } + vCard.customPhones.forEach( + function(customPhones) { + if(!Array.isArray(customPhones)){ + formattedVCardString += ''; + } else { + customPhones.forEach(function(customPhone) { + let customType = ''; + let customPhoneVal = e(customPhone.phone); + if(!customPhone.type) { + formattedVCardString += ''; + } else { + customType = 'TYPE=' + e(customPhone.type); + formattedVCardString += 'TEL;' + customType + ':' + customPhoneVal + nl(); + } + }); + } + } + ); + } + if (vCard.homePhone) { if(!Array.isArray(vCard.homePhone)){ vCard.homePhone = [vCard.homePhone]; @@ -363,6 +447,30 @@ formattedVCardString += 'URL' + encodingPrefix + ':' + e(vCard.url) + nl(); } + if (vCard.customUrls) { + if(!Array.isArray(vCard.customUrls)){ + formattedVCardString += ''; + } + vCard.customUrls.forEach( + function(customUrls) { + if(!Array.isArray(customUrls)){ + formattedVCardString += ''; + } else { + customUrls.forEach(function(customUrl) { + let customType = ''; + let customWebVal = e(customUrl.web); + if(!customUrl.type) { + formattedVCardString += ''; + } else { + customType = 'TYPE=' + e(customUrl.type); + formattedVCardString += 'URL;' + customType + encodingPrefix + ':' + customWebVal + nl(); + } + }); + } + } + ); + } + if (vCard.workUrl) { formattedVCardString += 'URL;type=WORK' + encodingPrefix + ':' + e(vCard.workUrl) + nl(); } diff --git a/package-lock.json b/package-lock.json index 98fe143..5cec8f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,244 @@ { "name": "vcards-js", - "version": "2.9.0", - "lockfileVersion": 1, + "version": "2.10.0", + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "vcards-js", + "version": "2.10.0", + "license": "MIT", + "devDependencies": { + "mocha": "~5.2.0" + } + }, + "node_modules/balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, + "node_modules/commander": { + "version": "2.15.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", + "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==", + "dev": true + }, + "node_modules/concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "node_modules/debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/diff": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true, + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "node_modules/glob": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + } + }, + "node_modules/growl": { + "version": "1.10.5", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", + "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", + "dev": true, + "engines": { + "node": ">=4.x" + } + }, + "node_modules/has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true, + "engines": { + "node": ">=4" + } + }, + "node_modules/he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "dev": true, + "bin": { + "he": "bin/he" + } + }, + "node_modules/inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "dependencies": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "node_modules/inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "node_modules/minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "node_modules/mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "deprecated": "Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)", + "dev": true, + "dependencies": { + "minimist": "0.0.8" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, + "node_modules/mocha": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz", + "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==", + "dev": true, + "dependencies": { + "browser-stdout": "1.3.1", + "commander": "2.15.1", + "debug": "3.1.0", + "diff": "3.5.0", + "escape-string-regexp": "1.0.5", + "glob": "7.1.2", + "growl": "1.10.5", + "he": "1.1.1", + "minimatch": "3.0.4", + "mkdirp": "0.5.1", + "supports-color": "5.4.0" + }, + "bin": { + "_mocha": "bin/_mocha", + "mocha": "bin/mocha" + }, + "engines": { + "node": ">= 4.0.0" + } + }, + "node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/supports-color": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", + "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", + "dev": true, + "dependencies": { + "has-flag": "^3.0.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + }, "dependencies": { "balanced-match": { "version": "1.0.0", diff --git a/test/testCard.vcf b/test/testCard.vcf index 0769ff0..e747a7d 100644 --- a/test/testCard.vcf +++ b/test/testCard.vcf @@ -9,10 +9,18 @@ BDAY:20181201 ANNIVERSARY:20181201 EMAIL;CHARSET=UTF-8;type=HOME,INTERNET:john.doe@testmail EMAIL;CHARSET=UTF-8;type=WORK,INTERNET:john.doe@workmail +EMAIL;CHARSET=UTF-8;type=X-Father:john@father.email +EMAIL;CHARSET=UTF-8;type=X-Mother:john@mother.email +EMAIL;CHARSET=UTF-8;type=X-Sister:john@sister.email +EMAIL;CHARSET=UTF-8;type=X-Friend:john@friend.email LOGO;TYPE=png:https://testurl PHOTO;TYPE=png:https://testurl TEL;TYPE=CELL:12345678900 TEL;TYPE=PAGER:312-555-1515 +TEL;TYPE=X-Father:12345678901 +TEL;TYPE=X-Mother:12345678902 +TEL;TYPE=X-Sister:12345678903 +TEL;TYPE=X-Friend:12345678904 TEL;TYPE=HOME,VOICE:312-555-1313 TEL;TYPE=WORK,VOICE:312-555-1212 TEL;TYPE=HOME,FAX:312-555-1616 @@ -25,6 +33,10 @@ TITLE;CHARSET=UTF-8:Crash Test Dummy ROLE;CHARSET=UTF-8:Crash Testing ORG;CHARSET=UTF-8:ACME Corporation URL;CHARSET=UTF-8:http://johndoe +URL;TYPE=X-Father;CHARSET=UTF-8:http://john.doe/father +URL;TYPE=X-Mother;CHARSET=UTF-8:http://john.doe/mother +URL;TYPE=X-Sister;CHARSET=UTF-8:http://john.doe/sister +URL;TYPE=X-Friend;CHARSET=UTF-8:http://john.doe/friend URL;type=WORK;CHARSET=UTF-8:http://acemecompany/johndoe NOTE;CHARSET=UTF-8:John Doe's \nnotes\;\, X-SOCIALPROFILE;TYPE=facebook:https://facebook/johndoe @@ -33,5 +45,5 @@ X-SOCIALPROFILE;TYPE=twitter:https://twitter/johndoe X-SOCIALPROFILE;TYPE=flickr:https://flickr/johndoe X-SOCIALPROFILE;TYPE=custom:https://custom/johndoe SOURCE;CHARSET=UTF-8:http://sourceurl -REV:2019-01-04T20:18:50.127Z +REV:2023-01-23T14:02:09.232Z END:VCARD diff --git a/test/tests.js b/test/tests.js index 9e1f1d5..e19ab46 100644 --- a/test/tests.js +++ b/test/tests.js @@ -52,6 +52,12 @@ describe('vCard', function() { testCard.homePhone = '312-555-1313'; testCard.cellPhone = '12345678900'; testCard.pagerPhone = '312-555-1515'; + testCard.customPhones = [ + [{'type': 'Father', 'phone': '12345678901'}], + [{'type': 'Mother', 'phone': '12345678902'}], + [{'type': 'Sister', 'phone': '12345678903'}], + [{'type': 'Friend', 'phone': '12345678904'}] + ]; testCard.homeFax = '312-555-1616'; testCard.workFax = '312-555-1717'; testCard.birthday = new Date(2018, 11, 1); @@ -60,8 +66,20 @@ describe('vCard', function() { testCard.role = 'Crash Testing'; testCard.email = 'john.doe@testmail'; testCard.workEmail = 'john.doe@workmail'; + testCard.customEmails = [ + [{'type': 'Father', 'email': 'john@father.email'}], + [{'type': 'Mother', 'email': 'john@mother.email'}], + [{'type': 'Sister', 'email': 'john@sister.email'}], + [{'type': 'Friend', 'email': 'john@friend.email'}] + ]; testCard.url = 'http://johndoe'; testCard.workUrl = 'http://acemecompany/johndoe'; + testCard.customUrls = [ + [{'type': 'Father', 'web': 'http://john.doe/father'}], + [{'type': 'Mother', 'web': 'http://john.doe/mother'}], + [{'type': 'Sister', 'web': 'http://john.doe/sister'}], + [{'type': 'Friend', 'web': 'http://john.doe/friend'}] + ]; testCard.homeAddress.label = 'Home Address'; testCard.homeAddress.street = '123 Main Street'; @@ -77,6 +95,35 @@ describe('vCard', function() { testCard.workAddress.postalCode = '54321'; testCard.workAddress.countryRegion = 'California Republic'; + testCard.customAddresses = [ + [ + { + 'type': 'House', + 'address': { + 'label': 'Home Address', + 'street': '123 Main Street', + 'city': 'Chicago', + 'stateProvince': 'IL', + 'postal': '12345', + 'countryRegion': 'United States of America' + } + }, + ], + [ + { + 'type': 'Job', + 'address': { + 'label': 'Work Address', + 'street': '123 Corporate Loop\nSuite 500', + 'city': 'Los Angeles', + 'stateProvince': 'CA', + 'postal': '54321', + 'countryRegion': 'California Republic' + } + } + ], + ]; + testCard.source = 'http://sourceurl'; testCard.note = 'John Doe\'s \nnotes;,';