Skip to content

Commit

Permalink
getRouteName now takes state object
Browse files Browse the repository at this point in the history
  • Loading branch information
sometimeskind committed Jun 16, 2018
1 parent 1aa6c74 commit 2aa21ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
20 changes: 13 additions & 7 deletions lib/am-path-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import pathToRegexp from 'path-to-regexp';
import qs from 'qs';

import type { RouteNameType } from '../index';

export type ParamsType = { [paramName: string]: string };

export default function getPathHelpers(routesMap: { [routeName: RouteNameType]: { path?: string, paths?: Array<string> } }) {
export default function getPathHelpers(routesMap: { [routeName: string]: { path?: string, paths?: Array<string> } }) {
if (!routesMap) {
throw new Error('No routes provided.');
}
Expand Down Expand Up @@ -63,13 +61,21 @@ export default function getPathHelpers(routesMap: { [routeName: RouteNameType]:
}

return {
getRouteNameFromResource(resource: string): ?RouteNameType {
const [path] = resource.split('?');
getRouteName(parentState: { resource: string }): ?string {
if (typeof parentState.resource !== 'string') {
throw new Error('Resource not provided.');
}

const [path] = parentState.resource.split('?');
return Object.keys(pathCheckers).find((routeName) => pathCheckers[routeName].find((checker) => checker.exec(path)));
},

getAdditionalState(routeName: RouteNameType, resource: string): { params: ParamsType, query: ParamsType } {
const [path, search] = resource.split('?');
getAdditionalState(routeName: string, parentState: { resource: string }): { params: ParamsType, query: ParamsType } {
if (typeof parentState.resource !== 'string') {
throw new Error('Resource not provided.');
}

const [path, search] = parentState.resource.split('?');
return {
params: (routeName && evalPathParams(routeName, path)) || {},
query: qs.parse(search),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "am-path-helpers",
"version": "0.0.2",
"version": "0.1.0",
"description": "Helpers for App Manager that provide enhanced tools for path matching",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 9 additions & 9 deletions test/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ describe('Path Helpers', () => {
});
});

describe('getRouteNameFromResource', () => {
describe('getRouteName', () => {
const routes = {
APP: { paths: ['/path/:id/:otherParam(valid|options)/:optional?', '/path/:otherParam(valid|options)/:optional?'] },
OTHER: { path: '/not-path/:id/:optional?' },
};

const { getRouteNameFromResource } = getPathHelpers(routes);
const { getRouteName } = getPathHelpers(routes);

it('Finds a route from its path', () => {
const routeName = getRouteNameFromResource('/path/1058690/valid/optional');
const routeName = getRouteName({ resource: '/path/1058690/valid/optional' });
expect(routeName).to.equals('APP');
});

it('Returns undefined if no urls match', () => {
const routeName = getRouteNameFromResource('/path/1058690/invalid/optional');
const routeName = getRouteName({ resource: '/path/1058690/invalid/optional' });
expect(routeName).to.be.undefined;
});
});
Expand All @@ -51,7 +51,7 @@ describe('Path Helpers', () => {
const { getAdditionalState } = getPathHelpers(routes);

it('finds params from route', () => {
const { params } = getAdditionalState('APP', '/path/1058690/valid/optional');
const { params } = getAdditionalState('APP', { resource: '/path/1058690/valid/optional' });
expect(params).to.deep.equals({
id: '1058690',
otherParam: 'valid',
Expand All @@ -60,7 +60,7 @@ describe('Path Helpers', () => {
});

it('handles optional params from route', () => {
const { params, query } = getAdditionalState('APP', '/path/1058690/valid');
const { params, query } = getAdditionalState('APP', { resource: '/path/1058690/valid' });
expect(params).to.deep.equals({
id: '1058690',
otherParam: 'valid',
Expand All @@ -70,17 +70,17 @@ describe('Path Helpers', () => {
});

it('handles invalid routes', () => {
const { params } = getAdditionalState('APP', '/invalid/1058690/valid');
const { params } = getAdditionalState('APP', { resource: '/invalid/1058690/valid' });
expect(params).to.be.an('object').that.is.empty;
});

it('handles invalid params', () => {
const { params } = getAdditionalState('APP', '/path/1058690/invalid/abcde');
const { params } = getAdditionalState('APP', { resource: '/path/1058690/invalid/abcde' });
expect(params).to.be.an('object').that.is.empty;
});

it('parses queries', () => {
const { params, query } = getAdditionalState('APP', '/path/1058690/valid?test=true');
const { params, query } = getAdditionalState('APP', { resource: '/path/1058690/valid?test=true' });
expect(params).to.deep.equals({
id: '1058690',
otherParam: 'valid',
Expand Down

0 comments on commit 2aa21ec

Please sign in to comment.