Skip to content

extremeheat/dumb-csv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dumb-csv

NPM version Build Status Try it on gitpod

An unfancy CSV to JSON/markdown parsing lib with no streams or asynchrony

Install

npm install dumb-csv

Usage

const dumbcsv = require('dumb-csv')
dumbcsv
  .fromCSV({ data: 'hello,world', separator: ',', headerFields: ['a', 'b'] })
  .toJSON()
// [{"a":"hello","b":"world"}]

API

fromCSV(options: { data, file, separator = ',', headerFields, overrideExistingHeader, parseFloats = true, commentPrefix? }) : { toJSON: () => object, toMarkdown: () => string }

Input options object:

  • data (string) -- a string representing the CSV data
  • OR file (string) -- a path to the CSV
  • headerFields (optional string[]) -- an array of strings containing the headers for the CSV, assuming they are not the 1st line
  • overrideExistingHeader (optional bool) -- if the 1st line IS a header, but your headerFields should override it
  • parseFloats (default true) -- if we should try and convert rows that are numbers into JS Number's, or if false keep everything as strings
  • commentPrefix (optional) -- Ignore all lines that start with this comment prefix.

Output object :

  • toJSON() (function) -- calling this will return a javascript object that represents the CSV
  • toMarkdown() (function) -- calling this will return a string that has a formatted markdown table inside that represents the CSV

withDir(root: string): { openCSV(path: string, options?), openTSV(path: string, options?) }

A simple way to open CSV/TSV/delimiter seperated files with respect to the root directory. Calling this function will return 2 functions, openCSV and openTSV, both of which take a path that will be appended to the root path. The options these functions take are the same as fromCSV's options, except that openTSV sets the seperator to '\t'. Additionally, fromTSV()'s toJSON() method is called at the end for simplicity. If you need more configurability, use fromCSV directly.

This is intended to return functions that can act similarly to the Node.js/CommonJS require() function. For example,

const reader = require('dumb-csv').withDir(__dirname)
const data = reader.openTSV('./data/data.tsv')

the above function will open './data/data.tsv' with respect to this file's current directory, with the seperator set as '\t'.

About

unfancy and dumb Node.js CSV/TSV/delimiter separated data to json or markdown table converter

Resources

License

Stars

Watchers

Forks

Packages

No packages published