route-match
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.
Installation
npm install --save route-match
Usage
When using a client that does not support ECMA6, please use a javascript compiler like Babel to compile the code to a readable format.
Generating URLs
; var myRouteCollection = 'user_detail' '/user/:id(\\d+)'; var myPathGenerator = myRouteCollection; console; // 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
Matching urls against patterns
; var myRouteCollection = 'user_detail' '/user/:id(\\d+)'; var myPathMatcher = myRouteCollection; console
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
Running unit tests
npm test
Compiling ECMA6 code to 'regular' javascript
npm run compile