Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add: test.html, fix readme and index.js to clarify browser support

  • Loading branch information...
commit 760fdbcbdbb4d6478114e1f65dcb8e67698bef17 1 parent 02b7147
@tmpvar authored
Showing with 109 additions and 97 deletions.
  1. +1 −1  README.md
  2. +74 −79 index.js
  3. +10 −0 test.html
  4. +24 −17 test.js
View
2  README.md
@@ -266,4 +266,4 @@ console.log(a.get('area')); // outputs '40'
### Browser
-works with a plain ol' script tag or [browserify](https://github.com/substack/node-browserify)
+works with a plain ol' script tag and access it via `window.createChain`
View
153 index.js
@@ -1,100 +1,95 @@
-(function (hasWindow, nop) {
- var expose = {
- createChain : function (layers, coreFn) {
- if (typeof layers === 'function' && !coreFn) {
- coreFn = layers;
- layers = [];
- } else {
- layers = layers || [];
- }
+(function (exports, nop) {
+ function createChain (layers, coreFn) {
+ if (typeof layers === 'function' && !coreFn) {
+ coreFn = layers;
+ layers = [];
+ } else {
+ layers = layers || [];
+ }
- var chainInstance = function (data, fn) {
- if (typeof data === 'function' && !fn) {
- fn = data;
- data = null;
- }
+ var chainInstance = function (data, fn) {
+ if (typeof data === 'function' && !fn) {
+ fn = data;
+ data = null;
+ }
- var
- index = -1,
- errors = [],
- bubbles = [],
- current,
- bubble = function (data) {
+ var
+ index = -1,
+ errors = [],
+ bubbles = [],
+ current,
+ bubble = function (data) {
- if (bubbles.length < 1) {
- if (fn) {
- fn((errors.length) ? errors : null, data);
+ if (bubbles.length < 1) {
+ if (fn) {
+ fn((errors.length) ? errors : null, data);
+ }
+ } else {
+ var
+ bubbleFn = bubbles.pop(),
+ ret = bubbleFn(data, function done(err, data) {
+ if (err) {
+ errors.push(err);
}
- } else {
- var
- bubbleFn = bubbles.pop(),
- ret = bubbleFn(data, function done(err, data) {
- if (err) {
- errors.push(err);
- }
- bubble(data);
- });
+ bubble(data);
+ });
- if (ret !== nop) {
- bubble(ret);
- }
+ if (ret !== nop) {
+ bubble(ret);
}
- },
- capture = function (data) {
- index+=1;
-
- if (index>=layers.length) {
- if (chainInstance.core) {
- chainInstance.core(data, function (err, data) {
- if (err) {
- errors.push(err);
- }
- // called when the core of the chain is complete
- bubble(data);
- });
- } else {
- bubble(data);
- }
- } else if (typeof layers[index] === 'function') {
- layers[index](data, function next(data, bubbleFn) {
- if (typeof bubbleFn === 'function') {
- bubbles.push(bubbleFn);
- } else if (bubbleFn) {
- // handle the case where flows are being composed
- // data is the errors
- if (data) {
- errors.push(data);
- return bubble(bubbleFn);
- }
- data = bubbleFn;
- }
+ }
+ },
+ capture = function (data) {
+ index+=1;
- capture(data);
- }, function cancel(err, data) {
+ if (index>=layers.length) {
+ if (chainInstance.core) {
+ chainInstance.core(data, function (err, data) {
if (err) {
errors.push(err);
}
+ // called when the core of the chain is complete
bubble(data);
});
} else {
- errors.push(new Error('user error! A link in the layers was not a function'));
bubble(data);
}
- };
+ } else if (typeof layers[index] === 'function') {
+ layers[index](data, function next(data, bubbleFn) {
+ if (typeof bubbleFn === 'function') {
+ bubbles.push(bubbleFn);
+ } else if (bubbleFn) {
+ // handle the case where flows are being composed
+ // data is the errors
+ if (data) {
+ errors.push(data);
+ return bubble(bubbleFn);
+ }
+ data = bubbleFn;
+ }
- capture(data);
+ capture(data);
+ }, function cancel(err, data) {
+ if (err) {
+ errors.push(err);
+ }
+ bubble(data);
+ });
+ } else {
+ errors.push(new Error('user error! A link in the layers was not a function'));
+ bubble(data);
+ }
};
- chainInstance.layers = layers;
- chainInstance.core = coreFn;
- return chainInstance;
- }
- };
+ capture(data);
+ };
- if (hasWindow) {
- window.filterchain = expose;
- } else {
- module.exports = expose;
+ chainInstance.layers = layers;
+ chainInstance.core = coreFn;
+ return chainInstance;
}
-})(typeof window !== 'undefined');
+
+ exports.createChain = createChain;
+
+})((typeof exports === "undefined") ? window : exports);
View
10 test.html
@@ -0,0 +1,10 @@
+<html>
+<head>
+ <title>filterchain browser tests</title>
+ <script type="text/javascript" src="index.js"></script>
+ <script type="text/javascript" src="test.js"></script>
+</head>
+<body>
+ check your console.
+</body>
+</html>
View
41 test.js
@@ -1,17 +1,25 @@
var
-fc = require('./'),
ok = function(a, msg) {
if (!a) {
- console.log(new Error(msg));
- process.exit(1);
+ var err = new Error(msg);
+ if (typeof process !== 'undefined') {
+ console.log(err);
+ process.exit(1);
+ } else {
+ throw err;
+ }
}
},
equal = function(expected, actual) {
ok(expected === actual, 'expected "' + expected + '"; saw "' + actual + '"')
};
+if (typeof createChain === 'undefined') {
+ var createChain = require('./').createChain;
+}
+
// Successful pre + post execution
-var basic = fc.createChain([
+var basic = createChain([
function(data, next, cancel) {
data.hello = 'pre-' + data.hello
next(data, function(data, done) {
@@ -27,7 +35,7 @@ basic({ hello : 'world' }, function(errors, data) {
});
// No provided data
-var noData = fc.createChain([], function(data, fn) {
+var noData = createChain([], function(data, fn) {
fn(null, 'this is some data')
});
@@ -36,7 +44,7 @@ noData(function(errors, data) {
});
// No chain provided
-var noChain = fc.createChain(function(data, fn) {
+var noChain = createChain(function(data, fn) {
fn(null, data + ' world');
});
@@ -45,7 +53,7 @@ noChain('hello', function(errors, data) {
});
// Return from bubbler function
-var bubbleReturn = fc.createChain([
+var bubbleReturn = createChain([
function(data, next, cancel) {
next('hello', function(data, done) {
return data + ' world';
@@ -60,7 +68,7 @@ bubbleReturn('', function(errors, data) {
// Cancel request
-var cancel = fc.createChain([
+var cancel = createChain([
function(data, next, cancel) {
next(data, function(data, done) {
done(null, data += 'outer');
@@ -80,7 +88,7 @@ cancel('', function(errors, data) {
});
// Call the core function after capture and before bubble
-var captureBubble = fc.createChain([
+var captureBubble = createChain([
function (data, next, cancel) {
next('capture-'+ data, function(data, done) {
done(null, data + '-bubble');
@@ -97,7 +105,7 @@ captureBubble('', function(errors, data) {
// Composable chains
-var inner = fc.createChain([
+var inner = createChain([
function (data, next, cancel) {
next(data + ' inner-capture ', function(data, done) {
done(null, data + ' inner-bubble ');
@@ -107,7 +115,7 @@ var inner = fc.createChain([
done(null, data+' inner-core ')
});
-var outer = fc.createChain([
+var outer = createChain([
function (data, next, cancel) {
next(data + ' outer-capture ', function(data, done) {
@@ -126,7 +134,7 @@ outer('', function(errors, data) {
// Manipulate filterchain after creation
-var afterCreation = fc.createChain();
+var afterCreation = createChain();
afterCreation.layers.push(function(data, next, cancel) {
next('made it');
});
@@ -141,7 +149,7 @@ afterCreation('', function(errors, data) {
});
// User error
-var filterNotAFunction = fc.createChain(['abc']);
+var filterNotAFunction = createChain(['abc']);
filterNotAFunction('', function(e, data) {
ok(e, 'should error');
ok(!data, 'data is nothing as it wasnt operated on');
@@ -149,11 +157,11 @@ filterNotAFunction('', function(e, data) {
// optional callback
var testHook = ''
-var optionalCallback = fc.createChain();
+var optionalCallback = createChain();
optionalCallback();
// chain with 2 cores
-var inner = fc.createChain([
+var inner = createChain([
function(data, next) {
next(data + 'icap,', function(outgoingData, done) {
done(null, outgoingData + 'ibub,');
@@ -163,7 +171,7 @@ var inner = fc.createChain([
fn(null, data + 'icore,')
})
-var outer = fc.createChain([
+var outer = createChain([
function(data, next) {
next(data + 'ocap,', function(outgoingData, done) {
done(null, outgoingData + 'obub');
@@ -178,4 +186,3 @@ var outer = fc.createChain([
outer('', function(errors, data) {
equal('ocap,icap,icore,ibub,ocore,obub', data);
});
-
Please sign in to comment.
Something went wrong with that request. Please try again.