Comparing version 0.1.1 to 0.2.0
95
index.js
@@ -5,9 +5,49 @@ (function(){ | ||
function stringify (data) { | ||
function extend (o) { | ||
Array.prototype.slice.call(arguments, 1).forEach(function(source){ | ||
if (!source) return | ||
for (var keys = Object.keys(source), i = 0; i < keys.length; i++) { | ||
var key = keys[i] | ||
o[key] = source[key] | ||
} | ||
}) | ||
return o | ||
} | ||
function unquote (str) { | ||
var match | ||
return (match = str.match(/(['"]?)(.*)\1/)) && match[2] || str | ||
} | ||
function comments (line) { | ||
return !/#@/.test(line[0]) | ||
} | ||
function getValues (line, sep) { | ||
return line.split(sep).map(function(value){ | ||
var value = unquote(value), num = +value | ||
return num === parseInt(value, 10) ? num : value | ||
}) | ||
} | ||
function Parser (sep, options) { | ||
var opt = extend({ | ||
header: true | ||
}, options) | ||
this.sep = sep | ||
this.header = opt.header | ||
} | ||
Parser.prototype.stringify = function (data) { | ||
var sep = this.sep | ||
, keys = Object.keys(data[0]) | ||
, header = keys.join(sep) | ||
, output = header + br | ||
, head = !!this.header | ||
, keys = (typeof data[0] === 'object') && Object.keys(data[0]) | ||
, header = keys && keys.join(sep) | ||
, output = head ? (header + br) : '' | ||
if (!data || !keys) return '' | ||
return output + data.map(function(obj){ | ||
var item = keys ? {} : [] | ||
var values = keys.reduce(function(p, key){ | ||
@@ -21,38 +61,29 @@ p.push(obj[key]) | ||
function comments (line) { | ||
return !/#@/.test(line[0]) | ||
} | ||
function parse (tsv) { | ||
Parser.prototype.parse = function (tsv) { | ||
var sep = this.sep | ||
, lines = tsv.split(/[\n\r]/).filter(comments) | ||
, keys = lines.shift().split(sep) | ||
, head = !!this.header | ||
, keys = head ? getValues(lines.shift(), sep) : {} | ||
return lines.reduce(function(p, line){ | ||
p.push(line.split(sep).reduce(function(p, val, i){ | ||
p[keys[i]] = val | ||
return p | ||
}, {})) | ||
return p | ||
if (lines.length < 1) return [] | ||
return lines.reduce(function(output, line){ | ||
var item = head ? {} : [] | ||
output.push(getValues(line, sep).reduce(function(item, val, i){ | ||
item[keys[i] || i] = val | ||
return item | ||
}, item)) | ||
return output | ||
}, []) | ||
} | ||
var TSV = { | ||
stringify: stringify | ||
, parse: parse | ||
, sep: "\t" | ||
} | ||
// Export TSV parser as main, but also expose `.TSV`, `.CSV` and `.Parser`. | ||
var TSV = new Parser("\t") | ||
// cyclical reference to allow both | ||
// var TSV = require('tsv') | ||
// and | ||
// { TSV, CSV } = require('tsv') | ||
TSV.TSV = TSV | ||
extend(TSV, { | ||
TSV : TSV | ||
, CSV : new Parser(",") | ||
, Parser : Parser | ||
}) | ||
TSV.CSV = { | ||
stringify: stringify | ||
, parse: parse | ||
, sep: "," | ||
} | ||
if (typeof module !== 'undefined' && module.exports){ | ||
@@ -59,0 +90,0 @@ module.exports = TSV |
{ | ||
"name": "tsv", | ||
"description": "Simple dependency-free TSV and CSV converter/parser", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -37,2 +37,22 @@ TSV | ||
- `CSV.stringify(object)` | ||
- `CSV.parse(csv_string)` | ||
- `CSV.parse(csv_string)` | ||
#### Options and custom separators | ||
No headers: | ||
// Creating a new parser | ||
var Parser = require('tsv').Parser | ||
var CSV = new Parser(",", { header: false }) | ||
// Using the default parser | ||
var CSV = require('tsv').CSV | ||
CSV.header = false | ||
Custom "pipe-separated values": | ||
var TSV = require('tsv') | ||
PSV = new TSV.Parser("|") | ||
var res = PSV.parse(...) | ||
@@ -24,2 +24,8 @@ [{ | ||
"source": "earth" | ||
}, | ||
{ | ||
"id": 90, | ||
"food": "raw meat", | ||
"type": "noquotes", | ||
"source": " spaces " | ||
}] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
12020
13
283
57
0
2