initial commit

This commit is contained in:
Nils Norman Haukås 2015-04-07 19:09:00 +02:00
commit b39a02c048
6 changed files with 120 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.vcf
node_modules

69
dedupe-vcards.js Normal file
View file

@ -0,0 +1,69 @@
'use strict'
var fs = require('fs');
var _ = require('lodash');
exports.findAndDedupeVcards = function (vcardDirectory, callback) {
fs.readdir(vcardDirectory, function (err, files) {
if (err) throw err;
exports.processVcardFiles(filterAndAddFilePath(files), callback);
});
function filterAndAddFilePath (files) {
return _.reduce(files, function (newArray, file) {
if (/vcf$/.test(file)) newArray.push(vcardDirectory + file);
return newArray
}, []);
}
}
exports.processVcardFiles = function (vcardFiles, callback) {
var vcardsResult = [];
var done = _.after(vcardFiles.length, function () {
callback(deduplicate(vcardsResult));
});
_.map(vcardFiles, function (fileUri) {
fs.readFile(fileUri, {encoding: 'UTF-8'}, function (err, data) {
if (err) throw err;
var result = exports.createVcardArray(data);
vcardsResult = addArraysToArray(vcardsResult, result);
done();
});
});
function addArraysToArray (destinationArray, arrays) {
return _.reduce(arrays, function (newArray, arrayInArrays) {
newArray.push(arrayInArrays);
return newArray;
}, destinationArray);
}
function deduplicate (vcardArray) {
return _.reduce(vcardArray, function (dedupedArray, vcard) {
if (!isAlreadyIncluded(vcard, dedupedArray)) dedupedArray.push(vcard);
return dedupedArray;
}, []);
function isAlreadyIncluded (vcard, dedupedArray) {
return _.some(dedupedArray, function (vcardInArray) {
return _.isEqual(vcardInArray, vcard);
});
}
}
}
exports.createVcardArray = function (vcardFileData) {
return _.reduce(vcardFileData.split('\n'), function (vcardObjArray, entry) {
if (/^BEGIN:VCARD/.test(entry)) {
vcardObjArray.push(["BEGIN:VCARD"]);
return vcardObjArray;
} else {
return exports.addVCardEntry(vcardObjArray, entry);
}
}, []);
}
exports.addVCardEntry = function (vcardObjArray, entry) {
if (entry != "") {
var vcard = vcardObjArray.pop();
vcard.push(entry);
vcardObjArray.push(vcard);
}
return vcardObjArray;
}

10
index.js Normal file
View file

@ -0,0 +1,10 @@
var fs = require('fs');
var vcard = require('./dedupe-vcards')
var _ = require('lodash');
vcard.findAndDedupeVcards('./vcards/', function (dedupedData) {
fs.writeFile('deduped-contacts.vcf', _.flatten(dedupedData).join('\n'), function (err) {
if (err) throw err;
console.log('Successfully wrote file');
});
});

21
package.json Normal file
View file

@ -0,0 +1,21 @@
{
"name": "dedupe-vcards",
"version": "0.0.1",
"description": "Concatenates and removes duplicate vcards from result",
"main": "dedupe-vcards.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"dedupe",
"vcards"
],
"author": "Nils Norman Haukaas",
"license": "MIT",
"dependencies": {
"lodash": "^3.6.0"
},
"devDependencies": {
"assert": "^1.3.0"
}
}

View file

@ -0,0 +1,17 @@
var assert = require("assert")
var dedupeVcards = require('../dedupe-vcards.js')
describe('vcard program', function(){
it('processVcardData() should be able to create vcardArrays', function(){
var data = "BEGIN:VCARD\n" +
"N:Test\n" +
"END:VCARD\n" +
"BEGIN:VCARD\n" +
"FN:TestName\n" +
"END:VCARD\n";
var actualResult = dedupeVcards.createVcardArray(data);
var desiredResult = [["BEGIN:VCARD", "N:Test", "END:VCARD"],
["BEGIN:VCARD", "FN:TestName", "END:VCARD"]];
assert.deepEqual(actualResult, desiredResult);
})
})

1
vcards/readme.md Normal file
View file

@ -0,0 +1 @@
Vcard (.vcf) in this folder will be concatenated and any duplicates removed. The result will be exported to this project root folder (alongside dedupe-vcard.js).