Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vcard-creator

joaocarmo23.7kMIT0.7.2TypeScript support: included

A JavaScript vCard creator library for both node.js and the web

vcard, node, browser

readme

vcard-creator

npm version jest tests

A JavaScript vCard 3.0 creator library for both node.js and the web. It outputs the vCard text that should be saved as a *.vcf file.

Origin

This is based on jeroendesloovere's vCard for PHP.

Installation

pnpm add vcard-creator

# or

npm install vcard-creator

Usage

As an ESM module (web)

Load vcard-creator directly from skypack (CDN).

<script type="module">
  import VCard from 'https://cdn.skypack.dev/vcard-creator'
</script>

Demo available in codepen.

On the web (self-hosted)

It's exposed through the window global object as explained below.

index.html

<head>
  <script type="text/javascript" src="./js/vcard-creator.js"></script>
  <script type="text/javascript" src="./js/main.js"></script>
</head>

main.js

// Define a new vCard
var VCard = window.vcardcreator.default
var myVCard = new VCard()

// ...rest of the code

With a bundler / Node.js

With a bundler (e.g. webpack) or in Node.js you can just require / import it.

const VCard = require('vcard-creator').default

// Define a new vCard
const myVCard = new VCard()

Or...

import VCard from 'vcard-creator'

// Define a new vCard
const myVCard = new VCard()

Including an image

You need to provide the image already properly encoded (base64). Note that most applications will probably ignore a photo URL, even if it adheres to the specification.

// Example in Node.js

const fs = require('fs')
const VCard = require('vcard-creator').default

const imagePath = './path/to/my/assets/sample.jpg'
const image = fs.readFileSync(imagePath, { encoding: 'base64', flag: 'r' })

const vCard = new VCard()

vCard.addPhoto(image, 'JPEG')

Include the proper MIME type (defaults to JPEG).

[DEPRECATED] iCalendar format

For Apple devices that don't support the vcf file format, there is a workaround. Specify the format of the output as vcalendar and then save it with a ics file extension instead.

The trick is to create an iCalendar file with a vCard attached.

// Define a new vCard as 'vcalendar'
const myVCalendar = new VCard('vcalendar')

// ...or set it afterwards
const myOtherVCalendar = new VCard()
myOtherVCalendar.setFormat('vcalendar')

Example

import VCard from 'vcard-creator'

// Define a new vCard
const myVCard = new VCard()

// Some variables
const lastname = 'Desloovere'
const firstname = 'Jeroen'
const additional = ''
const prefix = ''
const suffix = ''

myVCard
  // Add personal data
  .addName(lastname, firstname, additional, prefix, suffix)
  // Add work data
  .addCompany('Siesqo')
  .addJobtitle('Web Developer')
  .addRole('Data Protection Officer')
  .addEmail('info@jeroendesloovere.be')
  .addPhoneNumber(1234121212, 'PREF;WORK')
  .addPhoneNumber(123456789, 'WORK')
  .addAddress(null, null, 'street', 'worktown', null, 'workpostcode', 'Belgium')
  .addSocial('https://twitter.com/desloovere_j', 'Twitter', 'desloovere_j')
  .addURL('http://www.jeroendesloovere.be')

console.log(myVCard.toString())

Output

BEGIN:VCARD
VERSION:3.0
REV:2017-08-31T17:00:15.850Z
N;CHARSET=utf-8:Desloovere;Jeroen;;;
FN;CHARSET=utf-8:Jeroen Desloovere
ORG;CHARSET=utf-8:Siesqo
TITLE;CHARSET=utf-8:Web Developer
ROLE;CHARSET=utf-8:Data Protection Officer
EMAIL;INTERNET:info@jeroendesloovere.be
TEL;PREF;WORK:1234121212
TEL;WORK:123456789
ADR;WORK;POSTAL;CHARSET=utf-8:name;extended;street;worktown;state;workpos
 tcode;Belgium
X-SOCIALPROFILE;type=Twitter;x-user=desloovere_j:https://twitter.com/desl
oovere_j
URL:http://www.jeroendesloovere.be
END:VCARD

Forking / Contributing

If you're interested in the development of this project, you can run some ready to use commands to compile and test your changes.

# Build
pnpm build

# Test
pnpm test:unit

pnpm test:functional

pnpm test:web-build

pnpm test:web-export

changelog

Changelog

0.6.0

Jan 1, 2023

  • Adds support for addNickname (#38) and addSocial (#39)

0.5.0

Aug 11, 2022

  • Enhances the types definitions
  • Reorganizes the files
  • Adds support for UID (#32)

0.4.2

Jan 9, 2022

  • Fixes the issue with the addPhoneNumber() method only accepting number as a parameter, making it impossible to pass leading zeros (#27)

0.4.1 (no release)

Jul 10, 2021

  • Updated the docs

0.4.1

Mar 25, 2021

  • Fixes the addMediaContent() method (#17 thanks to @Falklian)
  • Fixes the addMediaURL() method

0.4.0

Mar 21, 2021

  • Removed @deprecated from the toString() method
  • Added a custom error object VCardException
  • Removed the include argument from the addLogo() and addPhoto() methods
  • Removed unnecessary comments
  • Added the missing functionality to the addLogo() and addPhoto() methods
  • Added the missing functionality to the addLogoURL() and addPhotoURL() methods

0.3.4

Mar 21, 2021

  • Fixes the exported typings (#13) to have the same name as the distribution file

0.3.3

Jan 31, 2021

  • Fixes the exported typings (#13)

0.3.2

Jan 9, 2021

0.3.1

Jan 9, 2021

  • Updated the TypeScript declaration file's output to match the name of the bundle (#11)

0.3.0 (no release)

Nov 6, 2020

  • Updated the documentation to include information about using the module in the browser (ESM)
  • Updated the package.json to comply with ESM best practices
  • Updated the dependencies

0.3.0

Aug 9, 2020

  • Added support for iCalendar data files

0.2.0

Aug 8, 2020

  • Converted to TypeScript
  • Added unit and functional tests
  • Updated the documentation (#1)

0.1.0 (no release)

Aug 11, 2019

  • Updated the dependencies to resolve the security vulnerability CVE-2019-10744

0.1.0

Apr 1, 2019

  • Changed the named export to a default export, making it incompatible with previous versions [BREAKING]
  • Renamed the global object from vcard_creator to vcardcreator [BREAKING]
  • Renamed the constructor VCard [BREAKING]

0.0.6

Feb 8, 2019

  • Updated the dependencies to resolve the security vulnerability in lodash

0.0.5

Jun 1, 2018

  • Added tests for CLI, compiled for web (i.g. webpack) and not compiled (i.g. used from the global window object)
  • Updated the README.md

0.0.4

Apr 26, 2018

  • Exposed the library as a compiled bundle to avoid dependencies

0.0.3

Apr 25, 2018

  • Removed the index.js and exposed the module directly from the lib folder

0.0.2

Sep 1, 2017

  • Removed the transliteration dependency in order to be compatible with UglifyJS

0.0.1

Aug 31, 2017

  • Removed the locutus dependency
  • Fixed the folding of long lines into multiple lines

0.0.0

Aug 29, 2017

  • Initial release