Skip to content

fix(vcf): keep every phone, email and name when converting to CSV - #709

Open
jayhemnani9910 wants to merge 2 commits into
C4illin:mainfrom
jayhemnani9910:fix/vcf-multiple-values
Open

jayhemnani9910 wants to merge 2 commits into
C4illin:mainfrom
jayhemnani9910:fix/vcf-multiple-values

Conversation

@jayhemnani9910

@jayhemnani9910 jayhemnani9910 commented Nov 14, 2026 •

Copy link
Copy Markdown

Converting a contacts export to CSV loses data. With three cards in the shapes iPhone, Google and Android contacts export write (contacts.vcf.txt):

before:
"Appleseed","John","John Appleseed","Acme\, Inc.","+1 (555) 987-6543",""
"Souza","Ana","Ana Souza","","+55 11 91234-5678","ana@work.example"
"","","","","+49 30 1234567",""

after:
"Appleseed","John","John Appleseed","Acme, Inc.","john@example.com","+1 (555) 123-4567; +1 (555) 987-6543"
"Souza","Ana","Ana Souza","","ana@home.example; ana@work.example","+55 11 91234-5678"
"Müller","Jürgen","Jürgen Müller","","","+49 170 1234567; +49 30 1234567"
  • Each TEL and EMAIL overwrote the previous one, so only the last of each was kept. They are now joined with ; .
  • item1.EMAIL (iPhone writes custom-labelled fields with a group prefix) didn't match startsWith("EMAIL"). The property name is now read without the group and parameters.
  • N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:... (Android's 2.1 export for any non-ASCII name) didn't match key === "N", and quoted-printable values weren't decoded. Both are handled now, including = soft line breaks on quoted-printable lines.
  • Long values that iPhone and Google exports fold onto a second line (starting with a space) were cut at the fold. Folded lines are joined now.
  • \,, \;, \\ and \n escapes are undone, and N/ORG are split only on unescaped ; (N:Doe\;Smith;John is Last Doe;Smith, First John).
  • Quoted-printable values are decoded in their CHARSET (UTF-8 if none), and a bare QUOTED-PRINTABLE parameter (vCard 2.1) is accepted too.

Several phones or emails go into one cell, joined with ; , to keep one row per contact with the same columns. If you'd rather have extra columns (Phone 2, ...) I can change that.

Four new cases in tests/converters/vcf.test.ts fail before the change; one also has a base64 PHOTO line ending in == next to a TEL, to make sure only quoted-printable lines are joined. bun test (non-integration): 464 passed. The 5 failures (archive route, production info, converter discovery) fail the same way on main here. eslint, prettier --check and tsc are clean for the changed files.


Summary by cubic

Fixes VCF to CSV conversion dropping data when contacts have multiple phones, emails, or non-ASCII names.

  • Repeated TEL and EMAIL fields are now joined with ; instead of overwriting the previous value.
  • Handles iPhone grouped fields like item1.EMAIL, folded continuation lines, and Android 2.1 style CHARSET/QUOTED-PRINTABLE values with charset decoding, including quoted parameter values like ENCODING="QUOTED-PRINTABLE".
  • Undoes \,, \;, \\, and \n escapes and splits N/ORG only on unescaped semicolons; fields are decoded after splitting so an encoded =3B isn't taken as a separator.

One row per contact with the same columns as before; multiple phone numbers or emails share a cell.

Written for commit 352715f. Summary will update on new commits.

View guided diff

Each TEL or EMAIL overwrote the previous one, grouped properties like
item1.EMAIL (iPhone) were skipped, N and FN with parameters (Android
2.1 exports) were dropped, quoted-printable values weren't decoded,
folded long lines were cut at the fold, and \, \; escapes were left
in or split on. Unfold lines, read the property name without group and
parameters, join repeated phones and emails with "; ", decode
quoted-printable values in their CHARSET, split N and ORG only on
unescaped semicolons and undo text escapes.
@github-actions github-actions Bot added the Fix label Nov 14, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/converters/vcf.ts">

<violation number="1" location="src/converters/vcf.ts:17">
P2: `splitFields` treats a semicolon as escaped whenever its immediate predecessor is `\`, even when that backslash is itself escaped. For `N:Doe\\;Smith;John;;;`, this shifts `Smith` out of the first-name field; count consecutive preceding backslashes before splitting.</violation>

<violation number="2" location="src/converters/vcf.ts:21">
P2: Quoted vCard parameter values are not normalized, so `ENCODING="QUOTED-PRINTABLE"` remains undecoded and quoted non-UTF-8 charsets corrupt decoded names. Normalize optional quotes before matching encoding and charset parameters.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/converters/vcf.test.ts Resolved
Comment thread src/converters/vcf.ts

// vCard 2.1 parameters: "ENCODING=QUOTED-PRINTABLE" or a bare "QUOTED-PRINTABLE"
const isQuotedPrintable = (params: string[]) =>
params.some((param) => /^(ENCODING=)?QUOTED-PRINTABLE$/i.test(param));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Quoted vCard parameter values are not normalized, so ENCODING="QUOTED-PRINTABLE" remains undecoded and quoted non-UTF-8 charsets corrupt decoded names. Normalize optional quotes before matching encoding and charset parameters.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/converters/vcf.ts, line 21:

<comment>Quoted vCard parameter values are not normalized, so `ENCODING="QUOTED-PRINTABLE"` remains undecoded and quoted non-UTF-8 charsets corrupt decoded names. Normalize optional quotes before matching encoding and charset parameters.</comment>

<file context>
@@ -9,6 +9,32 @@ export const properties = {
+
+// vCard 2.1 parameters: "ENCODING=QUOTED-PRINTABLE" or a bare "QUOTED-PRINTABLE"
+const isQuotedPrintable = (params: string[]) =>
+  params.some((param) => /^(ENCODING=)?QUOTED-PRINTABLE$/i.test(param));
+
+// vCard 2.1 exports (Android) write non-ASCII values as quoted-printable bytes in CHARSET
</file context>

Comment thread src/converters/vcf.ts Outdated
value.replace(/\\([,;\\nN])/g, (_, c) => (c === "n" || c === "N" ? "\n" : c));

// Split a structured value (N, ORG) on the ";" separators, not on escaped "\;"
const splitFields = (value: string) => value.split(/(?<!\\);/);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: splitFields treats a semicolon as escaped whenever its immediate predecessor is \, even when that backslash is itself escaped. For N:Doe\\;Smith;John;;;, this shifts Smith out of the first-name field; count consecutive preceding backslashes before splitting.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/converters/vcf.ts, line 17:

<comment>`splitFields` treats a semicolon as escaped whenever its immediate predecessor is `\`, even when that backslash is itself escaped. For `N:Doe\\;Smith;John;;;`, this shifts `Smith` out of the first-name field; count consecutive preceding backslashes before splitting.</comment>

<file context>
@@ -9,6 +9,32 @@ export const properties = {
+  value.replace(/\\([,;\\nN])/g, (_, c) => (c === "n" || c === "N" ? "\n" : c));
+
+// Split a structured value (N, ORG) on the ";" separators, not on escaped "\;"
+const splitFields = (value: string) => value.split(/(?<!\\);/);
+
+// vCard 2.1 parameters: "ENCODING=QUOTED-PRINTABLE" or a bare "QUOTED-PRINTABLE"
</file context>
Suggested change
const splitFields = (value: string) => value.split(/(?<!\\);/);
const splitFields = (value: string) => {
const fields = [""];
let backslashes = 0;
for (const char of value) {
if (char === "\\") {
fields[fields.length - 1] += char;
backslashes++;
} else if (char === ";" && backslashes % 2 === 0) {
fields.push("");
backslashes = 0;
} else {
fields[fields.length - 1] += char;
backslashes = 0;
}
}
return fields;
};

Comment thread src/converters/vcf.ts Resolved
@C4illin

C4illin commented Nov 15, 2026

Copy link
Copy Markdown
Owner

looks good! just check the ai feedback

Decode each field after splitting, so an encoded "=3B" stays in the
field instead of acting as a separator. Split on a semicolon after an
escaped backslash ("\;"), accept quoted parameter values like
ENCODING="QUOTED-PRINTABLE", and test that repeated emails are joined.
@jayhemnani9910

jayhemnani9910 commented Nov 16, 2026 •

Copy link
Copy Markdown
Author

Thanks! I went through the review comments, all fixed in 352715f:

  • Second email in the test: added a second EMAIL to the iPhone-style card, and the test checks both are joined with ; .
  • Escaped backslash before a separator: splitFields now only skips a ; that has an odd number of backslashes before it, so N:Doe\\;John gives Last Doe\, First John.
  • Quoted parameter values: quotes are dropped from parameters before matching, so ENCODING="QUOTED-PRINTABLE" and a quoted CHARSET work.
  • Decoding before splitting: N and ORG are now split first and each field is decoded after, so =3B stays a literal ;.

New test for the last three fails before and passes after. bun run test: 465 pass, the same 5 failures as on main (archive route, production info, converter discovery). eslint, prettier and tsc are clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants