Skip to content

Instantly share code, notes, and snippets.

@gitSambhal
Last active April 14, 2024 06:03
Show Gist options
  • Save gitSambhal/49899b8998e2c07e0f4dab6e118c41a0 to your computer and use it in GitHub Desktop.
Save gitSambhal/49899b8998e2c07e0f4dab6e118c41a0 to your computer and use it in GitHub Desktop.
# Compare and find missing contacts This script compares two VCF contact files, extracts the phone numbers, finds missing numbers between the two files, and filters the original contacts to only those that contain missing numbers. To run: - Install dependencies npm install - Run script node index.js - Output files written to /out folder - missin…
const fs = require('fs');
const vcf = require('vcf');
const vcardParser = require('vcard-parser');
// Utils
const writeToFile = (fileName, content) => {
fs.writeFileSync(fileName, content);
};
const cleanPhoneNumber = (num) => {
// Clean up phone number format
num = num?.replace('+91', '');
num = num?.replace(/ /g, '');
num = num?.replace(/-/g, '');
num = num?.replace(/^0/, '');
return num;
};
// Parse contacts
const getPhoneNumbers = (vcard) => {
const parsed = vcardParser.parse(vcard.toString());
// Get phone numbers
const telNumbers = parsed.tel ?? [];
let numbers = telNumbers.map((v) => v.value) ?? [];
// Clean numbers
const cleanNumbers = numbers.map(cleanPhoneNumber);
return cleanNumbers;
};
// Load contacts
const contacts1 = vcf.parse(fs.readFileSync('contacts1.vcf'));
const contacts2 = vcf.parse(fs.readFileSync('contacts2.vcf'));
// Extract phone numbers
const phones1 = [];
const phones2 = [];
contacts1.forEach((c) => {
const numbers = getPhoneNumbers(c);
phones1.push(...numbers);
});
contacts2.forEach((c) => {
const numbers = getPhoneNumbers(c);
phones2.push(...numbers);
});
// Deduplicate numbers
const uniquePhones1 = new Set(phones1);
const uniquePhones2 = new Set(phones2);
// Find missing numbers
const missingPhones = new Set(
[...uniquePhones1].filter((x) => !uniquePhones2.has(x))
);
// Filter original contacts
const missingContacts = [];
/**
* Filters the `contacts1` array to find contacts that have at least one phone number that is missing from `contacts2`.
* The resulting `missingContacts` array contains the contacts from `contacts1` that have a missing phone number.
*/
contacts1.filter((c) => {
c = c.toString();
const numbers = getPhoneNumbers(c);
numbers.forEach((number) => {
if (missingPhones.has(number)) {
if (!missingContacts.includes(c)) {
missingContacts.push(c);
}
}
});
});
fs.mkdirSync('out');
// Write output files
writeToFile('./out/uniquePhones1.txt', [...uniquePhones1].join('\n'));
writeToFile('./out/uniquePhones2.txt', [...uniquePhones2].join('\n'));
writeToFile('./out/missingPhones.txt', [...missingPhones].join('\n'));
writeToFile('./out/missingContacts.vcf', missingContacts.join('\n'));
{
"name": "compare-vcf-files",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"vcard-parser": "^1.0.0",
"vcf": "^2.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment