Skip to content

Commit

Permalink
fix(formatter): use default value for telephone (#339)
Browse files Browse the repository at this point in the history
fixes: #321
  • Loading branch information
DanielHabenicht authored Sep 6, 2020
1 parent f4e9198 commit e824ca1
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 32 deletions.
102 changes: 84 additions & 18 deletions projects/ngx-vcard/src/lib/ngx-vcard.formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('NgxVcardFormatter', () => {
name: { firstNames: 'John', lastNames: 'Doe' },
organization: 'Example.com Inc.',
title: 'Imaginary test person',
email: [{ value: '[email protected]', param: { type: ['work', 'cell'] } }]
email: [{ value: '[email protected]', param: { type: ['work', 'cell'] } }],
};
expect(VCardFormatter.getVCardAsString(vCard)).toEqual(
`BEGIN:VCARD
Expand Down Expand Up @@ -47,10 +47,19 @@ END:VCARD
organization: 'Bubba Gump Shrimp Co.',
title: 'Shrimp Man',
email: ['[email protected]'],
photo: { value: 'http://www.example.com/dir_photos/my_photo.gif', param: { mediatype: 'image/gif' } },
photo: {
value: 'http://www.example.com/dir_photos/my_photo.gif',
param: { mediatype: 'image/gif' },
},
telephone: [
{ value: 'tel:+1-111-555-1212', param: { type: ['work', 'voice'], value: 'uri' } },
{ value: 'tel:+1-404-555-1212', param: { type: ['home', 'voice'], value: 'uri' } }
{
value: 'tel:+1-111-555-1212',
param: { type: ['work', 'voice'], value: 'uri' },
},
{
value: 'tel:+1-404-555-1212',
param: { type: ['home', 'voice'], value: 'uri' },
},
],
address: [
{
Expand All @@ -60,9 +69,9 @@ END:VCARD
locality: 'Baytown',
postalCode: '30314',
region: 'LA',
countryName: 'United States of America'
countryName: 'United States of America',
},
param: { type: ['work'], pref: 1 }
param: { type: ['work'], pref: 1 },
},
{
value: {
Expand All @@ -71,12 +80,12 @@ END:VCARD
locality: 'Baytown',
postalCode: '30314',
region: 'LA',
countryName: 'United States of America'
countryName: 'United States of America',
},
param: { type: ['home'] }
}
param: { type: ['home'] },
},
],
rev: '20080424T195243Z'
rev: '20080424T195243Z',
};
expect(VCardFormatter.getVCardAsString(vCard)).toEqual(
`BEGIN:VCARD
Expand All @@ -97,15 +106,63 @@ END:VCARD
);
});

it('use default values', () => {
const vCard: VCard = {
version: '3.0',
name: { firstNames: 'John', lastNames: 'Doe' },
telephone: ['+1234567890'],
};
expect(VCardFormatter.getVCardAsString(vCard)).toEqual(
`BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John;;;
TEL;TYPE=voice:+1234567890
END:VCARD
`
);
});

it('overwrite default values', () => {
const vCard: VCard = {
version: '3.0',
name: { firstNames: 'John', lastNames: 'Doe' },
telephone: [
{
value: '+1234567890',
param: {
type: 'work',
},
},
],
};
expect(VCardFormatter.getVCardAsString(vCard)).toEqual(
`BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John;;;
TEL;TYPE=work:+1234567890
END:VCARD
`
);
});

