Skip to content

Commit

Permalink
Merge pull request #247 from quarterto/wrapcallback-mappinghint
Browse files Browse the repository at this point in the history
Implement mappingHint for wrapCallback (Fixes #246, #334).
  • Loading branch information
vqvu committed Jul 6, 2015
2 parents 5db4fc1 + f576e5b commit 531eea6
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 33 deletions.
92 changes: 59 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ var Decoder = require('string_decoder').StringDecoder;
* event emitter as the two arguments to the constructor and the first
* argument emitted to the event handler will be written to the new Stream.
*
* You can also pass as an optional third parameter a function, an array of strings
* or a number. In this case the event handler will try to wrap the arguments emitted
* to it and write this object to the new stream.
* You can pass a mapping hint as the third argument, which specifies how
* event arguments are pushed into the stream. If no mapping hint is provided,
* only the first value emitted with the event to the will be pushed onto the
* Stream. (NB: in a future version, if more than one argument is passed to the
* event, an array of all arguments will be pushed to the stream; currently the
* extra arguments are dropped)
*
* If mappingHint is a number, an array of that length will be pushed
* onto the stream, containing exactly that many parameters from the event. If
* it's an array, it's used as keys to map the arguments into an object which
* is pushed to the tream. If the mapping hint is a function, it's called with
* the event arguments, and the returned value is pushed.
*
* **Promise -** Accepts an ES6 / jQuery style promise and returns a
* Highland Stream which will emit a single value (or an error).
Expand Down Expand Up @@ -415,6 +424,34 @@ function iteratorGenerator(it) {
});
}

function hintMapper(mappingHint) {
var mappingHintType = (typeof mappingHint);
var mapper;

if (mappingHintType === 'function') {
mapper = mappingHint;
}
else if (mappingHintType === 'number') {
mapper = function () {
return slice.call(arguments, 0, mappingHint);
};
}
else if (_.isArray(mappingHint)) {
mapper = function () {
var args = arguments;
return mappingHint.reduce(function (ctx, hint, idx) {
ctx[hint] = args[idx];
return ctx;
}, {});
};
}
else {
mapper = function (x) { return x; };
}

return mapper;
}

/**
* Actual Stream constructor wrapped the the main exported function
*/
Expand Down Expand Up @@ -553,29 +590,7 @@ function Stream(/*optional*/xs, /*optional*/ee, /*optional*/mappingHint) {
}
}
else if (_.isString(xs)) {
var mappingHintType = (typeof mappingHint);
var mapper;

if (mappingHintType === 'function') {
mapper = mappingHint;
}
else if (mappingHintType === 'number') {
mapper = function () {
return slice.call(arguments, 0, mappingHint);
};
}
else if (_.isArray(mappingHint)) {
mapper = function () {
var args = arguments;
return mappingHint.reduce(function (ctx, hint, idx) {
ctx[hint] = args[idx];
return ctx;
}, {});
};
}
else {
mapper = function (x) { return x; };
}
var mapper = hintMapper(mappingHint);

ee.on(xs, function () {
var ctx = mapper.apply(this, arguments);
Expand Down Expand Up @@ -3982,15 +3997,20 @@ _.log = function () {
/**
* Wraps a node-style async function which accepts a callback, transforming
* it to a function which accepts the same arguments minus the callback and
* returns a Highland Stream instead. Only the first argument to the
* callback (or an error) will be pushed onto the Stream. The wrapped
* function keeps its context, so you can safely use it as a method without
* binding (see the second example below).
* returns a Highland Stream instead. The wrapped function keeps its context,
* so you can safely use it as a method without binding (see the second
* example below).
*
* wrapCallback also accepts a mapping hint, which specifies how callback
* arguments are pushed to the stream. This can be a function, number, or
* array. See the documentation on [EventEmitter Stream Objects](#Stream Objects)
* for details.
*
* @id wrapCallback
* @section Utils
* @name _.wrapCallback(f)
* @param {Function} f - the node-style function to wrap
* @param {Function | Number | Array} mappingHint - how to pass the arguments to the callback (optional)
* @api public
*
* var fs = require('fs');
Expand All @@ -4012,17 +4032,23 @@ _.log = function () {
* Reader.prototype.readStream = _.wrapCallback(Reader.prototype.read);
*/

_.wrapCallback = function (f) {
/*eslint-disable no-multi-spaces */
_.wrapCallback = function (f, /*optional*/mappingHint) {
/*eslint-enable no-multi-spaces */
var mapper = hintMapper(mappingHint);

return function () {
var self = this;
var args = slice.call(arguments);
return _(function (push) {
var cb = function (err, x) {
var cb = function (err) {
if (err) {
push(err);
}
else {
push(null, x);
var cbArgs = slice.call(arguments, 1);
var v = mapper.apply(this, cbArgs);
push(null, v);
}
push(null, nil);
};
Expand Down
56 changes: 56 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,19 @@ exports['wrap EventEmitter (or jQuery) on handler with args wrapping by array']
});
};

exports['wrap EventEmitter default mapper discards all but first arg'] = function (test) {
var ee = {
on: function (name, f) {
test.same(name, 'myevent');
f(1, 2, 3);
}
};
_('myevent', ee).each(function (x) {
test.same(x, 1);
test.done()
});
};

exports['sequence'] = function (test) {
_.sequence([[1,2], [3], [[4],5]]).toArray(function (xs) {
test.same(xs, [1,2,3,[4],5]);
Expand Down Expand Up @@ -5755,6 +5768,49 @@ exports['wrapCallback - errors'] = function (test) {
test.done();
};

exports['wrapCallback with args wrapping by function'] = function (test) {
function f(cb) {
cb(null, 1, 2, 3);
}
function mapper(){
return Array.prototype.slice.call(arguments);
}
_.wrapCallback(f, mapper)().each(function (x) {
test.same(x, [1, 2, 3]);
test.done();
});
};

exports['wrapCallback with args wrapping by number'] = function (test) {
function f(cb) {
cb(null, 1, 2, 3);
}
_.wrapCallback(f, 2)().each(function (x) {
test.same(x, [1, 2]);
test.done();
});
};

exports['wrapCallback with args wrapping by array'] = function (test) {
function f(cb) {
cb(null, 1, 2, 3);
}
_.wrapCallback(f, ['one', 'two', 'three'])().each(function (x) {
test.same(x, {'one': 1, 'two': 2, 'three': 3});
test.done()
});
};

exports['wrapCallback default mapper discards all but first arg'] = function (test) {
function f(cb) {
cb(null, 1, 2, 3);
}
_.wrapCallback(f)().each(function (x) {
test.same(x, 1);
test.done()
});
};

exports['streamifyAll'] = {
'throws when passed a non-function non-object': function (test) {
test.throws(function () {
Expand Down

0 comments on commit 531eea6

Please sign in to comment.