Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.4.3 can call a series more than once

  • Loading branch information...
commit a05285067454b6feed1f60cb20c4f70092f75350 1 parent febcf2d
@seanhess seanhess authored
Showing with 37 additions and 13 deletions.
  1. +7 −5 index.coffee
  2. +14 −7 index.js
  3. +1 −1  package.json
  4. +15 −0 test.coffee
View
12 index.coffee
@@ -58,16 +58,17 @@ fjs = (_) ->
# uses async library, but functions don't require arrays
# also allows you to CREATE a series
makeSeries = (funcs...) ->
- index = 0
-
if anyUndefined funcs then throw new Error "null function in series"
- nextInSeries = (args..., cb) ->
- func = funcs[index++]
+ nextInSeries = (args..., index, cb) ->
+ func = funcs[index]
if not func? then return cb null, args...
func args..., (err, args...) ->
if err? then return cb err
- nextInSeries args..., cb
+ nextInSeries args..., (index+1), cb
+
+ start = (args..., cb) ->
+ nextInSeries args..., 0, cb
# make a series and call it immediately
# TODO is there a way to automatically detect this, like curry does?
@@ -82,6 +83,7 @@ fjs = (_) ->
seriesChain cb
toAsync = (f) ->
+ if not f? then return null
(args..., cb) ->
process.nextTick ->
res = f args...
View
21 index.js
@@ -70,16 +70,15 @@
chain: chain
};
makeSeries = function() {
- var funcs, index, nextInSeries;
+ var funcs, nextInSeries, start;
funcs = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- index = 0;
if (anyUndefined(funcs)) {
throw new Error("null function in series");
}
- return nextInSeries = function() {
- var args, cb, func, _i;
- args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
- func = funcs[index++];
+ nextInSeries = function() {
+ var args, cb, func, index, _i;
+ args = 3 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 2) : (_i = 0, []), index = arguments[_i++], cb = arguments[_i++];
+ func = funcs[index];
if (!(func != null)) {
return cb.apply(null, [null].concat(__slice.call(args)));
}
@@ -89,9 +88,14 @@
if (err != null) {
return cb(err);
}
- return nextInSeries.apply(null, __slice.call(args).concat([cb]));
+ return nextInSeries.apply(null, __slice.call(args).concat([index + 1], [cb]));
}]));
};
+ return start = function() {
+ var args, cb, _i;
+ args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
+ return nextInSeries.apply(null, __slice.call(args).concat([0], [cb]));
+ };
};
series = function() {
var cb, funcs, seriesChain, _i;
@@ -104,6 +108,9 @@
return seriesChain(cb);
};
toAsync = function(f) {
+ if (!(f != null)) {
+ return null;
+ }
return function() {
var args, cb, _i;
args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++];
View
2  package.json
@@ -2,7 +2,7 @@
"author": "Sean Hess <sean@i.tv> (http://seanhess.github.com)",
"name": "fjs",
"description": "Utilities for Functional Javascript",
- "version": "0.4.2",
+ "version": "0.4.3",
"homepage": "http://github.com/idottv/fjs",
"scripts": {
"test":"node_modules/.bin/coffee -c index.coffee && node_modules/.bin/mocha -R spec --compilers coffee:coffee-script test.coffee"
View
15 test.coffee
@@ -137,6 +137,21 @@ describe 'fjs', ->
asdf = (cb) -> cb null, "asdf"
+ it 'can be called twice', (done) ->
+ one = (name, cb) -> cb null, "hi #{name}"
+ two = (msg, cb) -> cb null, msg+"!"
+ both = fjs.makeSeries one, two
+
+ both "sean", (err, msg) ->
+ assert.ifError err
+ assert.equal msg, "hi sean!"
+
+ both "bob", (err, msg) ->
+ assert.ifError err
+ assert.equal msg, "hi bob!", "strange results on call twice"
+ done()
+
+
describe 'call', ->
it 'should call functions on the object', ->
obj =
Please sign in to comment.
Something went wrong with that request. Please try again.