Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsv

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsv - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

test/fb.json

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc