Created
June 11, 2016 23:47
-
-
Save thzinc/96d202bb0ae0b1e6663440e02073435c to your computer and use it in GitHub Desktop.
Script to parse list of barcodes and append additional UPC/ISBN info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var fileName = process.argv[2]; | |
var barcodes = readFile(fileName, 'utf8') | |
.then(data => data.split(/\r\n/g)) | |
.then(lines => lines | |
.slice(1) | |
.map(line => line.split(/,/g)) | |
.map(a => { | |
return { | |
barcode: a[0], | |
type: a[1], | |
scanDate: a[2], | |
}; | |
})); | |
var additionalInfo = barcodes | |
.then(b => b.map(o => readFile(`json/${o.barcode}.json`, 'utf8'))) | |
.then(promises => Promise.all(promises)) | |
.then(jsons => jsons | |
.map(JSON.parse) | |
.reduce((a, b) => a.concat(b), []) | |
.reduce((prev, curr) => { | |
prev[curr.Barcode] = curr; | |
return prev; | |
}, {})); | |
Promise.all([barcodes, additionalInfo]) | |
.then(values => { | |
var bs = values[0]; | |
var is = values[1]; | |
return bs | |
.map(b => { | |
var info = is[b.barcode]; | |
if (info) { | |
b.name = info.Name; | |
b.manufacturer = info.Manufacturer; | |
} | |
return b; | |
}) | |
.map(b => ['scanDate', 'barcode', 'type', 'name', 'manufacturer'] | |
.map(key => b[key] || '') | |
.map(value => `"${value.replace(/"/g, '\\"')}"`) | |
.join(',')) | |
; | |
}) | |
.then(lines => lines.map(log)); | |
function readFile(filename, enc){ | |
return new Promise(function (fulfill, reject){ | |
fs.readFile(filename, enc, function (err, res){ | |
if (err) reject(err); | |
else fulfill(res); | |
}); | |
}); | |
} | |
function log(value) { | |
console.log(value) | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment