diff --git a/Cakefile b/Cakefile index 117ec4d..f7291af 100644 --- a/Cakefile +++ b/Cakefile @@ -1,9 +1,6 @@ fs = require('fs') -sys = require('sys') {spawn, exec} = require('child_process') -package = JSON.parse(fs.readFileSync('package.json', 'utf8')) - execCmds = (cmds) -> exec cmds.join(' && '), (err, stdout, stderr) -> output = (stdout + stderr).trim() diff --git a/lib/ender.js b/lib/ender.js index e6d7a12..d933f2b 100644 --- a/lib/ender.js +++ b/lib/ender.js @@ -1,5 +1,3 @@ -(function($) { - return $.ender({ - route: require('route') - }); -})(ender); \ No newline at end of file +// Generated by CoffeeScript 1.3.1 + + diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..e45ed6b --- /dev/null +++ b/lib/index.js @@ -0,0 +1,52 @@ +// Generated by CoffeeScript 1.3.1 +var route; + +route = typeof exports !== "undefined" && exports !== null ? exports : (this['route'] = {}); + +route.Router = (function() { + + Router.name = 'Router'; + + Router.prototype._transformations = [[/([?=,\/])/g, '\\$1'], [/:([\w\d]+)/g, '([^/]*)'], [/\*([\w\d]+)/g, '(.*?)']]; + + function Router() { + this.routes = []; + } + + Router.prototype.add = function(routes) { + var expr, fn, pattern, replacement, transformer, _i, _len, _ref, _ref1; + for (expr in routes) { + fn = routes[expr]; + pattern = expr; + _ref = this._transformations; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + _ref1 = _ref[_i], transformer = _ref1[0], replacement = _ref1[1]; + pattern = pattern.replace(transformer, replacement); + } + this.routes.push({ + expr: expr, + pattern: new RegExp(pattern), + fn: fn + }); + } + }; + + Router.prototype.run = function(path, context) { + var m, result, results, route, _i, _len, _ref; + results = []; + _ref = this.routes; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + route = _ref[_i]; + if ((m = route.pattern.exec(path))) { + result = route.fn.apply(context, m.slice(1)); + if (result) { + results.push(result); + } + } + } + return results; + }; + + return Router; + +})(); diff --git a/lib/route.js b/lib/route.js deleted file mode 100644 index be4ae8e..0000000 --- a/lib/route.js +++ /dev/null @@ -1,61 +0,0 @@ -(function(route) { - var Route, _hash, _routes; - _routes = []; - _hash = null; - route.init = function(run) { - var onchange; - onchange = function() { - var hash; - hash = $.hash(); - if (hash !== _hash) { - _hash = hash; - route.run(hash); - } - }; - $(window).bind('hashchange', onchange); - if (run) { - onchange(); - } - }; - route.navigate = function(hash, run) { - if (!run) { - _hash = hash; - } - $.hash(hash); - }; - route.run = function(hash) { - var m, route, _i, _len; - for (_i = 0, _len = _routes.length; _i < _len; _i++) { - route = _routes[_i]; - if ((m = route.pattern.exec(hash))) { - route.fn.apply(route, m.slice(1)); - } - } - }; - route.add = function(routes, fn) { - var path; - if (fn) { - routes = {}; - routes[routes] = fn; - } - for (path in routes) { - fn = routes[path]; - _routes.push(new Route(path, fn)); - } - }; - return Route = (function() { - Route.prototype._transformations = [[/:([\w\d]+)/g, '([^/]*)'], [/\*([\w\d]+)/g, '(.*?)']]; - function Route(path, fn) { - var pattern, replacement, _i, _len, _ref, _ref2; - this.path = path; - this.fn = fn; - _ref = this._transformations; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - _ref2 = _ref[_i], pattern = _ref2[0], replacement = _ref2[1]; - path = path.replace(pattern, replacement); - } - this.pattern = new RegExp("^" + path + "$"); - } - return Route; - })(); -})(typeof exports !== "undefined" && exports !== null ? exports : (this['route'] = {})); \ No newline at end of file diff --git a/package.json b/package.json index a1dfe8f..a044714 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "repositories": [{ "type": "git", "url": "https://github.com/amccollum/route.git" }], "licenses": [{ "type": "MIT", "url": "http://opensource.org/licenses/mit-license.php" }], - "main": "lib/route.js", + "main": "lib/index.js", "ender": "lib/ender.js", "directories": { "lib": "lib" }, diff --git a/src/ender.coffee b/src/ender.coffee index b6c407c..984f1f2 100644 --- a/src/ender.coffee +++ b/src/ender.coffee @@ -1,3 +1,4 @@ -(($) -> - $.ender({ route: require('route') }) -)(ender) \ No newline at end of file +# (($) -> +# route = require('route') +# +# )(ender) \ No newline at end of file diff --git a/src/index.coffee b/src/index.coffee new file mode 100644 index 0000000..2d34877 --- /dev/null +++ b/src/index.coffee @@ -0,0 +1,42 @@ +route = exports ? (@['route'] = {}) + +class route.Router + _transformations: [ + [ # Escape URL Special Characters + /([?=,\/])/g + '\\$1' + ], + + [ # Named Parameters + /:([\w\d]+)/g + '([^/]*)' + ], + + [ # Splat Parameters + /\*([\w\d]+)/g + '(.*?)' + ], + ] + + constructor: -> + @routes = [] + + add: (routes) -> + for expr, fn of routes + pattern = expr + for [transformer, replacement] in @_transformations + pattern = pattern.replace(transformer, replacement) + + @routes.push({ expr: expr, pattern: new RegExp(pattern), fn: fn }) + + return + + run: (path, context) -> + results = [] + + for route in @routes + if (m = route.pattern.exec(path)) + result = route.fn.apply(context, m.slice(1)) + results.push(result) if result + + return results diff --git a/src/route.coffee b/src/route.coffee deleted file mode 100644 index 6195c97..0000000 --- a/src/route.coffee +++ /dev/null @@ -1,66 +0,0 @@ -((route) -> - _routes = [] - _hash = null - - route.init = (run) -> - onchange = () -> - hash = $.hash() - if hash != _hash - _hash = hash - route.run(hash) - - return - - $(window).bind 'hashchange', onchange - onchange() if run - - return - - route.navigate = (hash, run) -> - if not run - _hash = hash - - $.hash(hash) - return - - route.run = (hash) -> - for route in _routes - if (m = route.pattern.exec(hash)) - route.fn.apply(route, m.slice(1)) - - return - - route.add = (routes, fn) -> - if fn - routes = {} - routes[routes] = fn - - for path, fn of routes - _routes.push(new Route(path, fn)) - - return - - class Route - _transformations: [ - [ # Named Parameters - /:([\w\d]+)/g - '([^/]*)' - ], - - [ # Splat Parameters - /\*([\w\d]+)/g - '(.*?)' - ], - ] - - constructor: (path, fn) -> - @path = path - @fn = fn - - for [pattern, replacement] in @_transformations - path = path.replace(pattern, replacement) - - @pattern = new RegExp("^#{path}$") - - -)(exports ? (@['route'] = {})) \ No newline at end of file