allow dynamic parameters inside route parts - fixes #139

This commit is contained in:
Rich Harris
2018-02-28 09:39:21 -05:00
parent 631afbbfe4
commit 58c1eb9fa8
4 changed files with 90 additions and 25 deletions

View File

@@ -4,7 +4,7 @@ const { create_routes } = require('../../core.js');
describe('create_routes', () => {
it('sorts routes correctly', () => {
const routes = create_routes({
files: ['index.html', 'about.html', '[wildcard].html', 'post/foo.html', 'post/[id].html', 'post/bar.html']
files: ['index.html', 'about.html', 'post/f[xx].html', '[wildcard].html', 'post/foo.html', 'post/[id].html', 'post/bar.html', 'post/[id].json.js']
});
assert.deepEqual(
@@ -14,6 +14,8 @@ describe('create_routes', () => {
'about.html',
'post/foo.html',
'post/bar.html',
'post/f[xx].html',
'post/[id].json.js',
'post/[id].html',
'[wildcard].html'
]
@@ -133,4 +135,33 @@ describe('create_routes', () => {
b: null
});
});
it('matches a dynamic part within a part', () => {
const route = create_routes({
files: ['things/[slug].json.js']
})[0];
assert.deepEqual(route.exec('/things/foo.json'), {
slug: 'foo'
});
});
it('matches multiple dynamic parts within a part', () => {
const route = create_routes({
files: ['things/[id]_[slug].json.js']
})[0];
assert.deepEqual(route.exec('/things/someid_someslug.json'), {
id: 'someid',
slug: 'someslug'
});
});
it('fails if dynamic params are not separated', () => {
assert.throws(() => {
create_routes({
files: ['[foo][bar].js']
});
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
});
});