fix optional route with index and order

This commit is contained in:
Alexandre Chopin 2017-02-10 21:24:17 +01:00
parent 77f9e90c82
commit c4dfa34212

View File

@ -261,42 +261,49 @@ function createRoutes (files, srcDir) {
var res = 0 var res = 0
var _a = a.path.split('/') var _a = a.path.split('/')
var _b = b.path.split('/') var _b = b.path.split('/')
var _max = _b.length - 1 for (var i = 0; i < _a.length; i++) {
_a.forEach((s, i) => { if (res !== 0) { break }
if (i <= _max) { var y = (_a[i].indexOf('*') > -1) ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
if (res === 0) { var z = (_b[i].indexOf('*') > -1) ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
var y = (s.indexOf('*') > -1) ? 2 : (s.indexOf(':') > -1 ? 1 : 0) res = y - z
var z = (_b[i].indexOf('*') > -1) ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0) if (i === _b.length - 1 && res === 0) {
res = y - z res = 1
}
} else {
res = (res === 0) ? -1 : res
} }
}) }
return res === 0 ? ((_a.length === _b.length) ? _a[_max].length - _b[_max].length : _b.length - _a.length) : res return res === 0 ? -1 : res
}) })
}) })
return cleanChildrenRoutes(routes) return cleanChildrenRoutes(routes)
} }
function cleanChildrenRoutes (routes, isChild = false) { function cleanChildrenRoutes (routes, isChild = false) {
let hasIndex = false let start = -1
let parents = [] let routesIndex = []
routes.forEach((route) => {
if (/-index$/.test(route.name) || route.name === 'index') {
// Save indexOf 'index' key in name
let res = route.name.split('-')
let s = res.indexOf('index')
start = (start === -1 || s < start) ? s : start
routesIndex.push(res)
}
})
routes.forEach((route) => { routes.forEach((route) => {
route.path = (isChild) ? route.path.replace('/', '') : route.path route.path = (isChild) ? route.path.replace('/', '') : route.path
if ((isChild && /-index$/.test(route.name)) || (!isChild && route.name === 'index')) { if (route.path.indexOf('?') > -1) {
hasIndex = true let names = route.name.split('-')
} let paths = route.path.split('/')
let names = route.path.split('/') if (!isChild) { paths.shift() } // clean first / for parents
if (hasIndex && (isChild || (!isChild && names[1] && names[1].indexOf('?') > -1))) { routesIndex.forEach((r) => {
route.path = route.path.replace('?', '') let i = r.indexOf('index') - start // children names
} if (i < paths.length) {
if (/-index$/.test(route.name)) { for (var a = 0; a <= i; a++) {
parents.push(route.name) if (a === i) { paths[a] = paths[a].replace('?', '') }
} else { if (a < i && names[a] !== r[a]) { break }
if (hasIndex && parents.indexOf(route.name.split('-').slice(0, -1).join('-') + '-index') > -1) { }
route.path = route.path.replace('?', '') }
} })
route.path = (isChild ? '' : '/') + paths.join('/')
} }
route.name = route.name.replace(/-index$/, '') route.name = route.name.replace(/-index$/, '')
if (route.children) { if (route.children) {