initial commit
This commit is contained in:
commit
b39a02c048
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.vcf
|
||||
node_modules
|
69
dedupe-vcards.js
Normal file
69
dedupe-vcards.js
Normal 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
10
index.js
Normal 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
21
package.json
Normal 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"
|
||||
}
|
||||
}
|
17
test/dedupe-vcards.spec.js
Normal file
17
test/dedupe-vcards.spec.js
Normal 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
1
vcards/readme.md
Normal 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).
|
Loading…
Reference in a new issue