expect(matcher.getRecordMatcher('child')).toBe(undefined)
})
+ it('removes existing record when adding with the same name', () => {
+ const matcher = createRouterMatcher([], {})
+ matcher.addRoute({ path: '/', component, name: 'home' })
+ matcher.addRoute({ path: '/home', component, name: 'home' })
+ expect(matcher.getRoutes()).toHaveLength(1)
+ expect(matcher.resolve({ path: '/home' }, currentLocation)).toMatchObject({
+ name: 'home',
+ })
+ })
+
describe('warnings', () => {
mockWarn()
parent?: RouteRecordMatcher,
originalRecord?: RouteRecordMatcher
) {
+ // used later on to remove by name
+ let isRootAdd = !originalRecord
let mainNormalizedRecord = normalizeRouteRecord(record)
// we might be the child of an alias
mainNormalizedRecord.aliasOf = originalRecord && originalRecord.record
// other alias (if any) need to reference this record when adding children
originalRecord = originalRecord || matcher
+ // remove the route if named and only for the top record (avoid in nested calls)
+ if (isRootAdd && record.name) removeRoute(record.name)
+
insertMatcher(matcher)
}
}
const VALID_PARAM_RE = /[a-zA-Z0-9_]/
+// After some profiling, the cache seems to be unnecessary because tokenizePath
+// (the slowest part of adding a route) is very fast
+
+// const tokenCache = new Map<string, Token[][]>()
export function tokenizePath(path: string): Array<Token[]> {
if (!path) return [[]]
// remove the leading slash
if (path[0] !== '/') throw new Error('A non-empty path must start with "/"')
+ // if (tokenCache.has(path)) return tokenCache.get(path)!
+
function crash(message: string) {
throw new Error(`ERR (${state})/"${buffer}": ${message}`)
}
consumeBuffer()
finalizeSegment()
+ // tokenCache.set(path, tokens)
+
return tokens
}