Created
November 26, 2015 11:53
-
-
Save christian-fei/db8955a0aee72e16eb4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function route(name, config={}) { | |
let {url} = config | |
url = url || `/${name}` | |
url = normalizeRouteUrl(url) | |
const normalizedRouteName = normalizeRouteName(name) | |
const nameCap = normalizedRouteName[0].toUpperCase() + normalizedRouteName.slice(1) | |
const controller = `${nameCap}Ctrl as vm` | |
const templateUrl = `controllers/${nameCap}Ctrl.html` | |
return { | |
name, | |
url, | |
controller, | |
templateUrl, | |
...config, | |
} | |
} | |
function normalizeRouteUrl(routeUrl) { | |
if( /\.index$/g.test(routeUrl) ){ | |
return '/' | |
} | |
const match = routeUrl.match(/[^\/\.]*$/) | |
if( match ) { | |
return '/'+match[0] | |
} | |
return routeUrl | |
} | |
function normalizeRouteName(routeName) { | |
return routeName.replace(/\.(.)/g, function(_, group) { | |
return group.toUpperCase() | |
}) | |
} | |
function isConfig(object) { | |
return Object.prototype.toString.call(object) === '[object Object]' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment