Convert simple csv file and string to array
npm i silvery-csv
import SilveryCSV from "silvery-csv"
const csv = new SilveryCSV()
const str = `
a , b , c
1 , 2 , 3
4 , 5 , 6
`
const result = csv.csvToArray(str)
console.log(result)
/*
{
header: [ 'a', 'b', 'c' ],
data: [ [ '1', '2', '3' ], [ '4', '5', '6' ] ]
}
*/
import SilveryCSV from "silvery-csv"
const csv = new SilveryCSV()
const str = `
a , b , c
1 , 2 , 3
4 , 5 , 6
`
const result = csv.splitCsv(str)
console.log(result)
/*
The element will not trim.
[
'a , b , c',
' 1 , 2 , 3',
' 4 , 5 , 6'
]
*/
import SilveryCSV from "silvery-csv"
const csv = new SilveryCSV()
const str = `
a , b , c
1 , 2 , 3
4 , 5 , 6
`
const rows = csv.splitCsv(str)
const result = rows.map(r => csv.csvRowToArray(r))
console.log(result)
/*
[ [ 'a', 'b', 'c' ], [ '1', '2', '3' ], [ '4', '5', '6' ] ]
*/
Default config is { withHeader = true, trim = true }
You can pass it on the constructor
// with header and trim value
const csv = new SilveryCSV()
// no header and trim value
const csvNoHeader = new SilveryCSV({ withHeader: false })
// no header and not trim value
const csvNoHeaderAndTrim = new SilveryCSV({ withHeader: false, trim: false })
npm test
See license file.