Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate paths by name #74

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/href.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Universal Router (https://www.kriasoft.com/universal-router/)
*
* Copyright © 2015-2016 Konstantin Tarkus, Kriasoft LLC. All rights reserved.
*
* This source code is licensed under the Apache 2.0 license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import toRegExp from 'path-to-regexp';

function resolvePath(route, routeName) {
const routePath = (route.path === '/') ? '' : route.path;

if (route.name && route.name === routeName) {
return routePath;
}

if (route.children) {
for (let i = 0; i < route.children.length; i += 1) {
let resultPath = resolvePath(route.children[i], routeName);

if (resultPath !== null) {
resultPath = routePath + resultPath;
return resultPath.startsWith('/') ? resultPath : `/${resultPath}`;
}
}
}

return null;
}

function href(routes, routeName, routeParams = {}, options = { pretty: false }) {
const root = Array.isArray(routes) ? { path: '/', children: routes } : routes;
const path = resolvePath(root, routeName);

if (path === null) {
return null;
}

return toRegExp.compile(path)(routeParams, options);
}

export default href;
5 changes: 3 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import resolve from './resolve';
import href from './href';

export { resolve };
export default { resolve };
export { resolve, href };
export default { resolve, href };
90 changes: 90 additions & 0 deletions test/href.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Universal Router (https://www.kriasoft.com/universal-router/)
*
* Copyright © 2015-2016 Konstantin Tarkus, Kriasoft LLC. All rights reserved.
*
* This source code is licensed under the Apache 2.0 license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { href } from '../src/main';

const emptyRoutes = [];
const simpleRoutes = [{ name: 'root', path: '/', children: [{ name: 'a', path: '/a' }] }];
const nestRoutes = [{
name: 'root',
path: '/',
children: [
{ name: 'a', path: '/a/:a' },
{ name: 'b', path: '/b/:b?' },
{
name: 'c',
path: '/c/:c(\\d+)?',
children: [
{
name: 'd',
path: '/d',
children: [
{ name: 'f', path: '/f/:f' },
],
},
],
},
{ name: 'a', path: '/never-mounted-by-name' },
],
}];

describe('href(routes, routeName, routeParams, [options = { pretty: false }])', () => {

it('should return false if no route found', async () => {
let path;

path = href(emptyRoutes, 'z');
expect(path).to.be.null;

path = href(simpleRoutes, 'z');
expect(path).to.be.null;

path = href(nestRoutes, 'z');
expect(path).to.be.null;
});

it('should find and mount the first route by name', () => {
let path;

path = href(simpleRoutes, 'root');
expect(path).to.be.equal('/');

path = href(nestRoutes, 'root');
expect(path).to.be.equal('/');

path = href(nestRoutes, 'a', { a: 'a' });
expect(path).to.be.equal('/a/a');

path = href(nestRoutes, 'b');
expect(path).to.be.equal('/b');

path = href(nestRoutes, 'b', { b: 'b' });
expect(path).to.be.equal('/b/b');

path = href(nestRoutes, 'c');
expect(path).to.be.equal('/c');

path = href(nestRoutes, 'c', { c: '1' });
expect(path).to.be.equal('/c/1');

path = href(nestRoutes, 'd');
expect(path).to.be.equal('/c/d');

path = href(nestRoutes, 'd', { c: '1' });
expect(path).to.be.equal('/c/1/d');

path = href(nestRoutes, 'f', { f: 'f' });
expect(path).to.be.equal('/c/d/f/f');

path = href(nestRoutes, 'f', { c: '1', f: 'f' });
expect(path).to.be.equal('/c/1/d/f/f');
});

});