],
])
})
+ // end of describe token
})
describe('tokensToParser', () => {
})
it('param*', () => {
- matchRegExp('^/((?:\\d+)(?:/(?:\\d+))*)?$', [
+ matchRegExp('^(?:/((?:\\d+)(?:/(?:\\d+))*))?$', [
[
{
type: TokenType.Param,
})
it('param?', () => {
- matchRegExp('^/(\\d+)?$', [
+ matchRegExp('^(?:/(\\d+))?$', [
+ [
+ {
+ type: TokenType.Param,
+ value: 'id',
+ regexp: '\\d+',
+ repeatable: false,
+ optional: true,
+ },
+ ],
+ ])
+ })
+
+ it('static and param?', () => {
+ matchRegExp('^/ab(?:/(\\d+))?$', [
+ [
+ {
+ type: TokenType.Static,
+ value: 'ab',
+ },
+ ],
[
{
type: TokenType.Param,
],
])
})
+
+ it('static and param+', () => {
+ matchRegExp('^/ab/((?:\\d+)(?:/(?:\\d+))*)$', [
+ [
+ {
+ type: TokenType.Static,
+ value: 'ab',
+ },
+ ],
+ [
+ {
+ type: TokenType.Param,
+ value: 'id',
+ regexp: '\\d+',
+ repeatable: true,
+ optional: false,
+ },
+ ],
+ ])
+ })
// end of describe
})
it('param optional followed by static', () => {
matchParams('/:a?/one', '/two/one', { a: 'two' })
- // the second one is never matched
- matchParams('/:a?/one', '/one', null)
+ // since the first one is optional
+ matchParams('/:a?/one', '/one', { a: '' })
+ matchParams('/:a?/one', '/two', null)
// can only match one time
matchParams('/:a?/one', '/two/three/one', null)
matchParams('/:a*/one', '/two/one', { a: ['two'] })
ReturnType<ReturnType<typeof tokensToParser>['parse']>,
null
>,
- expectedUrl: string
+ expectedUrl: string,
+ options?: Parameters<typeof tokensToParser>[1]
) {
- const pathParser = tokensToParser(tokenizePath(path))
+ const pathParser = tokensToParser(tokenizePath(path), options)
expect(pathParser.stringify(params)).toEqual(expectedUrl)
}
matchStringify('/home', {}, '/home')
})
+ it('works with trailing slash', () => {
+ matchStringify('/home/', {}, '/home/')
+ matchStringify('/home/', {}, '/home/', { strict: true })
+ })
+
it('single param one segment', () => {
matchStringify('/:id', { id: 'one' }, '/one')
})