Comparing version 1.0.0 to 1.1.0
24
index.js
'use strict'; | ||
var split = require('split'); | ||
var through = require('through'); | ||
@@ -10,9 +11,6 @@ module.exports = urine; | ||
var opts = getOptions(options); | ||
var piped = strIn.pipe(split(opts.pattern)); | ||
piped.on('data', function onStdin(chunk) { | ||
if (shouldSample(opts.probability)) { | ||
strOut.write(chunk + "\n"); | ||
} | ||
}); | ||
strIn.pipe(getSplitTransform(opts)) | ||
.pipe(getSampleTransform(opts)) | ||
.pipe(strOut); | ||
} | ||
@@ -34,2 +32,16 @@ | ||
// getSampleTransform :: Object -> Stream | ||
function getSampleTransform(opts) { | ||
return through(function sampleTransform(chunk) { | ||
if (shouldSample(opts.probability)) { | ||
this.queue(chunk + "\n"); | ||
} | ||
}); | ||
} | ||
// getSplitTransform :: Object -> Stream | ||
function getSplitTransform(opts) { | ||
return split(opts.pattern); | ||
} | ||
// shouldSample :: Float|Integer -> Boolean | ||
@@ -36,0 +48,0 @@ function shouldSample(probability) { |
{ | ||
"name": "urine", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Sample a data stream", | ||
@@ -23,3 +23,4 @@ "keywords": ["cli", "pipe", "sample", "stream", "test"], | ||
"commander": "~2.3", | ||
"split": "~0.3" | ||
"split": "~0.3", | ||
"through": "~2.3" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
@@ -17,3 +17,4 @@ # Urine | ||
Typical usage is from the command line with the `urine` command. To get help or | ||
usage information, try `urine -h`. | ||
usage information, try `urine -h`. Some contrived examples of using the command | ||
are shown below. | ||
```bash | ||
@@ -35,2 +36,8 @@ # Sample the input.txt file, splitting around newlines with a probability of 0.1 | ||
$ tail -f /var/log/syslog | urine -p .1 | ||
# Sample the syslog file, grepping for "myapp" with a probability of 0.5 | ||
$ tail -f /var/log/syslog | grep --line-buffered 'myapp' | urine -p .5 | ||
# Sample output from a stream of `date` output with a probability of 0.1 | ||
$ while true; do date; sleep 1; done | urine -p .1 | ||
``` | ||
@@ -37,0 +44,0 @@ |
240
test.js
@@ -7,5 +7,6 @@ 'use strict'; | ||
var stream = require('stream'); | ||
var through = require('through'); | ||
suite('urine:', function () { | ||
var nativeRand, opts, strIn, strOut, strOutWrite, strPipe; | ||
var nativeRand, opts, strIn, strOut, strOutSpy; | ||
@@ -27,18 +28,4 @@ // monkeyPatchMathRand :: Function -> Undefined | ||
setup(function () { | ||
strIn = new stream.Writable(); | ||
strOut = new stream.Writable(); | ||
strPipe = new stream.Writable(); | ||
sinon.stub(strIn, 'pipe', function () { | ||
return strPipe; | ||
}); | ||
sinon.stub(strOut, 'write', function () { | ||
return strOut; | ||
}); | ||
sinon.stub(strPipe, 'on', function (type, fn) { | ||
strOutWrite = fn; | ||
return strPipe; | ||
}); | ||
strIn = new stream.PassThrough(); | ||
strOut = new stream.PassThrough(); | ||
}); | ||
@@ -54,13 +41,40 @@ | ||
test('`urine(strIn, strOut)` calls `strIn.pipe()`', function () { | ||
urine(strIn, strOut); | ||
suite('pipes:', function () { | ||
var strSplit, strTransform; | ||
assert.isTrue(strIn.pipe.calledOnce); | ||
}); | ||
setup(function () { | ||
strSplit = new stream.PassThrough(); | ||
strTransform = new stream.PassThrough(); | ||
test('`urine(strIn, strOut)` calls `strPipe.on()`', function () { | ||
urine(strIn, strOut); | ||
sinon.stub(strIn, 'pipe', function () { | ||
return strSplit; | ||
}); | ||
sinon.stub(strSplit, 'pipe', function () { | ||
return strTransform; | ||
}); | ||
assert.isTrue(strPipe.on.calledOnce); | ||
assert.isTrue(strPipe.on.calledWith('data')); | ||
strTransform.pipe = sinon.spy(); | ||
}); | ||
test('`urine(strIn, strOut)` calls `strIn.pipe()`', function () { | ||
urine(strIn, strOut); | ||
assert.isTrue(strIn.pipe.calledOnce); | ||
assert.isObject(strIn.pipe.args[0][0]); | ||
}); | ||
test('`urine(strIn, strOut)` calls `strSplit.pipe()`', function () { | ||
urine(strIn, strOut); | ||
assert.isTrue(strSplit.pipe.calledOnce); | ||
assert.isObject(strSplit.pipe.args[0][0]); | ||
}); | ||
test('`urine(strIn, strOut)` calls `strTransform.pipe()`', function () { | ||
urine(strIn, strOut); | ||
assert.isTrue(strTransform.pipe.calledOnce); | ||
assert.isObject(strTransform.pipe.args[0][0]); | ||
}); | ||
}); | ||
@@ -70,2 +84,4 @@ | ||
setup(function () { | ||
strIn.resume(); | ||
strOut.resume(); | ||
opts = { | ||
@@ -76,4 +92,6 @@ probability: 1 | ||
test('`urine(strIn, strOut, opts)` calls `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` pipes to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -84,11 +102,18 @@ monkeyPatchMathRand(function mathRandForUrineTesting1234() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.isTrue(strOut.write.calledOnce); | ||
assert.isTrue(strOut.write.calledWith(data + "\n")); | ||
assert.equal(count, 1); | ||
assert.equal(piped, data); | ||
}); | ||
test('`urine(strIn, strOut, opts)` calls `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` pipes to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -99,7 +124,12 @@ monkeyPatchMathRand(function mathRandForUrineTesting6789() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.isTrue(strOut.write.calledOnce); | ||
assert.isTrue(strOut.write.calledWith(data + "\n")); | ||
assert.equal(count, 1); | ||
assert.equal(piped, data); | ||
}); | ||
@@ -110,2 +140,4 @@ }); | ||
setup(function () { | ||
strIn.resume(); | ||
strOut.resume(); | ||
opts = { | ||
@@ -116,4 +148,6 @@ probability: .75 | ||
test('`urine(strIn, strOut, opts)` calls `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` pipes to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -124,11 +158,18 @@ monkeyPatchMathRand(function mathRandForUrineTesting1234() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.isTrue(strOut.write.calledOnce); | ||
assert.isTrue(strOut.write.calledWith(data + "\n")); | ||
assert.equal(count, 1); | ||
assert.equal(piped, data); | ||
}); | ||
test('`urine(strIn, strOut, opts)` calls `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` pipes to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -139,7 +180,12 @@ monkeyPatchMathRand(function mathRandForUrineTesting6789() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.isTrue(strOut.write.calledOnce); | ||
assert.isTrue(strOut.write.calledWith(data + "\n")); | ||
assert.equal(count, 1); | ||
assert.equal(piped, data); | ||
}); | ||
@@ -150,2 +196,4 @@ }); | ||
setup(function () { | ||
strIn.resume(); | ||
strOut.resume(); | ||
opts = { | ||
@@ -156,4 +204,6 @@ probability: .5 | ||
test('`urine(strIn, strOut, opts)` calls `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` pipes to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -164,11 +214,18 @@ monkeyPatchMathRand(function mathRandForUrineTesting1234() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.isTrue(strOut.write.calledOnce); | ||
assert.isTrue(strOut.write.calledWith(data + "\n")); | ||
assert.equal(count, 1); | ||
assert.equal(piped, data); | ||
}); | ||
test('`urine(strIn, strOut, opts)` doesn\'t call `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` doesn\'t pipe to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -179,6 +236,12 @@ monkeyPatchMathRand(function mathRandForUrineTesting6789() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.equal(strOut.write.callCount, 0); | ||
assert.equal(count, 0); | ||
assert.equal(piped, ''); | ||
}); | ||
@@ -189,2 +252,4 @@ }); | ||
setup(function () { | ||
strIn.resume(); | ||
strOut.resume(); | ||
opts = { | ||
@@ -195,4 +260,6 @@ probability: .25 | ||
test('`urine(strIn, strOut, opts)` calls `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` pipes to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -203,11 +270,18 @@ monkeyPatchMathRand(function mathRandForUrineTesting1234() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.isTrue(strOut.write.calledOnce); | ||
assert.isTrue(strOut.write.calledWith(data + "\n")); | ||
assert.equal(count, 1); | ||
assert.equal(piped, data); | ||
}); | ||
test('`urine(strIn, strOut, opts)` doesn\'t call `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` doesn\'t pipe to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -218,6 +292,12 @@ monkeyPatchMathRand(function mathRandForUrineTesting6789() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.equal(strOut.write.callCount, 0); | ||
assert.equal(count, 0); | ||
assert.equal(piped, ''); | ||
}); | ||
@@ -228,2 +308,4 @@ }); | ||
setup(function () { | ||
strIn.resume(); | ||
strOut.resume(); | ||
opts = { | ||
@@ -234,4 +316,6 @@ probability: 0 | ||
test('`urine(strIn, strOut, opts)` doesn\'t call `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` doesn\'t pipe to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -242,10 +326,18 @@ monkeyPatchMathRand(function mathRandForUrineTesting1234() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.equal(strOut.write.callCount, 0); | ||
assert.equal(count, 0); | ||
assert.equal(piped, ''); | ||
}); | ||
test('`urine(strIn, strOut, opts)` doesn\'t call `strOut.write()`', function () { | ||
var data = 'foo'; | ||
test('`urine(strIn, strOut, opts)` doesn\'t pipe to `strOut`', function () { | ||
var data = "foo\n"; | ||
var count = 0; | ||
var piped = ''; | ||
@@ -256,8 +348,14 @@ monkeyPatchMathRand(function mathRandForUrineTesting6789() { | ||
strOut.pipe(through(function (chunk) { | ||
count += 1; | ||
piped += chunk; | ||
})); | ||
urine(strIn, strOut, opts); | ||
strOutWrite(data); | ||
strIn.push(data); | ||
assert.equal(strOut.write.callCount, 0); | ||
assert.equal(count, 0); | ||
assert.equal(piped, ''); | ||
}); | ||
}); | ||
}); |
16357
9
359
81
3
+ Addedthrough@~2.3