expect(tokenizePath('/')).toEqual([[]])
})
+ it('escapes :', () => {
+ expect(tokenizePath('/\\:')).toEqual([
+ [{ type: TokenType.Static, value: ':' }],
+ ])
+ })
+
+ it('escapes ( inside custom re', () => {
+ expect(tokenizePath('/:a(\\))')).toEqual([
+ [
+ {
+ type: TokenType.Param,
+ value: 'a',
+ regexp: ')',
+ optional: false,
+ repeatable: false,
+ },
+ ],
+ ])
+ })
+
it('static single', () => {
expect(tokenizePath('/home')).toEqual([
[{ type: TokenType.Static, value: 'home' }],
matchStringify('/:a+', { a: ['one', 'two'] }, '/one/two')
})
+ it('repeatable params+ with extra segment', () => {
+ matchStringify('/:a+/other', { a: ['one', 'two'] }, '/one/two/other')
+ })
+
it('repeatable params*', () => {
matchStringify('/:a*', { a: ['one', 'two'] }, '/one/two')
})
case TokenizerState.ParamRegExp:
if (char === ')') {
- state = TokenizerState.ParamRegExpEnd
+ // handle the escaped )
+ if (customRe[customRe.length - 1] == '\\')
+ customRe = customRe.slice(0, -1) + char
+ else state = TokenizerState.ParamRegExpEnd
} else {
customRe += char
}
const BASE_PARAM_PATTERN = '[^/]+?'
+/**
+ * TODO: add options strict, sensitive, encode, decode
+ */
+
export function tokensToParser(segments: Array<Token[]>): PathParser {
let score = 0
let pattern = '^'