Permalink
Browse files

Added browser support

  • Loading branch information...
1 parent 9fbece8 commit 05b5b77cccdf2d880ccbd9951df6fcf879c50b7f @scttnlsn committed Aug 7, 2012
Showing with 120 additions and 59 deletions.
  1. +8 −2 README.md
  2. +0 −6 lib/index.js
  3. +0 −47 lib/stack.js
  4. +3 −2 package.json
  5. +75 −0 pagoda.js
  6. +32 −0 test/index.html
  7. +2 −2 test/{test_stack.js → test_pagoda.js}
View
@@ -1,7 +1,7 @@
pagoda
===
-Reusable function stacks.
+Reusable function stacks for Node.js and the browser.
Example
---
@@ -70,4 +70,10 @@ Install
Tests
---
- make test
+To test with Node.js, run:
+
+ make test
+
+Test in the browser, open:
+
+ test/index.html
View
@@ -1,6 +0,0 @@
-var Stack = require('./stack');
-
-exports.stack = function() {
- var layers = [].slice.call(arguments);
- return new Stack(layers);
-};
View
@@ -1,47 +0,0 @@
-module.exports = Stack;
-
-function Stack(layers) {
- this.layers = layers || [];
-}
-
-Stack.prototype.use = function(layer) {
- this.layers.push(layer);
- return this;
-};
-
-Stack.prototype.handle = function(context) {
- var self = this;
- var index = 0;
-
- function next(err) {
- var layer = self.layers[index++];
-
- if (!layer) {
- // Reached bottom of stack
- if (err) throw err;
- return;
- }
-
- var exec = function(args) {
- try {
- layer.apply(context || {}, args);
- } catch (err) {
- next(err);
- }
- };
-
- if (err) {
- if (layer.length === 2) {
- // Layer accepts the error
- exec([err, next]);
- } else {
- // Pass error down the stack
- next(err);
- }
- } else {
- exec([next]);
- }
- };
-
- next();
-};
View
@@ -1,17 +1,18 @@
{
"name": "pagoda",
"version": "0.1.0",
- "description": "Reusable function stacks",
+ "description": "Reusable function stacks for Node.js and the browser",
"homepage": "http://github.com/scttnlsn/pagoda",
"author": "Scott Nelson <scottbnel@gmail.com>",
- "main": "./lib/index",
+ "main": "./pagoda",
"repository": {
"type": "git",
"url": "git://github.com/scttnlsn/pagoda.git"
},
"devDependencies": {
+ "chai": ">= 1.2.0",
"mocha": ">= 1.3.2"
}
}
View
@@ -0,0 +1,75 @@
+(function() {
+
+ var pagoda = {};
+
+ var root = this;
+ var orig = root.pagoda;
+
+ if (typeof module !== 'undefined' && module.exports) {
+ module.exports = pagoda;
+ } else {
+ root.pagoda = pagoda;
+ }
+
+ pagoda.noConflict = function () {
+ root.pagoda = orig;
+ return pagoda;
+ };
+
+ pagoda.stack = function() {
+ var layers = [].slice.call(arguments);
+ return new Stack(layers);
+ };
+
+ pagoda.Stack = Stack;
+
+ // Stack
+ // ---------------
+
+ function Stack(layers) {
+ this.layers = layers || [];
+ }
+
+ Stack.prototype.use = function(layer) {
+ this.layers.push(layer);
+ return this;
+ };
+
+ Stack.prototype.handle = function(context) {
+ var self = this;
+ var index = 0;
+
+ function next(err) {
+ var layer = self.layers[index++];
+
+ if (!layer) {
+ // Reached bottom of stack
+ if (err) throw err;
+ return;
+ }
+
+ var exec = function(args) {
+ try {
+ layer.apply(context || {}, args);
+ } catch (err) {
+ next(err);
+ }
+ };
+
+ if (err) {
+ if (layer.length === 2) {
+ // Layer accepts the error
+ exec([err, next]);
+ } else {
+ // Pass error down the stack
+ next(err);
+ }
+ } else {
+ exec([next]);
+ }
+ };
+
+ next();
+ };
+
+})();
View
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <link rel="stylesheet" href="../node_modules/mocha/mocha.css">
+ </head>
+ <body>
+ <div id="mocha"></div>
+
+ <script src="../pagoda.js"></script>
+ <script src="../node_modules/mocha/mocha.js"></script>
+ <script src="../node_modules/chai/chai.js"></script>
+
+ <script>
+ mocha.setup('bdd');
+
+ window.require = function(module) {
+ var modules = {
+ 'assert': chai.assert,
+ '../pagoda': pagoda
+ };
+
+ return modules[module];
+ };
+ </script>
+
+ <script src="test_pagoda.js"></script>
+
+ <script>
+ mocha.run();
+ </script>
+ </body>
+</html>
@@ -1,11 +1,11 @@
var assert = require('assert');
-var Stack = require('../lib/stack');
+var pagoda = require('../pagoda');
describe('Stack', function() {
var stack;
beforeEach(function() {
- stack = new Stack();
+ stack = pagoda.stack();
});
describe('when adding function', function() {

0 comments on commit 05b5b77

Please sign in to comment.