+25
-7
| (function(){ | ||
| var TSV = {} | ||
| , sep = "\t" | ||
| , br = "\n" | ||
| var br = "\n" | ||
| TSV.stringify = function (data) { | ||
| var keys = Object.keys(data[0]) | ||
| function stringify (data) { | ||
| var sep = this.sep | ||
| , keys = Object.keys(data[0]) | ||
| , header = keys.join(sep) | ||
@@ -25,4 +24,5 @@ , output = header + br | ||
| TSV.parse = function (tsv) { | ||
| var lines = tsv.split(/[\n\r]/).filter(comments) | ||
| function parse (tsv) { | ||
| var sep = this.sep | ||
| , lines = tsv.split(/[\n\r]/).filter(comments) | ||
| , keys = lines.shift().split(sep) | ||
@@ -39,2 +39,20 @@ | ||
| var TSV = { | ||
| stringify: stringify | ||
| , parse: parse | ||
| , sep: "\t" | ||
| } | ||
| // cyclical reference to allow both | ||
| // var TSV = require('tsv') | ||
| // and | ||
| // { TSV, CSV } = require('tsv') | ||
| TSV.TSV = TSV | ||
| TSV.CSV = { | ||
| stringify: stringify | ||
| , parse: parse | ||
| , sep: "," | ||
| } | ||
| if (typeof module !== 'undefined' && module.exports){ | ||
@@ -41,0 +59,0 @@ module.exports = TSV |
+2
-2
| { | ||
| "name": "tsv", | ||
| "description": "Small sync TSV converter/parser", | ||
| "version": "0.1.0", | ||
| "description": "Simple dependency-free TSV and CSV converter/parser", | ||
| "version": "0.1.1", | ||
| "main": "index.js", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
+34
-1
| TSV | ||
| === | ||
| JSON-to-TSV and vice-versa converter. Good for efficiently serving data to D3 while keeping a readable format. | ||
| Simple TSV/CSV converter and parser. Good for serving time-series (or any series) data to use in D3.js or other client-side graph libraries. | ||
| Processing is *synchronous*. **Do not use** for large datasets - use something that supports streams instead, like [node-csv-parser](http://npmjs.org/node-csv-parser). | ||
| ### Install | ||
| npm install tsv | ||
| ### Usage | ||
| var csv = require('csv') | ||
| var tsv = require('xsv').tsv | ||
| { tsv, csv } = require 'xsv' // coffeescript | ||
| The `data` argument for stringify must be a flat array of objects. Keys will be derived from the first item. | ||
| TSV.stringify([ | ||
| { id: 1, name: 'xx' }, | ||
| { id: 2, name: 'yy' } | ||
| ... | ||
| ]) | ||
| Outputs | ||
| id name | ||
| 1 xx | ||
| 2 yy | ||
| ### API | ||
| - `TSV.stringify(object)` | ||
| - `TSV.parse(tsv_string)` | ||
| - `CSV.stringify(object)` | ||
| - `CSV.parse(csv_string)` |
+11
-5
| assert = require 'assert' | ||
| fs = require 'fs' | ||
| TSV = require '../index' | ||
| { TSV, CSV } = require '../index' | ||
| console.log(TSV) | ||
| tsv_data = fs.readFileSync('./test/test.tsv').toString() | ||
| csv_data = fs.readFileSync('./test/test.csv').toString() | ||
| json_data = JSON.parse(fs.readFileSync('./test/test.json').toString()) | ||
| suite 'TSV', -> | ||
@@ -18,2 +16,10 @@ | ||
| test 'TSV to JSON', -> | ||
| assert.deepEqual TSV.parse(tsv_data), json_data | ||
| assert.deepEqual TSV.parse(tsv_data), json_data | ||
| suite 'CSV', -> | ||
| test 'JSON to CSV', -> | ||
| assert.equal CSV.stringify(json_data), csv_data | ||
| test 'CSV to JSON', -> | ||
| assert.deepEqual CSV.parse(csv_data), json_data |
4485
41.35%73
25.86%37
825%