1.0.0 • Published 1 year ago

vcard-xml v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

vcard-xml - XML VCard parsing and writing

This library should simplify handling VCard XML documents.

This README currently only describes simple usage of this library.

Important Notices

This library requires @xmldom/xmldom as a peer-dependency only in node.js environments! JavaScript browser environments always have a DOM implementation on board, therefore @xmldom/xmldom is not needed there.

Installation

Requires node.js and npm.

npm install vcard-xml

Usage

VCard creation

import { 
  VCard,
  Gender,
  KeyId,

  XMLCompat, 
  getNodeImpl,
  getWebImpl,
} from 'vcard-xml';

// if xmldom interface is available (e.g. on web browsers)
XMLCompat.initialize(getNodeImpl());
// if xmldom interface is NOT available (e.g. on node environments)
// also don't forget to install required peer dependency @xmldom/xmldom
XMLCompat.initialize(getWebImpl());

const vcard = new VCard()
  .addFullName('Alice Smith')
  .addFirstname('Alice')
  .addLastname('Smith')
  .addNamePrefix('Dr.')
  .addNameSuffix('MSc.')
  .addBirthday(new Date(Date.UTC(1990, 2, 3)))
  .addGender(Gender.OTHER)
  .addTelephone('+436641234567')
  .addEmail('info@dec112.at')
  .addStreet('Example Street 3')
  .addCode('1234')
  .addLocality('Brunnenthal')
  .addRegion('Upper Austria')
  .addCountry('Austria');

if (!vcard)
  throw 'Something is wrong with this vcard';

const xmlObj = vcard.toXML();
console.log(vcard.toXMLString(vcard));

VCard parsing

import { 
  VCard,
  Gender,
  KeyId,

  XMLCompat, 
  getNodeImpl,
  getWebImpl,
} from 'vcard-xml';

// if xmldom interface is available (e.g. on web browsers)
XMLCompat.initialize(getNodeImpl());
// if xmldom interface is NOT available (e.g. on node environments)
// also don't forget to install required peer dependency @xmldom/xmldom
XMLCompat.initialize(getWebImpl());

const parsed = VCard.fromXML('<xml...>');

if (!parsed || !parsed.simple)
  throw 'Something is wrong with this vcard';

console.log(vcard.fullName);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Distributed under the MIT License. See LICENSE for more information.

1.0.0

1 year ago

0.1.0

1 year ago