Convert Hilton's stupid CSV thing to JSON.
Convert the given CSV file to JSON, invoking fn(err, obj).
Run this:
$ node example
MIT
| Brand | Category | Alt Text | Small File Name | Large File Name | |
|---|---|---|---|---|---|
| Conrad | BUSINESS | A woman smiling and looking straight ahead. Network and Business | Conrad_business_01_small.jpg | Conrad_business_01_large.jpg | |
| Conrad | BUSINESS | A Stethascope laying on a chart. Network...Relate | business_14_small.jpg | business_14_large.jpg | |
| Conrad | BUSINESS | Three men and three women sitting at a conference table looking at a computer monitor on the wall. Network and Business. | Conrad_business_02_small.jpg | Conrad_business_02_large.jpg | |
| Conrad | BUSINESS | A range of snow capped mountains with clouds in the sky. Inspire…Excel. | business_15_small.jpg | business_15_large.jpg | |
| Conrad | BUSINESS | Three women and two men smiling during a meeting. Network…Relate. | business_01_small.jpg | business_01_large.jpg | |
| Conrad | BUSINESS | A range of snow capped mountains with clouds in the sky. Meditate…Motivate. | business_16_small.jpg | business_16_large.jpg | |
| Conrad | BUSINESS | Empty chairs around a boardroom table. Network…Relate. | business_02_small.jpg | business_02_large.jpg | |
| Conrad | BUSINESS | Zeros and ones flowing through a map of the world. | business_17_small.jpg | business_17_large.jpg | |
| Conrad | BUSINESS | A range of snow capped mountains with clouds in the sky. Meditate…Relax. | business_03_small.jpg | business_03_large.jpg |
| var convertFile = require('./'); | |
| var fs = require('fs'); | |
| var ls = fs.readdirSync; | |
| var write = fs.writeFileSync; | |
| // get a list of all CSVs | |
| var files = ls(__dirname).filter(function (file) { | |
| return '.csv' == file.substr(-4); | |
| }); | |
| // serial loop to prevent EMFILEs | |
| (function next(i) { | |
| var file = files[i]; | |
| if (!file) return done(); | |
| // conver the file | |
| convertFile(file, function (err, json) { | |
| var jsonfile = file.slice(0, -4) + '.json'; | |
| write(jsonfile, JSON.stringify(json, null, 2)); | |
| console.log('\n %s -> %s', file, jsonfile); | |
| next(++i); | |
| }); | |
| }(0)); | |
| // a heart warming success message :) | |
| function done() { | |
| console.log('\n GREAT FUCKING JOB!'); | |
| } |
| var fs = require('fs'); | |
| var read = fs.readFileSync; | |
| var csv = require('csv'); | |
| /** | |
| * Expose `convertFile`. | |
| */ | |
| module.exports = convertFile; | |
| /** | |
| * Convert the given CSV `file` to JSON, | |
| * invoking `fn(err, obj)`. | |
| * | |
| * @api public | |
| * @param {String} file | |
| * @param {Function} fn | |
| * @return {Object} | |
| */ | |
| function convertFile(file, fn) { | |
| var data = read(file).toString(); | |
| csv().from.string(data).to.array(function (csv) { | |
| var headers = toObject(csv.shift()); | |
| var obj = csv.map(function (row) { | |
| return buildRow(headers, row); | |
| }); | |
| fn(null, obj); | |
| }); | |
| } | |
| /** | |
| * Build an Object `row` from the given `array`. | |
| * | |
| * @api private | |
| * @param {Object} headers | |
| * @param {Array} array | |
| * @return {Object} | |
| */ | |
| function buildRow(headers, array) { | |
| var row = copy(headers); | |
| Object.keys(row).forEach(function (key, i) { | |
| row[key] = array[i]; | |
| }); | |
| return row; | |
| } | |
| /** | |
| * Create an `Object` with the keys of the given `array`'s values. | |
| * | |
| * @api private | |
| * @param {Array} array | |
| * @return {Object} | |
| */ | |
| function toObject(array) { | |
| var obj = {}; | |
| for (var i = 0; i < array.length; i++) { | |
| obj[array[i]] = null; | |
| } | |
| return obj; | |
| } | |
| /** | |
| * Create a shallow copy of the given `obj`. | |
| * | |
| * @api private | |
| * @param {Object} obj | |
| * @return {Object} | |
| */ | |
| function copy(obj) { | |
| var res = {}; | |
| for (var prop in obj) { | |
| res[prop] = obj[prop]; | |
| } | |
| return res; | |
| } |
| { | |
| "name": "hilton-csv", | |
| "version": "0.0.0", | |
| "main": "index.js", | |
| "private": true, | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "Stephen Mathieson <[email protected]>", | |
| "license": "MIT", | |
| "dependencies": { | |
| "csv": "~0.3.7" | |
| } | |
| } |