Contacts move between apps as files, and the files are messier than they look. A .vcf exported by one phone is vCard 2.1 with QUOTED-PRINTABLE names and a CHARSET parameter. Another is vCard 3.0 with folded lines and a Base64 photo. A newer one is vCard 4.0. Google and Outlook each export a CSV with their own columns. An importer has to read all of them and lose nothing on the way.

Then comes the second problem. After an import, the same person is in the list three times: once as 0555 12 34 56, once as +213 555123456, and once under a name spelled with different accents. Finding those is its own piece of work.

vcard_tools is a pure Dart package for both jobs: contact interchange and deduplication. It reads vCard 2.1, 3.0 and 4.0, writes 3.0 and 4.0, imports and exports Google and Outlook CSV, and finds and merges duplicate contacts.

What it does

vCard parsing for 2.1, 3.0 (RFC 2426) and 4.0 (RFC 6350). The list of details is the point of the package:

  • line unfolding (CRLF, LF or CR followed by a space or tab);
  • property groups (item1.TEL);
  • parameters, including 2.1 bare types (TEL;CELL;PREF:), quoted values, comma-separated lists and RFC 6868 caret encoding in 4.0;
  • QUOTED-PRINTABLE with soft line breaks;
  • CHARSET for UTF-8, US-ASCII, ISO-8859-1 and Windows-1256 (Arabic);
  • ENCODING=BASE64, ENCODING=b and data: URI photos;
  • single-pass unescaping, so \\n is a backslash followed by n, not a newline;
  • multiple cards per file;
  • a lenient mode that skips and reports malformed properties, and a strict mode that throws with a line number.

A typed model for N, FN, TEL, EMAIL, ADR, ORG, TITLE, BDAY (including partial dates such as --0203), NOTE, URL, PHOTO, UID, REV and CATEGORIES. Every other property, including X- extensions, is kept verbatim so that a round trip does not lose it.

vCard writing in 3.0 and 4.0, with CRLF line endings, correct escaping, and folding at 75 octets that never splits a UTF-8 multi-byte sequence.

CSV import and export for Google Contacts and Outlook. The header is detected automatically, with a comma, semicolon or tab delimiter, and quoting follows RFC 4180.

Duplicate detection. Clustering is union-find over pluggable matchers:

  • Phones are compared by their full E.164 number. National numbers are resolved with a configurable default calling code, so 0555 12 34 56 matches +213 555123456. A last-N-digits comparison is available only if you opt in.
  • Emails are compared case-insensitively, with optional Gmail dot folding and +tag folding.
  • Names are compared fuzzily and diacritic-insensitively (Latin and Arabic) after sorting their tokens.
  • Each cluster gets a confidence score and the reasons for the match.

A merge helper that unions fields without creating duplicates.

How to use it

Add it from GitHub:

dependencies: vcard_tools: git: https://github.com/AbdeldjalilChougui/vcard_tools 

Parse a file and write it back as vCard 3.0:

