4.0.0-0 • Published 7 years ago
deep-extend-stream v4.0.0-0
deep-extend-stream
Recursively extend the object in a stream
const deepExtend = require('deep-extend-stream');
const target = {foo: {bar: 123}};
const deepExtendStream = deepExtend(target);
deepExtendStream.write({foo: {baz: 'Hello'}});
deepExtendStream.write({qux: 'World'});
deepExtendStream.on('finish', () => {
target; //=> {foo: {bar: 123, baz: 'Hello'}, qux: 'World'}
});
deepExtendStream.end();
Installation
npm install deep-extend-stream
API
const deepExtend = require('deep-extend-stream');
deepExtend(target,)
target: Object
or Array
callback: Function
Return: Object
(stream.Transform)
It returns a transform stream that recursively extends the target object with passed objects (what is called "deep extend").
Target object is optional ({}
by default).
const deepExtend = require('deep-extend-stream');
const deepExtendStream = deepExtend();
deepExtendStream
.on('finish', function() {
// this._target is an internal property to keep target object
this._target;
/*=> {
'0': 'a',
'1': {
b: 'c',
d: 'e'
},
'2': 'f'
} */
})
.write(['a', {'b': 'c'}, 'f'])
.write({'1': {'d': 'e'}})
.end();
callback(target)
You can specify a function to be called on finish
event. It will be called with the target object as its first argument.
const deepExtend = require('deep-extend-stream');
deepExtend(target => {
target; //=> [0, 1, 2]
}).end([0, 1, 2]);
deepExtend([0, 1, 2], target => {
target; //=> [0, 1, 2]
}).end();
License
Copyright (c) 2014 - 2017 Shinnosuke Watanabe
Licensed under the MIT License.