{"version":3,"file":"readCsv--FfP8n-H.mjs","names":[],"sources":["../src/lib/requests/readCsv.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\n\nimport { decodeCodec } from '@transcend-io/type-utils';\nimport type { Options } from 'csv-parse';\nimport { parse } from 'csv-parse/sync';\nimport * as t from 'io-ts';\n\n/**\n * Read in a CSV and validate its shape\n *\n * @param pathToFile - Path to file\n * @param codec - The codec to validate against. This is the codec for individual, non-header, rows\n * @param options - CSV parse options\n * @returns The JSON data\n */\nexport function readCsv<T extends t.Any>(\n  pathToFile: string,\n  codec: T,\n  options: Options = { columns: true },\n): t.TypeOf<T>[] {\n  // read file contents and parse\n  const fileContent = parse(readFileSync(pathToFile, 'utf-8'), options);\n\n  // validate codec\n  const data = decodeCodec(t.array(codec), fileContent);\n\n  // remove any special characters from object keys\n  const parsed = data.map((datum) =>\n    Object.entries(datum).reduce(\n      (acc, [key, value]) =>\n        Object.assign(acc, {\n          [key.replace(/[^a-z_.+\\-A-Z -~]/g, '')]: value,\n        }),\n      {} as T,\n    ),\n  );\n  return parsed;\n}\n"],"mappings":"6JAeA,SAAgB,EACd,EACA,EACA,EAAmB,CAAE,QAAS,GAAM,CACrB,CAEf,IAAM,EAAc,EAAM,EAAa,EAAY,QAAQ,CAAE,EAAQ,CAerE,OAZa,EAAY,EAAE,MAAM,EAAM,CAAE,EAGtB,CAAC,IAAK,GACvB,OAAO,QAAQ,EAAM,CAAC,QACnB,EAAK,CAAC,EAAK,KACV,OAAO,OAAO,EAAK,EAChB,EAAI,QAAQ,qBAAsB,GAAG,EAAG,EAC1C,CAAC,CACJ,EAAE,CACH,CAEU"}