jsonexport
Advanced tools
Comparing version 2.0.7 to 2.0.8
#! /usr/bin/env node | ||
const jsonexport = require('../lib/index.js'); | ||
const fs = require('fs'); | ||
var jsonexport = require('../lib/index.js'); | ||
const stdin = process.stdin; | ||
const stdout = process.stdout; | ||
const inputFile = process.argv[2]; | ||
const outputFile = process.argv[3]; | ||
var stdin = process.stdin, | ||
inputChunks = []; | ||
if (inputFile) | ||
return fs.createReadStream(inputFile) | ||
.pipe(jsonexport()) | ||
.pipe(outputFile ? fs.createWriteStream(outputFile) : stdout); | ||
stdin.setEncoding('utf8'); | ||
stdin.on('data', function (chunk) { | ||
inputChunks.push(chunk); | ||
}); | ||
stdin.on('end', function () { | ||
var input = inputChunks.join(''), | ||
parsedData = JSON.parse(input); | ||
jsonexport(parsedData, function (err, csv) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
console.log(csv); | ||
}); | ||
}); | ||
stdin | ||
.pipe(jsonexport()) | ||
.pipe(stdout); |
## Change log | ||
---------------------- | ||
- v2.0.8 - cli improvements | ||
- v2.0.7 - removed underscore as a dependency (dydx) | ||
@@ -4,0 +5,0 @@ - v2.0.4 - rename option |
@@ -6,3 +6,3 @@ 'use strict'; | ||
return fn && getType.toString.call(fn) === '[object Function]'; | ||
} | ||
}; | ||
@@ -9,0 +9,0 @@ module.exports.isArray = (arr) => Array.isArray(arr); |
@@ -7,3 +7,3 @@ /* jshint node:true */ | ||
//const _ = require('underscore'); | ||
const Parser = require('./parser/json'); | ||
const Parser = require('./parser/csv'); | ||
const Stream = require('./core/stream'); | ||
@@ -26,3 +26,3 @@ const helper = require('./core/helper'); | ||
} | ||
userOptions = !callback ? json : userOptions; | ||
userOptions = !callback ? json : userOptions; | ||
let parser = new Parser(userOptions); | ||
@@ -29,0 +29,0 @@ if (!callback || !helper.isFunction(callback)) return new Stream(parser); |
{ | ||
"name": "jsonexport", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"description": "Makes easy to convert JSON to CSV", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -21,2 +21,3 @@ # jsonexport | ||
Installation command is `npm install jsonexport`. | ||
Run tests with `npm test`. | ||
@@ -32,2 +33,11 @@ | ||
``` | ||
## CLI | ||
Global installation command is `npm install -g jsonexport`. | ||
Convert JSON to CSV using `cat data.json | jsonexport` or `jsonexport data.json` | ||
Usage: `jsonexport <JSON filename> <CSV filename>` | ||
## Stream | ||
@@ -34,0 +44,0 @@ |
@@ -10,16 +10,24 @@ /* jshint evil: true */ | ||
function getWriteStream(done) { | ||
var write = new stream.Writable(); | ||
var csv = ""; | ||
write._write = function(chunk, enc, next) { | ||
chunk = chunk.toString(); | ||
csv += chunk; | ||
next(); | ||
}; | ||
write.on('finish', () => { | ||
done(csv); | ||
}); | ||
return write; | ||
} | ||
describe('Stream', () => { | ||
it('simple', (done) => { | ||
var read = new stream.Readable(); | ||
var write = new stream.Writable(); | ||
var csv = ""; | ||
write._write = function(chunk, enc, next) { | ||
chunk = chunk.toString(); | ||
csv += chunk; | ||
next(); | ||
}; | ||
write.on('finish', () => { | ||
var write = getWriteStream((csv) => { | ||
expect(csv).to.equal(`name,lastname,escaped${os.EOL}Bob,Smith${os.EOL}James,David,I am a ""quoted"" field`); | ||
done(); | ||
}); | ||
read.pipe(jsonexport()).pipe(write); | ||
@@ -39,10 +47,3 @@ | ||
var read = new stream.Readable(); | ||
var write = new stream.Writable(); | ||
var csv = ""; | ||
write._write = function(chunk, enc, next) { | ||
chunk = chunk.toString(); | ||
csv += chunk; | ||
next(); | ||
}; | ||
write.on('finish', () => { | ||
var write = getWriteStream((csv) => { | ||
expect(csv).to.equal(`name|lastname|escaped${os.EOL}Bob|Smith${os.EOL}James|David|I am a ""quoted"" field`); | ||
@@ -68,10 +69,3 @@ done(); | ||
var read = new stream.Readable(); | ||
var write = new stream.Writable(); | ||
var csv = ""; | ||
write._write = function(chunk, enc, next) { | ||
chunk = chunk.toString(); | ||
csv += chunk; | ||
next(); | ||
}; | ||
write.on('finish', () => { | ||
var write = getWriteStream((csv) => { | ||
expect(csv).to.equal(`id,name,lastname,family.name,family.type${os.EOL}1,Bob,Smith,Peter,Father${os.EOL}2,James,David,Julie,Mother`); | ||
@@ -78,0 +72,0 @@ done(); |
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
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
10570813
24
259
2419
2