node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

Ability to rename headers after validation

Open entimaniac opened this issue 6 years ago • 1 comments

I want to be able to validate the headers of an uploaded file by uppercasing and replacing spaces in order to provide a forgiving user experience. and then proceed to replace the headers used as keys in the JSON so that I can reference the properties by the correct name. I do not want to simply supply my own headers upfront because that then eliminates the inherent ease of use this library provides which is being able to accept the columns in any order.

file:

header1, HeaDER 2, header        3
value1, value2, value3
const myKnownHeaders = ['HEADER1','HEADER2','HEADER3'];
csv().on('header', (headers) => {
  // validate headers 
  let newHeaders = headers.map(a => a.replace(/\s+/g, '_').toUpperCase());
  if ( myKnownHeaders.every(elem => newHeaders.indexOf(elem) > -1)) ) {
    // idk, set the headers that are to be used as key somehow
  }
})

I thought doing

csv({
        headers: fileHeaders
    }). . .

would have worked but the header event just emits my own headers back to me which doesn't work.

entimaniac avatar Dec 05 '19 20:12 entimaniac

brother, i figured it out - just run the darn thing twice.

mattvisk avatar Sep 21 '21 08:09 mattvisk