Comparing version 0.3.4 to 0.4.0
353
lib/after.js
@@ -1,238 +0,243 @@ | ||
(function _anonymousWrapper() { | ||
"use strict"; | ||
var slice = [].slice | ||
var slice = [].slice; | ||
after.forEach = handleMultipleArguments(forEach) | ||
after.map = handleMultipleArguments(map) | ||
after.reduce = handleMultipleArguments(reduce) | ||
after.filter = handleMultipleArguments(filter) | ||
after.some = handleMultipleArguments(some) | ||
after.every = handleMultipleArguments(every) | ||
after.reduceRight = handleMultipleArguments(reduceRight) | ||
after.unpack = unpack | ||
after.flow = flow | ||
after.forEach = handleMultipleArguments(forEach); | ||
after.map = handleMultipleArguments(map); | ||
after.reduce = handleMultipleArguments(reduce); | ||
after.filter = handleMultipleArguments(filter); | ||
after.some = handleMultipleArguments(some); | ||
after.every = handleMultipleArguments(every); | ||
after.reduceRight = handleMultipleArguments(reduceRight); | ||
after.unpack = unpack; | ||
module.exports = after; | ||
if (typeof module !== "undefined") { | ||
module.exports = after; | ||
} else { | ||
window.after = after; | ||
} | ||
/* | ||
after takes a callback and returns a proxy function. If you invoke the | ||
proxy function count number of times then the callback fires | ||
/* | ||
after takes a callback and returns a proxy function. If you invoke the | ||
proxy function count number of times then the callback fires | ||
@param Number count - number of times you need to invoke proxy | ||
before the callback fires | ||
@param Function callback - callback to fire | ||
@param Number count - number of times you need to invoke proxy | ||
before the callback fires | ||
@param Function callback - callback to fire | ||
@return Function proxy { count: Number } - proxy function you | ||
need to invoke count times before callback fires. | ||
The count property of the proxy function is the internal | ||
counter for when the callback should fire. | ||
If the internal counter is 0 then the callback fires. | ||
*/ | ||
function after(count, callback) { | ||
var results = []; | ||
@return Function proxy { count: Number } - proxy function you | ||
need to invoke count times before callback fires. | ||
The count property of the proxy function is the internal | ||
counter for when the callback should fire. | ||
If the internal counter is 0 then the callback fires. | ||
*/ | ||
function after(count, callback) { | ||
var results = [] | ||
proxy.count = count; | ||
proxy.count = count | ||
return (count === 0) ? callback() : proxy; | ||
return (count === 0) ? callback() : proxy | ||
function proxy() { | ||
results.push(arguments); | ||
function proxy() { | ||
results.push(arguments) | ||
--proxy.count === 0 && callback.apply(this, results); | ||
} | ||
--proxy.count === 0 && callback.apply(this, results) | ||
} | ||
} | ||
function unpack(data) { | ||
var obj = {}; | ||
[].forEach.call(data, function (tuple) { | ||
obj[tuple[0]] = tuple[1]; | ||
}); | ||
return obj; | ||
} | ||
function unpack(data) { | ||
var obj = {} | ||
;[].forEach.call(data, function (tuple) { | ||
obj[tuple[0]] = tuple[1] | ||
}); | ||
return obj | ||
} | ||
/* | ||
forEach takes a set and invokes the callback on it for each key | ||
in the set. The callback should invoke the next function passed | ||
to it when it's done. | ||
*/ | ||
function forEach(obj, callback, context, next, keys, length) { | ||
keys.forEach(loop); | ||
/* | ||
forEach takes a set and invokes the callback on it for each key | ||
in the set. The callback should invoke the next function passed | ||
to it when it's done. | ||
*/ | ||
function forEach(obj, callback, context, next, keys, length) { | ||
keys.forEach(loop) | ||
function loop(key) { | ||
var value = obj[key]; | ||
function loop(key) { | ||
var value = obj[key] | ||
invokeCallback(callback, context, value, key, obj, proxy); | ||
invokeCallback(callback, context, value, key, obj, proxy) | ||
function proxy(err) { | ||
if (err) return next(err); | ||
if (--length === 0) { | ||
return next(); | ||
} | ||
function proxy(err) { | ||
if (err) return next(err) | ||
if (--length === 0) { | ||
return next() | ||
} | ||
} | ||
} | ||
} | ||
function map(obj, callback, context, next, keys, length) { | ||
var returnValue = createReturnValue(obj); | ||
keys.forEach(loop); | ||
function map(obj, callback, context, next, keys, length) { | ||
var returnValue = createReturnValue(obj) | ||
keys.forEach(loop) | ||
function loop(key) { | ||
var value = obj[key]; | ||
function loop(key) { | ||
var value = obj[key] | ||
invokeCallback(callback, context, value, key, obj, proxy); | ||
invokeCallback(callback, context, value, key, obj, proxy) | ||
function proxy(err, value) { | ||
if (err) return next(err); | ||
returnValue[key] = value; | ||
if (--length === 0) { | ||
return next(null, returnValue); | ||
} | ||
function proxy(err, value) { | ||
if (err) return next(err) | ||
returnValue[key] = value | ||
if (--length === 0) { | ||
return next(null, returnValue) | ||
} | ||
} | ||
} | ||
} | ||
function reduce(obj, callback, memo, next, keys, length) { | ||
if (memo === null) { | ||
memo = obj[keys.shift()]; | ||
length--; | ||
function reduce(obj, callback, memo, next, keys, length) { | ||
if (memo === null) { | ||
memo = obj[keys.shift()] | ||
length-- | ||
} | ||
(function loop() { | ||
var key = keys.shift(), | ||
value = obj[key] | ||
if (length-- === 0) { | ||
return next(null, memo) | ||
} | ||
(function loop() { | ||
var key = keys.shift(), | ||
value = obj[key]; | ||
if (length-- === 0) { | ||
return next(null, memo); | ||
} | ||
invokeCallback(callback, null, memo, value, key, obj, proxy) | ||
invokeCallback(callback, null, memo, value, key, obj, proxy); | ||
function proxy(err, value) { | ||
if (err) return next(err) | ||
memo = value | ||
loop() | ||
} | ||
}()); | ||
} | ||
function proxy(err, value) { | ||
if (err) return next(err); | ||
memo = value; | ||
loop(); | ||
} | ||
}()); | ||
} | ||
function filter(obj, callback, context, next, keys, length) { | ||
var returnValue = createReturnValue(obj) | ||
keys.forEach(loop) | ||
function loop (key) { | ||
var value = obj[key] | ||
function filter(obj, callback, context, next, keys, length) { | ||
var returnValue = createReturnValue(obj); | ||
keys.forEach(loop); | ||
function loop (key) { | ||
var value = obj[key]; | ||
invokeCallback(callback, context, value, key, obj, proxy) | ||
invokeCallback(callback, context, value, key, obj, proxy); | ||
function proxy(err, bool) { | ||
if (err) return next(err); | ||
if (bool) returnValue[key] = value; | ||
if (--length === 0) { | ||
return next(null, returnValue); | ||
} | ||
function proxy(err, bool) { | ||
if (err) return next(err) | ||
if (bool) returnValue[key] = value | ||
if (--length === 0) { | ||
return next(null, returnValue) | ||
} | ||
} | ||
} | ||
} | ||
function some(obj, callback, context, next, keys, length) { | ||
keys.forEach(loop); | ||
function some(obj, callback, context, next, keys, length) { | ||
keys.forEach(loop) | ||
function loop (key) { | ||
var value = obj[key]; | ||
function loop (key) { | ||
var value = obj[key] | ||
invokeCallback(callback, context, value, key, obj, proxy); | ||
invokeCallback(callback, context, value, key, obj, proxy) | ||
function proxy(err, bool) { | ||
if (err) { | ||
next(err); | ||
next = noop; | ||
} else if (bool === true) { | ||
next(null, true); | ||
next = noop; | ||
} else if (--length === 0) { | ||
next(null, false); | ||
} | ||
function proxy(err, bool) { | ||
if (err) { | ||
next(err) | ||
next = noop | ||
} else if (bool === true) { | ||
next(null, true) | ||
next = noop | ||
} else if (--length === 0) { | ||
next(null, false) | ||
} | ||
} | ||
} | ||
} | ||
function every(obj, callback, context, next, keys, length) { | ||
keys.forEach(loop); | ||
function every(obj, callback, context, next, keys, length) { | ||
keys.forEach(loop) | ||
function loop(key) { | ||
var value = obj[key]; | ||
function loop(key) { | ||
var value = obj[key] | ||
invokeCallback(callback, context, value, key, obj, proxy); | ||
invokeCallback(callback, context, value, key, obj, proxy) | ||
function proxy(err, bool) { | ||
if (err) { | ||
next(err); | ||
next = noop; | ||
} else if (bool === false) { | ||
next(null, false); | ||
next = noop; | ||
} else if (--length === 0) { | ||
next(null, true); | ||
} | ||
function proxy(err, bool) { | ||
if (err) { | ||
next(err) | ||
next = noop | ||
} else if (bool === false) { | ||
next(null, false) | ||
next = noop | ||
} else if (--length === 0) { | ||
next(null, true); | ||
} | ||
} | ||
} | ||
} | ||
function createReturnValue(obj) { | ||
if (Array.isArray(obj)) { | ||
return []; | ||
} else { | ||
return Object.create(Object.getPrototypeOf(obj)); | ||
} | ||
function createReturnValue(obj) { | ||
if (Array.isArray(obj)) { | ||
return [] | ||
} else { | ||
return Object.create(Object.getPrototypeOf(obj)) | ||
} | ||
} | ||
function reduceRight(obj, callback, memo, next, keys, length) { | ||
reduce(obj, callback, memo, next, keys.reverse(), length); | ||
} | ||
function reduceRight(obj, callback, memo, next, keys, length) { | ||
reduce(obj, callback, memo, next, keys.reverse(), length) | ||
} | ||
function noop() { } | ||
function noop() { } | ||
function handleMultipleArguments(f) { | ||
return proxy; | ||
function handleMultipleArguments(f) { | ||
return proxy | ||
function proxy(obj, callback, context, next) { | ||
if (typeof context === "function") { | ||
next = context; | ||
context = null; | ||
} | ||
function proxy(obj, callback, context, next) { | ||
if (typeof context === "function") { | ||
next = context | ||
context = null | ||
} | ||
var keys = Object.keys(obj); | ||
if (keys.length === 0) { | ||
next(null, obj) | ||
} else { | ||
f(obj, callback, context, next, keys, keys.length); | ||
} | ||
var keys = Object.keys(obj) | ||
if (keys.length === 0) { | ||
next(null, obj) | ||
} else { | ||
f(obj, callback, context, next, keys, keys.length) | ||
} | ||
} | ||
} | ||
function invokeCallback(callback, context, memo, value, key, obj, next) { | ||
var callbackLength = callback.length; | ||
function invokeCallback(callback, context, memo, value, key, obj, next) { | ||
var callbackLength = callback.length | ||
if (typeof obj === "function") { | ||
next = obj; | ||
obj = null; | ||
} | ||
if (typeof obj === "function") { | ||
next = obj | ||
obj = null | ||
} | ||
if (callbackLength === 1) { | ||
callback.call(context, next); | ||
} else if (callbackLength === 2) { | ||
callback.call(context, memo, next); | ||
} else if (callbackLength === 3) { | ||
callback.call(context, memo, value, next); | ||
} else if (callbackLength === 4) { | ||
callback.call(context, memo, value, key, next); | ||
} else { | ||
callback.call(context, memo, value, key, obj, next); | ||
if (callbackLength === 1) { | ||
callback.call(context, next) | ||
} else if (callbackLength === 2) { | ||
callback.call(context, memo, next) | ||
} else if (callbackLength === 3) { | ||
callback.call(context, memo, value, next) | ||
} else if (callbackLength === 4) { | ||
callback.call(context, memo, value, key, next) | ||
} else { | ||
callback.call(context, memo, value, key, obj, next) | ||
} | ||
} | ||
function flow(array, context) { | ||
array.reduce(function (memo, outer) { | ||
return function (inner) { | ||
memo.call(context, function () { | ||
var args = [].slice.call(arguments).concat(inner) | ||
outer.apply(context, args) | ||
}) | ||
} | ||
} | ||
}()); | ||
})() | ||
} |
{ | ||
"name": "after", | ||
"description": "after - tiny flow control", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"author": "Raynos <raynos2@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -221,2 +221,19 @@ # After [![Build Status][1]][2] | ||
### <a href="#flow" name="flow">After.flow(array, context)</a> | ||
Creates a flow through all the functions in the array. Each function in the array is passed in the next function as the last argument. Optionally pass in a context which will be the this value for all functions | ||
after.flow([ | ||
function (next) { | ||
next("foo") | ||
}, | ||
function (foo, next) { | ||
assert.equal(foo, "foo") | ||
next() | ||
}, | ||
function () { | ||
assert.deepEqual(this, context) | ||
} | ||
], context) | ||
## Installation | ||
@@ -223,0 +240,0 @@ |
@@ -235,2 +235,34 @@ var assert = require("assert"), | ||
}) | ||
suite("flow", function () { | ||
test("it flows", function (done) { | ||
var count = 0, | ||
stack = [ | ||
function (next) { | ||
assert.equal(++count, 1) | ||
assert.equal(this.foo, "bar") | ||
next("foo") | ||
}, | ||
function (foo, next) { | ||
assert.equal(++count, 2) | ||
assert.equal(this.foo, "bar") | ||
assert.equal(foo, "foo") | ||
next("bar") | ||
}, | ||
function (bar, next) { | ||
assert.equal(++count, 3) | ||
assert.equal(this.foo, "bar") | ||
assert.equal(bar, "bar") | ||
next() | ||
}, | ||
function (next) { | ||
assert.equal(++count, 4) | ||
assert.equal(next, null) | ||
done() | ||
} | ||
] | ||
after.flow(stack, { foo: "bar" }) | ||
}) | ||
}) | ||
}); | ||
@@ -237,0 +269,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26834
435
269