Create vcards and format to strings to export in .vcf
or .vcard
files. This is an implementation of the version 4.0 vcard in Typescript according to RFC6350. This is an early version an many vcard fields or properties might not be supported. Check below for what's included.
npm install --save @covve/easy-vcard
Examples are written with Typescript in mind but you should be able to use it in nodejs projects as well. Below we construct a simple vcard and export it.
let vcard = new VCard();
vcard.setFullName('Johnny D. Doe-Smith')
.addFirstName('John')
.addLastName('Doe')
.addLastName('Smith')
.addPrefixName('Dr.')
.addNickname('Jonny')
.addPhone('+1 1221112', { pref: '1', type: 'home' })
.addEmail('jdoe@jdoecomp.co')
.addTitle('Senior Engineer')
.addOrganization('Jdoecomp co.', ['North Division']);
let formatter = new Formatter();
let formattedText = formatter.format(vcard);
\\ This should output the following
\\ "BEGIN:VCARD
\\ VERSION:4.0
\\ FN:Johnny D. Doe-Smith
\\ N:Doe,Smith;John;;Dr.;
\\ NICKNAME:Jonny
\\ TEL;PREF=1;TYPE=home:+1 1221112
\\ EMAIL:jdoe@jdoecomp.co
\\ TITLE:Senior Engineer
\\ ORG:Jdoecomp co.;North Division
\\ END:VCARD
\\
\\ which you can save to a filesystem as .vcf or encode it in a barcode or whatever
Note: IParams refers to a javascript object containing parameters used in certain vCard properties. Some methods support this, some don't. Refer to the RFC aforementioned or source/examples of this project for more details.
Set the FN property of the vcard. This is mandatory and unique. If it doesn't exist, one will be created from name components from the N property.
Add a first name, middle name, last name, honorific prefix or honorific suffix respectively in the N property. The N property is unique if it exists.
Add a nickname to a NICKNAME property.
Add a photo to a PHOTO property. data
can be either a link to a site where the photo is hosted or a base64 data representation.
addAddress(street: string, locality: string, region: string, postCode: string, country: string, params?: IParams): VCard
Add an address as an ADR property. Fields not available should be null or undefined.
Add a phone number to a TEL property.
Add an email to an EMAIL property.
Add a title in the list of the TITLE property.
Add a role in the list of the ROLE property.
Add an organization to an ORG property. Organization refers to the main name of the company and organizationUnits to second or more unit names.
Add a notes entry in a NOTE property.
Add a url entry in a URL property
Set the revision for this vcard.
Set the user id for this vcard.
Takes a VCard object as created above and formats it into a string. Note that a forceV3 argument is included, which if true, sets the VERSION property to 3.0 . This doesn't mean that this plugin supports 3.0 vcards or earlier, it's just a workaround to get your simple vcards read by older parsers found in various devices. Care should be taken using this as the card might not be readable by 3.0 or older parsers.
The following vCard properties are not yet included but might be in the future.
SOURCE, KIND, XML, BDAY, ANNIVERSARY, GENDER, IMPP, LANG, TZ, GEO,
LOGO, MEMBER, RELATED, CATEGORIES, PRODID, SOUND, CLIENTPIDMAP, KEY, FBURL, CALADRURI, CALURI
Feel free to submit PRs or open issues with improvements or bug fixes.
MIT