import 'dart:io'; import 'package:vcard_tools/vcard_tools.dart'; void main() { // parseBytes honours per-property CHARSET parameters. final result = const VCardParser().parseBytes( File('contacts.vcf').readAsBytesSync(), ); for (final issue in result.issues) { print('skipped line ${issue.line}: ${issue.message}'); } final card = result.cards.first; print(card.displayName); print(card.telephones.map((t) => '${t.number} ${t.types}')); // Strict mode throws VCardFormatException (with .line) instead. // const VCardParser(strict: true).parse(text); final vcf = const VCardWriter(version: VCardVersion.v30) .writeAll(result.cards); File('out.vcf').writeAsStringSync(vcf); } 

You can also build cards in code:

final card = VCard( name: const StructuredName(family: 'Benali', given: 'Amine'), formattedName: 'Amine Benali', telephones: [VCardTelephone('+213 555 12 34 56', types: ['cell'], pref: 1)], emails: [VCardEmail('amine@example.com', types: ['home'])], birthday: const VCardDate(month: 2, day: 3), // no year extended: [VCardProperty('X-CUSTOM', 'kept as is')], ); print(const VCardWriter().write(card)); // vCard 4.0 

Converting a Google CSV into an Outlook CSV takes two lines:

final cards = ContactsCsv.decode(File('google.csv').readAsStringSync()); final outlook = ContactsCsv.encode(cards, format: ContactsCsvFormat.outlook); 

Rfc4180Csv.decode and Rfc4180Csv.encode are also available for plain CSV.

Finding and merging duplicates

final finder = DuplicateFinder.standard( defaultCountryCode: '213', // Algeria; national numbers use trunk prefix 0 foldGmailDots: true, // optional ); final clusters = finder.find(cards); for (final cluster in clusters) { print('${cluster.indices} ${cluster.confidence.toStringAsFixed(2)}'); for (final reason in cluster.reasons) { print(' ${reason.matcher}: ${reason.detail}'); } } const merger = VCardMerger( phoneNormalizer: PhoneNormalizer(defaultCountryCode: '213'), ); final merged = merger.merge(clusters.first.cards); 

For finer control, pass your own matchers, for example a PhoneMatcher with another country's calling code, trunk prefix and international prefix, an EmailMatcher that folds +tag addresses, and a NameMatcher with a threshold, plus a minConfidence for the finder.

The scoring is documented. Pair confidence is the noisy-OR of the matching reasons (1 - Π(1 - c)). The defaults are 0.9 for a phone, 0.95 for an email and 0.6 × similarity for a name. A cluster's confidence is its weakest link on the maximum spanning tree. You can implement DuplicateMatcher to add your own evidence.

Because each cluster comes with its reasons, an app can show the user why two contacts were grouped ("same phone", "same email") before merging anything. A complete program is in example/main.dart.

Things to know

  • vCard 2.1 is read-only. The writer produces 3.0 or 4.0.
  • Four charsets. Supported charsets are UTF-8, US-ASCII, ISO-8859-1 and Windows-1256. Any other CHARSET is reported as an issue in lenient mode, or throws in strict mode.
  • Quoted-printable soft breaks. After a soft line break (= at the end of a line), leading whitespace on the next line is treated as indentation and removed.
  • Commas inside structured components (such as N:...;ing. jr,M.Sc.) are kept as literal text. On output they are written escaped (\,), so the displayed text is unchanged, but multi-value semantics inside a component are not preserved.
  • Groups and parameters are preserved on TEL, EMAIL, ADR, URL and on every unknown property. On single-valued properties such as FN, ORG and TITLE they are not kept. A second FN, N, ORG, TITLE, BDAY, PHOTO, UID or REV is kept as a raw extended property.
  • Writing 3.0 loses the numeric preference. A tel: URI is written as plain text, and any PREF becomes TYPE=PREF.
  • Outlook CSV has fixed columns. Phones that do not fit a free column are dropped on export, and only the first three emails and one web page are written.
  • Phone normalisation does not use number-plan metadata. It relies on the configured calling code, trunk prefix, international prefix and optional national number lengths. It does not validate that a number exists.
  • Fuzzy name matching is bounded. A fuzzy name comparison runs only on pairs that share at least one exact normalised name token, and blocking buckets larger than maxBucketSize (2000 by default) are skipped.
  • About the test data. The Android, iOS, Google and Outlook test fixtures follow those exporters' formats, and the contact data in them is invented. The fixtures also include the example vCards from RFC 2426 section 7 and RFC 6350 section 8, copied verbatim.

The README lists its data sources too. The Windows-1256 table comes from the Unicode Consortium mapping file, cross-checked against GNU libc iconv. The Latin diacritic folding table was generated from the canonical decompositions in the Unicode Character Database 16.0.0. The Google and Outlook CSV column names were checked against those products' published headers and field lists.

Why I built it

The README describes two problems that usually get solved separately, and badly: reading every flavour of contact file, and cleaning up afterwards.

For reading, the failures are in the old formats. vCard 2.1 files with QUOTED-PRINTABLE and Windows-1256 names still exist, and a parser that only knows 3.0 turns those names into noise. A parser that drops X- properties silently changes the user's data on a round trip. So the parser covers the three versions, keeps unknown properties verbatim, and reports what it skipped with a line number instead of failing the whole file.

For duplicates, I wanted matching that can explain itself. Comparing the last digits of two phone numbers is easy and produces false matches, so the default compares full E.164 numbers and the shortcut is opt-in. Every cluster carries a confidence and its reasons, and the merge is a separate step, so the decision to merge stays with the app and its user.

Get the code

The source, the example program, the fixtures and the tests are on GitHub: https://github.com/AbdeldjalilChougui/vcard_tools. It is MIT licensed. Issues and pull requests are welcome.

Originally published at abdeldjalilchougui.biz.