matchParams('/home', '/home/other', {}, { end: false })
})
+ it('should not match optional params + static without leading slash', () => {
+ matchParams('/a/:p?-b', '/a-b', null)
+ matchParams('/a/:p?-b', '/a/-b', { p: '' })
+ matchParams('/a/:p?-b', '/a/e-b', { p: 'e' })
+ })
+
it('returns an empty object with no keys', () => {
matchParams('/home', '/home', {})
})
matchStringify('/b-:a?/other', {}, '/b-/other')
})
+ it('starting optional param? with static segment should not drop the initial /', () => {
+ matchStringify('/a/:a?-other/other', { a: '' }, '/a/-other/other')
+ matchStringify('/a/:a?-other/other', {}, '/a/-other/other')
+ matchStringify('/a/:a?-other/other', { a: 'p' }, '/a/p-other/other')
+ })
+
it('optional param*', () => {
matchStringify('/:a*/other', { a: '' }, '/other')
matchStringify('/:a*/other', { a: [] }, '/other')
// prepend the slash if we are starting a new segment
if (!tokenIndex)
- subPattern = optional ? `(?:/${subPattern})` : '/' + subPattern
+ subPattern =
+ // avoid an optional / if there are more segments e.g. /:p?-static
+ // or /:p?-:p2
+ optional && segment.length < 2
+ ? `(?:/${subPattern})`
+ : '/' + subPattern
if (optional) subPattern += '?'
pattern += subPattern
const text: string = Array.isArray(param) ? param.join('/') : param
if (!text) {
if (optional) {
- // remove the last slash as we could be at the end
- if (path.endsWith('/')) path = path.slice(0, -1)
- // do not append a slash on the next iteration
- else avoidDuplicatedSlash = true
+ // if we have more than one optional param like /:a?-static we
+ // don't need to care about the optional param
+ if (segment.length < 2) {
+ // remove the last slash as we could be at the end
+ if (path.endsWith('/')) path = path.slice(0, -1)
+ // do not append a slash on the next iteration
+ else avoidDuplicatedSlash = true
+ }
} else throw new Error(`Missing required param "${value}"`)
}
path += text