├── .gitignore ├── .npmignore ├── History.md ├── Makefile ├── Readme.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.1 / 2015-10-05 3 | ================== 4 | 5 | * pass props as separate argument 6 | 7 | 1.0.0 / 2010-01-03 8 | ================== 9 | 10 | * Initial release 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter spec 6 | 7 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # enroute 3 | 4 | tiny functional router 5 | 6 | ## Installation 7 | 8 | ``` 9 | npm install enroute 10 | ``` 11 | 12 | ## Usage 13 | 14 | 15 | ```js 16 | function edit_user (params, props) { 17 | assert.equal(params.slug, 'matt') 18 | assert.equal(props.additional, 'props') 19 | return component(assign({}, params, props)) 20 | } 21 | 22 | var component = Enroute({ 23 | '/users/new': create_user, 24 | '/users/:slug': find_user, 25 | '/users/:slug/edit': edit_user, 26 | '*': not_found 27 | })('/users/matt/edit', { additional: 'props' }) 28 | 29 | // psuedo create component code 30 | create(component) 31 | ``` 32 | 33 | ## License 34 | 35 | (The MIT License) 36 | 37 | Copyright (c) 2015 Matthew Mueller <matt@lapwinglabs.com> 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining 40 | a copy of this software and associated documentation files (the 41 | 'Software'), to deal in the Software without restriction, including 42 | without limitation the rights to use, copy, modify, merge, publish, 43 | distribute, sublicense, and/or sell copies of the Software, and to 44 | permit persons to whom the Software is furnished to do so, subject to 45 | the following conditions: 46 | 47 | The above copyright notice and this permission notice shall be 48 | included in all copies or substantial portions of the Software. 49 | 50 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 51 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 52 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 53 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 54 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 55 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 56 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 57 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module Dependencies 3 | */ 4 | 5 | var Regexp = require('path-to-regexp') 6 | var assign = require('object-assign') 7 | 8 | /** 9 | * Export `Enroute` 10 | */ 11 | 12 | module.exports = Enroute 13 | 14 | /** 15 | * Create `enroute` 16 | * 17 | * @param {Object} routes 18 | * @return {Function} 19 | */ 20 | 21 | function Enroute (routes) { 22 | return function enroute (location, props) { 23 | if (!location) throw new Error('enroute requires a location') 24 | props = props || {} 25 | var params = {} 26 | 27 | for (var route in routes) { 28 | var m = match(route, params, location) 29 | var fn = routes[route] 30 | 31 | if (m) { 32 | if (typeof fn !== 'function') return fn 33 | else return fn(params, props) 34 | } 35 | } 36 | 37 | return null 38 | } 39 | } 40 | 41 | /** 42 | * Check if this route matches `path`, if so 43 | * populate `params`. 44 | * 45 | * @param {String} path 46 | * @param {Object} params 47 | * @return {Boolean} 48 | * @api private 49 | */ 50 | 51 | function match(path, params, pathname) { 52 | var keys = []; 53 | var regexp = Regexp(path, keys); 54 | var m = regexp.exec(pathname); 55 | 56 | if (!m) return false; 57 | else if (!params) return true; 58 | 59 | for (var i = 1, len = m.length; i < len; ++i) { 60 | var key = keys[i - 1]; 61 | var val = 'string' == typeof m[i] ? decodeURIComponent(m[i]) : m[i]; 62 | if (key) params[key.name] = val; 63 | } 64 | 65 | return true; 66 | } 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "enroute", 3 | "version": "1.0.1", 4 | "description": "tiny functional router", 5 | "keywords": [ 6 | "router", 7 | "functional", 8 | "small" 9 | ], 10 | "author": "Matthew Mueller ", 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/lapwinglabs/enroute.git" 14 | }, 15 | "dependencies": { 16 | "object-assign": "^4.0.1", 17 | "path-to-regexp": "^1.2.1" 18 | }, 19 | "devDependencies": { 20 | "mocha": "*", 21 | "should": "*" 22 | }, 23 | "main": "index" 24 | } --------------------------------------------------------------------------------