xit('sample test - 3', () => {
const vCard: VCard = {
version: '4.0',
formattedName: { firstNames: 'Simon', lastNames: 'Perreault' },
name: { firstNames: 'Simon', lastNames: 'Perreault', nameSuffix: 'ing. jr,M.Sc.' },
name: {
firstNames: 'Simon',
lastNames: 'Perreault',
nameSuffix: 'ing. jr,M.Sc.',
},
birthday: new Date(2003, 2),
anniversary: new Date(2009, 8, 8, 14, 30),
gender: { sex: 'M' },
language: [{ value: 'fr', param: { pref: 1 } }, { value: 'en', param: { pref: 2 } }],
language: [
{ value: 'fr', param: { pref: 1 } },
{ value: 'en', param: { pref: 2 } },
],
organization: { value: 'Viagenie', param: { type: ['work'] } },
address: [
{
Expand All @@ -115,19 +172,28 @@ END:VCARD
locality: 'Quebec',
postalCode: 'G1V 2M2',
region: 'QC',
countryName: 'Canada'
countryName: 'Canada',
},
param: { type: ['work'] }
}
param: { type: ['work'] },
},
],
telephone: [
{ value: 'tel:+1-418-656-9254', param: { value: 'uri', type: ['work', 'voice'], pref: 1 } },
{ value: 'tel:+1-418-262-6501', param: { value: 'uri', type: ['work', 'cell', 'video', 'text'] } }
{
value: 'tel:+1-418-656-9254',
param: { value: 'uri', type: ['work', 'voice'], pref: 1 },
},
{
value: 'tel:+1-418-262-6501',
param: { value: 'uri', type: ['work', 'cell', 'video', 'text'] },
},
],

email: [{ value: '[email protected]', param: { type: 'work' } }],
geoPosition: '',
photo: { value: 'http://www.example.com/dir_photos/my_photo.gif', param: { mediatype: 'image/gif' } }
photo: {
value: 'http://www.example.com/dir_photos/my_photo.gif',
param: { mediatype: 'image/gif' },
},
};
expect(VCardFormatter.getVCardAsString(vCard)).toEqual(`BEGIN:VCARD
VERSION:4.0
Expand Down
32 changes: 18 additions & 14 deletions projects/ngx-vcard/src/lib/ngx-vcard.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
isPropertyWithParameters,
propertyToVCardString,
BasicPropertyParameters,
isPropertyWithParametersAddressValue
isPropertyWithParametersAddressValue,
} from './types/parameter/BasicPropertyParameters.type';
import { nl, e } from './helpers';

Expand Down Expand Up @@ -40,7 +40,7 @@ export class VCardFormatter {
nameArray = [vCard.name.firstNames, vCard.name.addtionalNames, vCard.name.lastNames];
}

formattedName = nameArray.filter(string => string != null).join(' ');
formattedName = nameArray.filter((string) => string != null).join(' ');

formattedVCardString += 'FN' + encodingPrefix + ':' + e(formattedName) + nl();

Expand All @@ -53,7 +53,7 @@ export class VCardFormatter {
e(vCard.name.firstNames),
e(vCard.name.addtionalNames),
e(vCard.name.namePrefix),
e(vCard.name.nameSuffix)
e(vCard.name.nameSuffix),
].join(';') +
nl();

Expand Down Expand Up @@ -86,7 +86,7 @@ export class VCardFormatter {
}

if (vCard.language) {
vCard.language.forEach(language => {
vCard.language.forEach((language) => {
if (isPropertyWithParameters(language)) {
formattedVCardString += 'LANG' + propertyToVCardString(language.param) + ':' + e(language.value) + nl();
} else {
Expand All @@ -105,7 +105,7 @@ export class VCardFormatter {
}

if (vCard.address) {
vCard.address.forEach(address => {
vCard.address.forEach((address) => {
if (isPropertyWithParametersAddressValue(address)) {
formattedVCardString +=
'ADR' +
Expand All @@ -119,18 +119,22 @@ export class VCardFormatter {
}

if (vCard.telephone) {
vCard.telephone.forEach(element => {
if (isPropertyWithParameters(element)) {
formattedVCardString +=
'TEL' + propertyToVCardString(element.param as BasicPropertyParameters) + ':' + e(element.value) + nl();
} else {
formattedVCardString += 'TEL:' + e(element) + nl();
vCard.telephone.forEach((element) => {
if (!isPropertyWithParameters(element)) {
element = {
value: element,
param: {
type: 'voice',
},
};
}
formattedVCardString +=
'TEL' + propertyToVCardString(element.param as BasicPropertyParameters) + ':' + e(element.value) + nl();
});
}

if (vCard.email) {
vCard.email.forEach(email => {
vCard.email.forEach((email) => {
if (isPropertyWithParameters(email)) {
formattedVCardString += 'EMAIL' + propertyToVCardString(email.param) + ':' + e(email.value) + nl();
} else {
Expand Down Expand Up @@ -160,7 +164,7 @@ export class VCardFormatter {
}

if (vCard.homeFax) {
vCard.homeFax.forEach(function(number) {
vCard.homeFax.forEach(function (number) {
if (+majorVersion >= 4) {
formattedVCardString += 'TEL;VALUE=uri;TYPE="fax,home":tel:' + e(number) + nl();
} else {
Expand All @@ -170,7 +174,7 @@ export class VCardFormatter {
}

if (vCard.workFax) {
vCard.workFax.forEach(function(number) {
vCard.workFax.forEach(function (number) {
if (+majorVersion >= 4) {
formattedVCardString += 'TEL;VALUE=uri;TYPE="fax,work":tel:' + e(number) + nl();
} else {
Expand Down

0 comments on commit e824ca1

Please sign in to comment.