Comparing version 0.2.6 to 0.2.7
143
index.js
#!/usr/bin/env node | ||
'use strict'; /*jslint node: true, es5: true, indent: 2 */ | ||
var async = require('async'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var stream = require('stream'); | ||
var util = require('util'); | ||
var stream = require('stream'); | ||
var inference = require('./inference'); | ||
var merge = require('./merge'); | ||
var Parser = exports.Parser = require('./parser'); | ||
@@ -57,14 +61,65 @@ var Stringifier = exports.Stringifier = require('./stringifier'); | ||
function describe(rows, parser) { | ||
// parser.columns are ordered like the original, inference.columns may not be | ||
var columns = parser.columns || inference.columns(rows); | ||
for (var column_index in columns) { | ||
var column_name = columns[column_index]; | ||
console.log('[' + column_index + '] ' + column_name + ':'); | ||
var cells = pluck(rows, column_name); | ||
console.log(' ' + cells.join(', ')); | ||
var whitespace_literals = { | ||
'\r': '\\r', | ||
'\n': '\\n', | ||
'\t': '\\t', | ||
}; | ||
function escapeWhitespace(s) { | ||
return whitespace_literals[s]; | ||
} | ||
function describe(stream, filename, opts, callback) { | ||
if (filename) { | ||
console.log(filename); | ||
} | ||
process.exit(0); | ||
var rows = []; | ||
var parser = stream.pipe(new Parser()); | ||
var onData = function(row) { | ||
rows.push(row); | ||
if (rows.length > 10) { | ||
parser.removeListener('data', onData); | ||
// parser.columns are ordered like the original, inference.columns may not be | ||
var columns = parser.columns || inference.columns(rows); | ||
for (var i = 0, l = parser.columns.length; i < l; i++) { | ||
var name = parser.columns[i]; | ||
console.log('[' + i + '] ' + name + ':'); | ||
var cells = pluck(rows, name).join(', ').replace(/\r|\n|\t/g, escapeWhitespace); | ||
var segment = opts.width - 2; | ||
for (var start = 0, end = cells.length; start < end; start += segment) { | ||
console.log(' ' + cells.slice(start, start + segment)); | ||
} | ||
} | ||
callback(); | ||
} | ||
}; | ||
parser.on('data', onData); | ||
} | ||
function read(stream, filename, opts, callback) { | ||
if (filename) { | ||
console.error('Reading ' + filename); | ||
} | ||
stream = stream.pipe(new Parser()); | ||
if (opts.omit) { | ||
stream = stream.pipe(new ObjectOmitter(opts.omit.split(/,/g))); | ||
} | ||
if (opts.filter) { | ||
stream = stream.pipe(new ObjectFilter(opts.filter.split(/,/g))); | ||
} | ||
var stringifier = opts.json ? new JSONStringifier() : new Stringifier(opts); | ||
stream = stream.pipe(stringifier); | ||
stream = stream.pipe(process.stdout); | ||
stream.on('finish', callback); | ||
stream.on('error', callback.bind(null)); | ||
} | ||
if (require.main === module) { | ||
@@ -76,2 +131,3 @@ var optimist = require('optimist') | ||
'Usage: <sprints.txt sv [options] > sprints.csv', | ||
' or: sv [options] ~/Desktop/**/*.csv > ~/all.csv', | ||
'', | ||
@@ -86,9 +142,12 @@ 'Options:', | ||
' --omit c,d leave out fields x and y from the results', | ||
' (do not use filter and omit together)', | ||
' do not use filter and omit together', | ||
' --describe only describe the data, using headers and a few examples', | ||
' --width width of the terminal (used by --describe)', | ||
' --merge merge multiple files supplied as command line args', | ||
' -v --verbose turn up the verbosity (still all on STDERR)', | ||
'', | ||
'Only STDIN is supported, and it is coerced to utf8', | ||
'STDIN, if supplied, will be coerced to utf8', | ||
].join('\n')) | ||
.string('delimiter') | ||
.boolean(['json', 'describe']) | ||
.string(['delimiter', 'quotechar', 'escapechar']) | ||
.boolean(['json', 'describe', 'merge', 'verbose']) | ||
.alias({ | ||
@@ -100,6 +159,10 @@ p: 'peek', | ||
j: 'json', | ||
v: 'verbose', | ||
}) | ||
.default({ | ||
width: process.stdout.columns || 80, | ||
}); | ||
var argv = optimist.argv; | ||
process.stdin.setEncoding('utf8'); | ||
var func = argv.describe ? describe : read; // function (stream, filename, opts, callback) { ... } | ||
@@ -110,34 +173,32 @@ if (argv.help) { | ||
} | ||
else if (process.stdin.isTTY) { | ||
optimist.showHelp(); | ||
console.error('You must supply data via STDIN'); | ||
} | ||
else if (argv.describe) { | ||
// var stringifier = argv.json ? new JSONStringifier() : new Stringifier(argv); | ||
var parser = process.stdin.pipe(new Parser()); | ||
var rows = []; | ||
parser.on('data', function(row) { | ||
rows.push(row); | ||
if (rows.length > 10) { | ||
describe(rows, parser); | ||
} | ||
else if (!process.stdin.isTTY) { | ||
// process.stdin.setEncoding('utf8'); | ||
func(process.stdin, null, argv, function(err) { | ||
if (err) throw err; | ||
console.error('Done.'); | ||
}); | ||
} | ||
else { | ||
var stringifier = argv.json ? new JSONStringifier() : new Stringifier(argv); | ||
process.stdin.setEncoding('utf8'); | ||
var parser = process.stdin.pipe(new Parser()); | ||
var filtered = null; | ||
if (argv.filter) { | ||
filtered = parser.pipe(new ObjectFilter(argv.filter.split(/,/g))); | ||
else if (argv._.length) { | ||
if (argv.merge) { | ||
console.error('Merging.'); | ||
merge(argv._, argv, function(err) { | ||
if (err) throw err; | ||
console.error('Done.'); | ||
}); | ||
} | ||
else if (argv.omit) { | ||
filtered = parser.pipe(new ObjectOmitter(argv.omit.split(/,/g))); | ||
} | ||
else { | ||
filtered = parser; | ||
async.eachSeries(argv._, function(filepath, callback) { | ||
var stream = fs.createReadStream(filepath); | ||
func(stream, filepath, argv, callback); | ||
console.error(''); // newline | ||
}, function(err) { | ||
if (err) throw err; | ||
console.error('Done.'); | ||
}); | ||
} | ||
filtered.pipe(stringifier).pipe(process.stdout); | ||
} | ||
else { | ||
optimist.showHelp(); | ||
console.error('You must supply data via STDIN or as unflagged command line arguments.'); | ||
} | ||
} |
23
merge.js
#!/usr/bin/env node | ||
'use strict'; /*jslint node: true, es5: true, indent: 2 */ /*globals setImmediate */ | ||
var async = require('async'); | ||
var fs = require('fs'); | ||
var inference = require('./inference'); | ||
var os = require('os'); | ||
var fs = require('fs'); | ||
var sv = require('./index'); | ||
var util = require('util'); | ||
var async = require('async'); | ||
var sv = require('./index'); | ||
var inference = require('./inference'); | ||
@@ -32,3 +32,3 @@ function File(path) { | ||
var merge = module.exports = function(filepaths, verbose) { | ||
var merge = module.exports = function(filepaths, opts, callback) { | ||
// first pass: collect all possible fieldnames, and linecounts, while we're at it. | ||
@@ -69,3 +69,3 @@ async.map(filepaths, function(filepath, callback) { | ||
console.error('Found ' + total_in + ' lines covering ' + columns.length + ' columns in ' + files.length + ' files.'); | ||
if (verbose) { | ||
if (opts.verbose) { | ||
console.error(columns.map(function(column) { return ' ' + column + ' (' + column.length + ')'; }).join('\n')); | ||
@@ -113,14 +113,5 @@ } | ||
console.error('Done. Wrote a total of ' + pkid + ' rows.'); | ||
callback(err); | ||
}); | ||
}); | ||
}; | ||
if (require.main === module) { | ||
var argv = require('optimist') | ||
.usage([ | ||
'Merge several csv files into a single (sparse) csv file.', | ||
'', | ||
'Usage: merge.js ~/Desktop/**/*.csv > ~/all.csv', | ||
].join('\n')).boolean('verbose').alias('v', 'verbose').argv; | ||
merge(argv._, argv.verbose); | ||
} |
{ | ||
"name": "sv", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Any separated values.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
35218
853
8