Skip to content

Instantly share code, notes, and snippets.

@aheld
Created October 29, 2018 12:56
Show Gist options
  • Save aheld/e93881ddceff7e3b03e137c269d2a13b to your computer and use it in GitHub Desktop.
Save aheld/e93881ddceff7e3b03e137c269d2a13b to your computer and use it in GitHub Desktop.
Given a csv file with a column called "Category IDs" add a column called 'Category Names' with the name based on a lookup and write it out
const csv = require('csv')
const fs = require('fs')
const path = require('path')
const devnull = require('dev-null')
const catData = require('./catData')
const inputs = ['Members_49827_14.csv', 'Contacts Combined-3-Files.csv']
inputs.forEach(processCSV)
function processCSV (filename) {
const parser = csv.parse({ columns: true })
const transformer = csv.transform(function (record, callback) {
record['Category Names'] = catLookup(record['Category IDs'])
callback(null, record)
},
{ parallel: 5 }
)
const stringify = csv.stringify({ header: true })
fs.createReadStream(path.join(__dirname, filename), 'utf-8')
.pipe(parser)
.pipe(transformer)
.pipe(stringify)
.pipe(fs.createWriteStream(path.join(__dirname, filename + '.output.csv')))
}
function catLookup (data) {
var cats = data.split(',') // Split the data by commas
return dedupeArray(cats) // remove duplicates
.map(function (code) { return lookupCode(code) }) // map runs the function for every item in the array
.filter(function (cat) { return !!cat }) // filter out any categories that are blank (no match for teh code)
.join(',') // join all the categories in the comma seperated list
}
function lookupCode (code) {
code = parseInt(code)
return catData[code] || ''
}
function dedupeArray (arr) {
var seen = {}
return arr.filter(function (item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment