markty-csv
v0.0.1
Published
Nano csv parser for Javascript.
Downloads
1
Readme
:microscope: Nano CSV parser for javascript using Markty.js.
Quick start
For Node.js
npm install markty-csv
var csv = require('markty-csv')
// or using ES6:
import csv from 'markty-csv'
const someCSV = `
a,b,c
1,2,3
4,5,6
`
console.log( CSV(someCSV) )
// > prints:
// [
// [a,b,c],
// [1,2,3],
// [4,5,6]
// ]
In-Browser
Find latest version here.
To get the umd
version:
- Observe the URL here and see the latest version used after
@
like@0.0.1
. - Just modify the URL to get something like this:
https://unpkg.com/[email protected]/dist/martycsv.umd.js
Then just import it normally :
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/martycsv.umd.js"></script>
Then the exported name is marktycsv()
, so you can just:
<script>
var someCSV = 'a,b,c\n1,2,3\n4,5,6';
console.log( marktycsv(someCSV) )
</script>
// > prints:
// [
// [a,b,c],
// [1,2,3],
// [4,5,6]
// ]
FEATURES
- :microscope: Ridiculously SMALL:: 100 LOC, 800 bytes gzipped
- :zap: Blazing fast :zap: see benchmarks
- @TODO:
- [ ] parse integers like
1
,2
,3
as javascriptint
... - [ ] parse float like
3.14
as javascriptfloat
- [ ] parse boolean like
true
,false
as javascriptboolean
- [ ] parse signed numbers like
+27
,-23
as javascript's signed mumber - [ ] parse infinity like
+inf
,inf
,-inf
as javascript'sInf
,-Inf
- [ ] parse hexadecimals like
0xDEADBEEF
- [ ] parse octals like
0o01234567
,0o755
- [ ] parse binaries like
0b11010110
- [ ] parse dates like
1979-05-27T00:32:00-07:00
,1979-05-27
as javascript'sdate
object - [ ] allow for various delimiters like
;
,|
,\t
... - [ ] allow escape character like
"
- [ ] parse integers like
Should you use it ?
- :baby: Not fully unit-tested YET...
- Not benchmarked YET, especially against HUGE csv files, so just make your tests before using it :)
- Not meant to replace fully-fledged CSV parsers like https://github.com/Keyang/node-csvtojson.
markty-CSV
just gives you an array of csv values line by line: the rest is up to you !!
Hey !! But I need to convert my CSV to JSON !
Well, markty-CSV
will just parse CSV for you and push every values into an array. Now, if you want to convert it to JSON, we got you covered with this nice short snippet:
function MatrixToJSON(matrix,from,to){
let jsonResult = []; from = from||0;
matrix.map((a,i) => {
let obj = Object.assign({}, ...matrix[0].map((h, index) => ({[h]: matrix[i][index]})))
jsonResult.push(obj)
})
return to ? jsonResult.splice(from,to) : jsonResult.splice(from)
}
// then later in your code:
var someCSV = 'a,b,c\n1,2,3\n4,5,6';
var parsedToArray = marktycsv(someCSV)
var convertToJson = MatrixToJSON(parsedToArray, 1)
// > convertToJson should look like:
// [
// { "a":"1", "b":"2", "c":"3" },
// { "a":"4", "b":"5", "c":"6" }
// ]
Benchmarks
| Test | Observations | markty-CSV | node-csvtojson | 5 liner gist | |:-------------|:-------------|-----------:|--------------------:|------------------:| | gzipped size | | 307 b | 46.899 b | 147 b |