Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion scully/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import {HandledRoute} from './routerPlugins/addOptionalRoutesPlugin';
import {updateScullyConfig} from './utils/config';
import {RouteTypes, ScullyConfig} from './utils/interfacesandenums';
import {routeSplit} from './utils/routeSplit';
import {replaceFirstRouteParamWithVal} from './utils/replaceFirstRouteParamWithVal';
import {startScully} from './utils/startup';

export {startScully, updateScullyConfig, ScullyConfig, registerPlugin, HandledRoute, RouteTypes, routeSplit, configValidator};
export {
startScully,
updateScullyConfig,
ScullyConfig,
registerPlugin,
HandledRoute,
RouteTypes,
routeSplit,
replaceFirstRouteParamWithVal,
configValidator,
};
20 changes: 20 additions & 0 deletions scully/utils/replaceFirstRouteParamWithVal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* This function replaces the first instance of a route param with the passed value
* @param route - the route from scullyconfig.routes section
* @param val - a string that you will be adding into the parameterized route
*
* replaceFirstRouteParamWithVal('/foo/:bar', '123') // '/foo/123'
* replaceFirstRouteParamWithVal('/foo/:bar/:baz', '123') // '/foo/123/:baz'
*/
export function replaceFirstRouteParamWithVal(route: string, val: string): string {
const pieces: string[] = route.split('/');
for (let i = 0; i < pieces.length; i++) {
if (pieces[i].startsWith(':')) {
pieces[i] = val;
// Once you replace a param, break the loop.
// This allows you to use this on routes with multiple params.
break;
}
}
return pieces.join('/');
}