Skip to content

Commit

Permalink
Remove --explicit opt, fix object literal handling in map
Browse files Browse the repository at this point in the history
Prior to this commit, object literals were printed as
"[object Object]" when map was the final modifier of
the intput stream. E.g.

$ (echo 'foo' && echo 'foobar') | ./bin/pjs -m '{name: $, length: length}'
[object Object]
[object Object]

After this commit:
$ (echo 'foo' && echo 'foobar') | ./bin/pjs -m '{name: $, length: length}'
{ name: 'foo', length: 3 }
{ name: 'foobar', length: 6 }
  • Loading branch information
danielstjules committed Mar 5, 2015
1 parent 508c4e6 commit 4af10a5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ pjs is a cli tool that can accept input on stdin, or read from a list of files.
Its filter, map and reduce options take expressions to be run, in that order,
and applies them to the supplied input. The expressions themselves can contain
identifiers used by keys in String.prototype, which will automatically be bound
to the given line unless the `--explicit` flag is used. This lets you save a
bit of typing with your one-liners, while still giving you access to all your
JS string functions! Check out some of the examples below to see how they
translate.
to the given line. This lets you save a bit of typing with your one-liners,
while still giving you access to all your JS string functions! Check out some
of the examples below to see how they translate.

``` bash
# Return all lines longer than 5 chars
Expand All @@ -37,11 +36,15 @@ ls -1 | pjs -m '" " + toUpperCase()'
ls -1 | pjs -f 'length > 5' -m 'replace(/\d/g, "")'
```

When using the `--explicit` flag, the current line and value can be accessed
via the $ variable.
The current line and value can also be accessed via the $ variable, and the
tool supports json output.

``` bash
ls -1 | pjs -e -f '$.length > 5' -m '$.replace(/\d/g, "")'
$ (echo 'foo' && echo 'foobar') | pjs -jm '{name: $, length: length}'
[
{"name":"foo","length":3},
{"name":"foobar","length":6}
]
```

## Installation
Expand All @@ -68,7 +71,6 @@ Options:
-h, --help output usage information
-V, --version output the version number
-e, --explicit bind lines to $
-i, --ignore ignore empty lines
-j, --json output as json
-f, --filter <exp> filter by a boolean expression
Expand Down Expand Up @@ -141,4 +143,4 @@ pjs -m 'length' -r max file
| Easy JSON output | Yes | No | No |
| Webscale<sup>TM</sup> | YES | No | No |

<sub>[1] Can't perform "tail -f logfile | py -x x"</sub>
<sub>[1] Can't perform "tail -f logfile | py -x x"</sub>
4 changes: 1 addition & 3 deletions bin/pjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var summ = " Functions and expressions are invoked in the following order:\n" +
program
.version(require('../package.json').version)
.usage("[options] [files ...]\n\n" + summ)
.option('-e, --explicit', 'bind lines to $')
.option('-i, --ignore', 'ignore empty lines')
.option('-j, --json', 'output as json')
.option('-f, --filter <exp>', 'filter by a boolean expression')
Expand Down Expand Up @@ -83,8 +82,7 @@ function getDestination() {
// Pipe to each action, when set
['filter', 'map', 'reduce'].forEach(function(action) {
if (program[action]) {
stream = pjs[action](program[action], outputString[action],
program.explicit);
stream = pjs[action](program[action], outputString[action]);

prev.pipe(stream, opts[action]);
prev = stream;
Expand Down
17 changes: 8 additions & 9 deletions lib/pjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var stream = require('stream');
var Stringify = require('streaming-json-stringify');
var StringDecoder = require('string_decoder').StringDecoder;
var inspect = require('util').inspect;
var utils = require('./utils');
var _nullObject = utils._nullObject;

Expand All @@ -18,15 +19,13 @@ var _nullObject = utils._nullObject;
* for each piped value, streaming those which resulted in a truthy expression.
*
* @param {string} expression The expression to evaluate
* @param {bool} explicit Whether or not to require "$"
* @param {bool} outputString Whether or not to output strings
* returns {Transform} A transform stream in objectMode
*/
exports.filter = function(expression, outputString, explicit) {
exports.filter = function(expression, outputString) {
var i, include, filterStream;

i = 0;

include = function($, i) {
with ($) {
return eval(expression);
Expand All @@ -36,7 +35,7 @@ exports.filter = function(expression, outputString, explicit) {
filterStream = new stream.Transform({objectMode: true});
filterStream._transform = function(chunk, encoding, fn) {
if (!include(chunk, i++)) return fn();
if (outputString) chunk = String(chunk) + "\n";
if (outputString) chunk = String(chunk) + '\n';

this.push(chunk);
return fn();
Expand All @@ -54,15 +53,13 @@ exports.filter = function(expression, outputString, explicit) {
* as a result of the evaluated expression.
*
* @param {string} expression The expression to evaluate
* @param {bool} explicit Whether or not to require "$"
* @param {bool} outputString Whether or not to output strings
* returns {Transform} A transform stream in objectMode
*/
exports.map = function(expression, outputString, explicit) {
exports.map = function(expression, outputString) {
var i, update, mapStream;

i = 0;

update = function($, i) {
with ($) {
var _result = eval('(' + expression + ')');
Expand All @@ -73,7 +70,9 @@ exports.map = function(expression, outputString, explicit) {
if (outputString) {
update = function($, i) {
with ($) {
return String(eval('(' + expression + ')')) + "\n";
var res = eval('(' + expression + ')');
if (typeof res === 'string') return res + '\n';
return inspect(res) + '\n';
}
};
}
Expand Down Expand Up @@ -129,7 +128,7 @@ exports.reduce = function(expression, outputString) {
src.on('end', function() {
var result = performReduce();
if (outputString) {
result = String(result) + "\n";
result = String(result) + '\n';
} else if (result === null) {
result = _nullObject;
}
Expand Down
14 changes: 2 additions & 12 deletions spec/pjsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ describe('pjs', function() {
done();
});
});

it('requires the line be referenced as "$" if explicit is true', function(done) {
var filter = pjs.filter('$.indexOf("b") !== -1', false, true);

testStream(lines, filter, {}, function(err, res) {
expect(err).to.be(null);
expect(res).to.eql(['b', 'bar']);
done();
});
});
});

describe('map', function() {
Expand Down Expand Up @@ -99,8 +89,8 @@ describe('pjs', function() {
});
});

it('requires the line be referenced as "$" if explicit is true', function(done) {
var map = pjs.map('$.charAt(0)', false, true);
it('can reference the line as "$"', function(done) {
var map = pjs.map('$.charAt(0)', false);

testStream(lines, map, {}, function(err, res) {
expect(err).to.be(null);
Expand Down

0 comments on commit 4af10a5

Please sign in to comment.