NPM package that can be used to match and generate url paths agains predefined patterns. Can be used when applying routing in javascript client-side and backend applications. Uses ECMA6 javascript standard.
npm install --save route-match
When using a client that does not support ECMA6, please use a javascript compiler like Babel to compile the code to a readable format.
import { Route, RouteCollection, PathGenerator } from 'route-match';
var myRouteCollection = new RouteCollection([
new Route('user_detail', '/user/:id(\\d+)')
]);
var myPathGenerator = new PathGenerator(myRouteCollection);
console.log(myPathGenerator.generate('user_detail', { id: 10 })); // result: /user/10
Remarks:
- when you supply more parameters then the route pattern requires, the extra parameters are added to the generated url as query parameters
- when you supply parameters that do not match the requirements an error will be thrown
- Feel free to use non-ECMA6 syntax
import { Route, RouteCollection, PathMatcher } from 'route-match';
var myRouteCollection = new RouteCollection([
new Route('user_detail', '/user/:id(\\d+)')
]);
var myPathMatcher = new PathMatcher(myRouteCollection);
console.log(myPathGenerator.match('/user/10'); // result: instance of RouteMatch containing route name, payload and parameters
Remarks:
- When the supplied url contains query parameters, they will also be available in the eventual RouteMatch object
- When no route is matched,
null
is returned - Feel free to use non-ECMA6 syntax
npm test
npm run compile