-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (69 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*!
* always-stream <https://github.com/hybridables/always-stream>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var utils = require('./utils')
/**
* > Create a stream from any value.
*
* **Example**
*
* ```js
* var fs = require('fs')
* var toStream = require('always-stream')
*
* var numberStream = toStream(123)
* numberStream().on('data', function (val) {
* console.log(val) // => 123
* })
* var stringStream = toStream('str foo')
* stringStream().on('data', function (val) {
* console.log(val) // => 'str foo'
* })
*
* var readFile = toStream(fs.readFile)
* readFile('package.json', 'utf8')
* .on('data', function (val) {
* var json = JSON.parse(val)
* console.log(json.name) // => 'always-stream'
* })
* .once('error', console.error)
* .once('end', console.error)
*
* // or sync functions
* var statFile = toStream(fs.statSync)
* statFile('package.json')
* .on('data', function (stats) {
* console.log(stats) // => stat object
* })
* .once('error', console.error)
* .once('end', console.error)
*
* // also works for native functions
* // like JSON.parse and JSON.stringify
* // by respect optional arguments
* var stringifyStream = toStream(JSON.stringify)
* stringifyStream({ foo: 'bar' }, null, 2)
* .on('data', function (val) {
* console.log(val) // => '{\n "foo": "bar"\n}'
* })
* .once('error', console.error)
* ```
*
* @param {Mixed} `val` Any type of value.
* @param {Object|Function=} `[opts]` Directly passed to [value2stream][], [callback2stream][] and [through2][].
* @param {Function} `[Promize]` Promise constructor to be used when no support for native Promise. Can be 2nd argument.
* @return {Function} That when executed, returns `TransformStream`, using [through2][]
* @api public
*/
module.exports = function alwaysStream (val, opts, Promize) {
if (typeof val === 'function') {
return utils.callback2stream(val, opts, Promize)
}
return function () {
return utils.value2stream(val, opts, Promize)
}
}