Skip to content

Instantly share code, notes, and snippets.

@adeyahya
Created January 14, 2018 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeyahya/f1307d2b8f4b57b9d529cab736c6fa62 to your computer and use it in GitHub Desktop.
Save adeyahya/f1307d2b8f4b57b9d529cab736c6fa62 to your computer and use it in GitHub Desktop.
CSV Utils using Ramda
const R = require('ramda');
const csvToArray = csvText =>
csvText
.split('\n')
.map(item => item.split(','))
// new line will produce Array with length 1 & with value ''
// ignore it
.filter(item => item[0] !== '');
const csvArrayToObject = csvArray => {
// array[0] expected as headers
const headerArray = csvArray[0];
const bodyArray = R.drop(1, csvArray);
return bodyArray.map(body => {
const newObj = new Object();
body.forEach((bodyItem, index) => {
const key = headerArray[index];
// ignore if the body is overlength than key
// that mean key is undefined
if (!key) return;
newObj[key] = bodyItem.replace('\r', '').replace('\n', '');
});
return newObj;
});
};
const csvToObject = csvText => R.compose(csvArrayToObject, csvToArray)(csvText);
const objectToCsv = csvObj => {
if (!Array.isArray(csvObj))
throw new Error('input must be an Array of object');
const headerText = R.keys(csvObj[0])
.join(',')
.concat('\n');
const bodyText = csvObj.map(item => R.values(item).join(',')).join('\n');
return headerText.concat(bodyText);
};
module.exports = {
csvToArray,
csvArrayToObject,
csvToObject,
objectToCsv,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment