Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

routr

Package Overview
Dependencies
Maintainers
5
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

routr - npm Package Compare versions

Comparing version 2.3.0 to 3.0.0

.eslintrc.js

77

CHANGELOG.md
# Change Log
## 3.0.0
### Breaking Changes
- `routr` uses native `URLSearchParams` instead of `query-string`
library internally. If you need to support old browsers, you can
either add a `URLSearchParams` polyfill or inject `query-string`
when instantiating `routr`:
```js
router = new Routr(routes, {
queryLib: require('query-string'),
});
```
## 2.1.0
* [#37] Enhance makePath for routes with path array
- [#37] Enhance makePath for routes with path array
## 2.0.2
* [#36] Bug fix: Add support for question marks in hash fragments
- [#36] Bug fix: Add support for question marks in hash fragments
## 2.0.1
* [#35] Fix decodeURIComponent of undefined bug
- [#35] Fix decodeURIComponent of undefined bug

@@ -19,3 +34,3 @@ ## 2.0.0

* [#33] `getRoute` will now `decodeURIComponent` route values, you might need to remove `decodeURIComponent` from your route actions if you were supporting extended characters manually in your routes.
- [#33] `getRoute` will now `decodeURIComponent` route values, you might need to remove `decodeURIComponent` from your route actions if you were supporting extended characters manually in your routes.

@@ -26,23 +41,23 @@ ## 1.0.0

* [#29] `navigate` is no longer used as part of `router.getRoute` options
* [#29] `route.navigate` has been removed from the matched route object
- [#29] `navigate` is no longer used as part of `router.getRoute` options
- [#29] `route.navigate` has been removed from the matched route object
### Features
* [#30] Route definitions should now be defined as an array of route objects
rather than a map of routes. The old method of defining routes with a map
is still supported, but ordering can not be guaranteed (as per the JavaScript
engine's implementation).
* [#31] Added support for parsing and constructing urls with query strings.
Matched route objects now contain a `query` property containing the map of
query parameters. `router.makePath` now accepts a third `query` parameter
which is a map of query parameters to add to the resulting URL string. e.g.
`router.makePath('home', {}, { foo: 'bar' });` will result in `/?foo=bar`.
Query strings are generated using the `query-string` npm module, but can
be customized by adding the `options.queryLib` to the `Router` constructor.
The replacement should have a `parse` and `stringify` method similar to
`query-string`. An example replacement would be `qs`.
* [#32] Allow routes to match multiple HTTP methods by using an array
for the `route.method` attribute. By default, routes with an undefined
`method` will match ANY method.
- [#30] Route definitions should now be defined as an array of route objects
rather than a map of routes. The old method of defining routes with a map
is still supported, but ordering can not be guaranteed (as per the JavaScript
engine's implementation).
- [#31] Added support for parsing and constructing urls with query strings.
Matched route objects now contain a `query` property containing the map of
query parameters. `router.makePath` now accepts a third `query` parameter
which is a map of query parameters to add to the resulting URL string. e.g.
`router.makePath('home', {}, { foo: 'bar' });` will result in `/?foo=bar`.
Query strings are generated using the `query-string` npm module, but can
be customized by adding the `options.queryLib` to the `Router` constructor.
The replacement should have a `parse` and `stringify` method similar to
`query-string`. An example replacement would be `qs`.
- [#32] Allow routes to match multiple HTTP methods by using an array
for the `route.method` attribute. By default, routes with an undefined
`method` will match ANY method.

@@ -53,3 +68,3 @@ ## 0.1.3

* [#27] Support for array paths with parameter name collision
- [#27] Support for array paths with parameter name collision

@@ -60,3 +75,3 @@ ## 0.1.2

* Replace `reverand` with `path-to-regexp` for creation of paths
- Replace `reverand` with `path-to-regexp` for creation of paths

@@ -67,3 +82,3 @@ ## 0.1.1

* [#22] Make route methods case-insensitive
- [#22] Make route methods case-insensitive

@@ -74,3 +89,3 @@ ## 0.1.0

* Renamed "path" field to "url" in the return object of getRoute()
- Renamed "path" field to "url" in the return object of getRoute()

@@ -81,3 +96,3 @@ ## 0.0.6

* Update devDependencies and Readme
- Update devDependencies and Readme

@@ -88,3 +103,3 @@ ## 0.0.5

* Freeze route objects in non-production environments
- Freeze route objects in non-production environments

@@ -95,3 +110,3 @@ ## 0.0.4

* Allow matching paths containing query strings
- Allow matching paths containing query strings

@@ -102,3 +117,3 @@ ## 0.0.3

* Updated dependencies
- Updated dependencies

@@ -109,3 +124,3 @@ ## 0.0.2

* [#2] Introduction of `navigate` property under route
- [#2] Introduction of `navigate` property under route

@@ -112,0 +127,0 @@ ## 0.0.1

@@ -1,7 +0,6 @@

Contributing Code to `routr`
-------------------------------
## Contributing Code to `routr`
Please be sure to sign our [CLA][] before you submit pull requests or otherwise contribute to `routr`. This protects developers, who rely on [BSD license][].
[BSD license]: https://github.com/yahoo/routr/blob/master/LICENSE.md
[CLA]: https://yahoocla.herokuapp.com/
[bsd license]: https://github.com/yahoo/routr/blob/master/LICENSE.md
[cla]: https://yahoocla.herokuapp.com/

@@ -7,11 +7,11 @@ # Routr API

* `routes` (optional): Ordered list of routes used for matching.
** `route.name`: Name of the route (used for path making)
** `route.path`: The matching pattern of the route. Follows rules of [path-to-regexp](https://github
.com/pillarjs/path-to-regexp)
** `route.method=undefined`: The method that the path should match to. Will match all methods if `undefined` and no
methods
if `null`.
* `options` (optional): Options for parsing and generating the urls
** `options.queryLib=require('query-string')`: Library to use to `parse` and `stringify` query strings
- `routes` (optional): Ordered list of routes used for matching.
** `route.name`: Name of the route (used for path making)
** `route.path`: The matching pattern of the route. Follows rules of [path-to-regexp](https://github
.com/pillarjs/path-to-regexp)
\*\* `route.method=undefined`: The method that the path should match to. Will match all methods if `undefined` and no
methods
if `null`.
- `options` (optional): Options for parsing and generating the urls
\*\* `options.queryLib=require('query-string')`: Library to use to `parse` and `stringify` query strings

@@ -24,5 +24,5 @@ ## Instance Methods

* `url` (required) The url to be used for route matching. Query strings are **not** considered when performing the match.
* `options` options object
* `options.method` (optional) The case-insensitive HTTP method string. DEFAULT: 'get'
- `url` (required) The url to be used for route matching. Query strings are **not** considered when performing the match.
- `options` options object
- `options.method` (optional) The case-insensitive HTTP method string. DEFAULT: 'get'

@@ -33,4 +33,4 @@ ### makePath(name, params, query)

* `name` (required) The route name
* `params` (required) The route parameters to be used to create the path string
* `query` (optional) The query parameters to be used to create the path string
- `name` (required) The route name
- `params` (required) The route parameters to be used to create the path string
- `query` (optional) The query parameters to be used to create the path string

@@ -6,9 +6,30 @@ /**

'use strict';
/*global process:true */
var pathToRegexp = require('path-to-regexp');
var queryString = require('query-string');
var DEFAULT_METHOD = 'GET';
var cachedCompilers = {};
var queryString = {
parse: function (string) {
var obj = {};
var params = new URLSearchParams(string);
params.forEach(function (value, key) {
obj[key] = value;
});
return obj;
},
stringify: function (obj) {
var params = new URLSearchParams();
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
params.append(key, obj[key]);
}
}
params.sort();
return params.toString();
},
};
/**

@@ -33,3 +54,3 @@ * @class Route

this.methods = {};
for (var i=0; i<method.length; ++i) {
for (var i = 0; i < method.length; ++i) {
this.methods[method[i].toUpperCase()] = true;

@@ -39,3 +60,3 @@ }

this.keys = [];
this.regexp = pathToRegexp(this.config.path, this.keys);
this.regexp = pathToRegexp.pathToRegexp(this.config.path, this.keys);
this._queryLib = options.queryLib || queryString;

@@ -103,3 +124,3 @@ }

// Leave `pos` at the beginning of the query-string, if any.
['#', '?'].forEach(function(delimiter){
['#', '?'].forEach(function (delimiter) {
pos = path.indexOf(delimiter);

@@ -122,6 +143,11 @@ if (pos >= 0) {

// name if the path was an array.
if (pathMatches[i+1] !== undefined && routeParams[self.keys[i].name] === undefined){
if (
pathMatches[i + 1] !== undefined &&
routeParams[self.keys[i].name] === undefined
) {
// Because pathToRegexp encodeURIComponent params values, it is necessary
// to decode when reading from URL
routeParams[self.keys[i].name] = decodeURIComponent(pathMatches[i+1]);
routeParams[self.keys[i].name] = decodeURIComponent(
pathMatches[i + 1]
);
}

@@ -133,3 +159,3 @@ }

if (-1 !== pos) {
queryParams = self._queryLib.parse(url.substring(pos+1));
queryParams = self._queryLib.parse(url.substring(pos + 1));
}

@@ -140,3 +166,3 @@

route: routeParams,
query: queryParams
query: queryParams,
};

@@ -169,3 +195,3 @@ };

try {
return this._makePath(routePath, params, query)
return this._makePath(routePath, params, query);
} catch (pathErr) {

@@ -179,3 +205,3 @@ err = pathErr;

Route.prototype._makePath = function(routePath, params, query) {
Route.prototype._makePath = function (routePath, params, query) {
var compiler;

@@ -185,3 +211,5 @@ var url;

if (typeof routePath === 'string') {
compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath);
compiler =
cachedCompilers[routePath] ||
pathToRegexp.compile(routePath, { encode: encodeURIComponent });
cachedCompilers[routePath] = compiler;

@@ -199,3 +227,3 @@ url = compiler(params);

}
}
};

@@ -256,3 +284,7 @@ /**

self._routeOrder.push(route.name);
self._routes[route.name] = new Route(route.name, route, self._options);
self._routes[route.name] = new Route(
route.name,
route,
self._options
);
});

@@ -298,3 +330,3 @@ }

config: route.config,
query: match.query
query: match.query,
};

@@ -315,5 +347,10 @@ }

Router.prototype.makePath = function (name, params, query) {
return (name && this._routes[name] && this._routes[name].makePath(params, query)) || null;
return (
(name &&
this._routes[name] &&
this._routes[name].makePath(params, query)) ||
null
);
};
module.exports = Router;

@@ -1,6 +0,4 @@

Software License Agreement (BSD License)
========================================
# Software License Agreement (BSD License)
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
----------------------------------------------------
## Copyright (c) 2014, Yahoo! Inc. All rights reserved.

@@ -11,8 +9,8 @@ Redistribution and use of this software in source and binary forms, with or

* Redistributions of source code must retain the above copyright notice, this
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Yahoo! Inc. nor the names of YUI's contributors may be
- Neither the name of Yahoo! Inc. nor the names of YUI's contributors may be
used to endorse or promote products derived from this software without

@@ -19,0 +17,0 @@ specific prior written permission of Yahoo! Inc.

{
"name": "routr",
"version": "2.3.0",
"version": "3.0.0",
"description": "A router for both server and client",

@@ -12,3 +12,5 @@ "main": "index.js",

"cover": "node node_modules/istanbul/lib/cli.js cover --dir artifacts -- ./node_modules/mocha/bin/_mocha tests/unit/ --recursive --reporter spec",
"lint": "eslint .",
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "eslint . && npm run format:check",
"test": "mocha tests/unit/ --recursive --require babel-register --require babel-polyfill --reporter spec"

@@ -24,4 +26,3 @@ },

"dependencies": {
"path-to-regexp": "^1.1.1",
"query-string": "^5.0.0"
"path-to-regexp": "^6.2.0"
},

@@ -40,3 +41,4 @@ "devDependencies": {

"pre-commit": "^1.0.7",
"sinon": "^8.0.0"
"prettier": "^2.3.2",
"sinon": "^11.1.1"
},

@@ -50,3 +52,7 @@ "pre-commit": [

"router"
]
],
"prettier": {
"singleQuote": true,
"tabWidth": 4
}
}

@@ -1,10 +0,7 @@

# Routr
# Routr
[![npm version](https://badge.fury.io/js/routr.svg)](http://badge.fury.io/js/routr)
[![Build Status](https://travis-ci.org/yahoo/routr.svg?branch=master)](https://travis-ci.org/yahoo/routr)
[![Dependency Status](https://david-dm.org/yahoo/routr.svg)](https://david-dm.org/yahoo/routr)
[![devDependency Status](https://david-dm.org/yahoo/routr/dev-status.svg)](https://david-dm.org/yahoo/routr#info=devDependencies)
![github actions](https://github.com/yahoo/routr/actions/workflows/node.js.yml/badge.svg)
[![Coverage Status](https://img.shields.io/coveralls/yahoo/routr.svg)](https://coveralls.io/r/yahoo/routr?branch=master)
Routr library is an implementation of router-related functionalities that can be used for both server and client. It follows the same routing rules as [Express](http://expressjs.com/) by using the same library. This library does not use callbacks for routes, instead just mapping them to string names that can be used as application state and used within your application later. For instance in Flux, the current route would be held as state in a store.

@@ -17,5 +14,5 @@

```javascript
var Router = require('routr');
import Router from 'routr';
var router = new Router([
const router = new Router([
{

@@ -26,4 +23,4 @@ name: 'view_user',

foo: {
bar: 'baz'
}
bar: 'baz',
},
},

@@ -33,8 +30,8 @@ {

path: '/user/:id/post/:post',
method: 'get'
}
method: 'get',
},
]);
// match route
var route = router.getRoute('/user/garfield?foo=bar');
const route = router.getRoute('/user/garfield?foo=bar');
if (route) {

@@ -52,7 +49,11 @@ // this will output:

// "path" will be "/user/garfield/post/favoriteFood?meal=breakfast"
var path = router.makePath('view_user_post', {id: 'garfield', post: 'favoriteFood'}, { meal: 'breakfast' });
const path = router.makePath(
'view_user_post',
{ id: 'garfield', post: 'favoriteFood' },
{ meal: 'breakfast' }
);
```
## Object.freeze
We use `Object.freeze` to freeze the router and route objects for non-production environments to ensure the immutability of these objects.

@@ -65,7 +66,10 @@

### Build with Webpack
Two main utility plugins:
* use [DefinePlugin](http://webpack.github.io/docs/list-of-plugins.html#defineplugin) to define the value for `process.env`
* use [UglifyJsPlugin](http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) to remove dead code.
- use [DefinePlugin](http://webpack.github.io/docs/list-of-plugins.html#defineplugin) to define the value for `process.env`
- use [UglifyJsPlugin](http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) to remove dead code.
Example of the webpack configuration:
```js

@@ -84,7 +88,10 @@ plugins: [

### Build with Browserify
Similar to webpack, you can also use the following two utils with your favorite build system:
* use [envify](https://github.com/hughsk/envify) to set `process.env.NODE_ENV` to the desired environment
* use [uglifyjs](https://github.com/mishoo/UglifyJS2) to remove dead code.
- use [envify](https://github.com/hughsk/envify) to set `process.env.NODE_ENV` to the desired environment
- use [uglifyjs](https://github.com/mishoo/UglifyJS2) to remove dead code.
Command-line example:
```bash

@@ -94,6 +101,5 @@ $ browserify index.js -t [ envify --NODE_ENV production ] | uglifyjs -c > bundle.js

## API
- [Routr](https://github.com/yahoo/routr/blob/master/docs/routr.md)
- [Routr](https://github.com/yahoo/routr/blob/master/docs/routr.md)

@@ -105,5 +111,4 @@ ## License

[LICENSE file]: https://github.com/yahoo/routr/blob/master/LICENSE.md
[license file]: https://github.com/yahoo/routr/blob/master/LICENSE.md
Third-pary open source code used are listed in our [package.json file]( https://github.com/yahoo/routr/blob/master/package.json).
Third-pary open source code used are listed in our [package.json file](https://github.com/yahoo/routr/blob/master/package.json).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc