Skip to content

Commit

Permalink
return string by default and add extra method for returning buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 17, 2014
1 parent 0ec4bd2 commit 665c240
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
'use strict';
module.exports = function (cb) {
var ret = '';

process.stdin.setEncoding('utf8');

process.stdin.on('data', function (chunk) {
ret += chunk;
});

process.stdin.on('end', function () {
cb(ret);
});
};

module.exports.buffer = function (cb) {
var ret = [];
var len = 0;

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "echo unicorns | node test.js"
"test": "echo unicorns | node test.js && echo unicorns | node test-buffer.js"
},
"files": [
"index.js"
Expand All @@ -29,7 +29,7 @@
"stream"
],
"devDependencies": {
"ava": "0.0.3",
"ava": "0.0.4",
"buffer-equal": "0.0.1"
}
}
13 changes: 12 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $ npm install --save get-stdin
var stdin = require('get-stdin');

stdin(function (data) {
console.log(data.toString());
console.log(data);
//=> unicorns
});
```
Expand All @@ -28,6 +28,17 @@ unicorns
```


## API

### stdin(callback)

Get `stdin` as a string.

### stdin.buffer(callback)

Get `stdin` as a buffer.


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
13 changes: 13 additions & 0 deletions test-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
var equal = require('buffer-equal');
var test = require('ava');
var stdin = require('./');

test('get stdin as a buffer', function (t) {
t.plan(2);

stdin.buffer(function (data) {
t.assert(equal(data, new Buffer('unicorns\n')));
t.assert(data.toString().trim() === 'unicorns');
});
});
8 changes: 3 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
var equal = require('buffer-equal');
var test = require('ava');
var stdin = require('./');

test('should get stdin', function (t) {
t.plan(2);
test('get stdin', function (t) {
t.plan(1);

stdin(function (data) {
t.assert(equal(data, new Buffer('unicorns\n')));
t.assert(data.toString().trim() === 'unicorns');
t.assert(data.trim() === 'unicorns');
});
});

0 comments on commit 665c240

Please sign in to comment.