Skip to content

Commit

Permalink
flush
Browse files Browse the repository at this point in the history
  • Loading branch information
fritx committed Jul 16, 2016
1 parent 5093f89 commit eb2f96f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ cat .gitignore | js -ti 'x.trim().split(`\n`)' \
```shell
# lodash is also integrated in
# https://github.com/lodash/lodash
echo '[1,2,3,4]' | js '_.filter(x, x => x %2)' \
echo '[1,2,3,4]' | js '_.filter(x, x => x % 2)' \
| js '_.reverse(x)' \
>> file
> file

# or in chain
echo '[1,2,3,4]' \
| js '_(x).filter(x => x %2).reverse().value()' \
>> file
| js '_(x).filter(x => x % 2).reverse().value()' \
> file
```

Don't forget to take an alias if you want.
Expand All @@ -78,6 +78,7 @@ alias js="jayin"
- `-c`: shortcut of exec(cmd)
- `x`: current input value
- `i`: current index value (with -e)
- `_`: lodash
- `exec(cmd)`: child_process.execSync(cmd)

jayin is based on [through2](https://github.com/rvagg/through2).
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// todo: maybe JacksonTian/bufferhelper?
// todo: minimist for friendly cli handling?
// vm: https://nodejs.org/api/vm.html
'use strict'
var through2 = require('through2')
var _ = require('lodash')
var cp = require('child_process')
Expand Down
22 changes: 9 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/* global describe, it, after */
'use strict'
const BufferHelper = require('bufferhelper')
const assert = require('assert')
const chmodSync = require('child_process').chmodSync
const execSync = require('child_process').execSync
const spawn = require('child_process').spawn
const cp = require('child_process')
const fs = require('fs')

// todo: more test cases

// execSync('chmod +x ./index.js')
// execSync('alias js="./index.js"') // not work with spawn

describe('jayin', () => {
it ('x', (done) => {
const helper = new BufferHelper()
const js = spawn('node', ['./index.js', 'x.slice(2, 4)'])
const js = cp.spawn('node', ['./index.js', 'x.slice(2, 4)'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[3,4]')
done()
Expand All @@ -27,7 +23,7 @@ describe('jayin', () => {

it('-t', (done) => {
const helper = new BufferHelper()
const js = spawn('node', ['./index.js', '-t', 'x.slice(1, -1).replace(/,/g, `\n`)'])
const js = cp.spawn('node', ['./index.js', '-t', 'x.slice(1, -1).replace(/,/g, `\n`)'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '1\n2\n3\n4\n5')
done()
Expand All @@ -40,7 +36,7 @@ describe('jayin', () => {

it('exprs', (done) => {
const helper = new BufferHelper()
const js = spawn('node', ['./index.js', 'x.filter(x => x % 2)', 'x.reverse()'])
const js = cp.spawn('node', ['./index.js', 'x.filter(x => x % 2)', 'x.reverse()'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[3,1]')
done()
Expand All @@ -53,7 +49,7 @@ describe('jayin', () => {

it('no expr', (done) => {
const helper = new BufferHelper()
const js = spawn('node', ['./index.js'])
const js = cp.spawn('node', ['./index.js'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[1,2,3,4]')
done()
Expand All @@ -66,7 +62,7 @@ describe('jayin', () => {

it('_, _.filter, _.reverse', (done) => {
const helper = new BufferHelper()
const js = spawn('node', ['./index.js', '_(x).filter(x => x % 2).reverse().value()'])
const js = cp.spawn('node', ['./index.js', '_(x).filter(x => x % 2).reverse().value()'])
js.stdout.once('end', () => {
assert.equal(helper.toString(), '[3,1]')
done()
Expand All @@ -82,12 +78,12 @@ describe('jayin', () => {
// const env = { count: 0 }
// execSync('count=0')
const helper = new BufferHelper()
const js = spawn('node', ['./index.js', '-e', '-c', 'echo "${i}: ${x}" >> tmp'])
const js = cp.spawn('node', ['./index.js', '-e', '-c', 'echo "${i}: ${x}" >> tmp'])
js.stdout.once('end', () => {
// assert.equal(execSync('echo $count').toString(), '15')
// assert.equal(env.count, '15')
assert.equal(helper.toString(), '["a","b","c"]') // in chain
assert.equal(execSync('cat tmp').toString(), '0: a\n1: b\n2: c\n')
assert.equal(cp.execSync('cat tmp').toString(), '0: a\n1: b\n2: c\n')
done()
})
js.stdout.on('data', (chunk) => {
Expand Down

0 comments on commit eb2f96f

Please sign in to comment.