universal-router
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -40,3 +40,5 @@ /*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */ | ||
function generateUrls(router, options) { | ||
function generateUrls(router) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!(router instanceof Router)) { | ||
@@ -60,16 +62,43 @@ throw new TypeError('An instance of Router is expected'); | ||
var path = ''; | ||
while (route) { | ||
if (route.path !== '/') { | ||
var toPath = cache.get(route.path); | ||
if (!toPath) { | ||
toPath = Router.pathToRegexp.compile(route.path); | ||
cache.set(route.path, toPath); | ||
var regexp = cache.get(route.fullPath); | ||
if (!regexp) { | ||
var fullPath = ''; | ||
var rt = route; | ||
while (rt) { | ||
if (rt.path !== '/') { | ||
fullPath = rt.path + fullPath; | ||
} | ||
path = toPath(params, options) + path; | ||
rt = rt.parent; | ||
} | ||
route = route.parent; | ||
var tokens = Router.pathToRegexp.parse(fullPath); | ||
var toPath = Router.pathToRegexp.tokensToFunction(tokens); | ||
var keys = Object.create(null); | ||
for (var i = 0; i < tokens.length; i += 1) { | ||
if (typeof tokens[i] !== 'string') { | ||
keys[tokens[i].name] = true; | ||
} | ||
} | ||
regexp = { toPath: toPath, keys: keys }; | ||
cache.set(fullPath, regexp); | ||
route.fullPath = fullPath; | ||
} | ||
return router.baseUrl + path || '/'; | ||
var url = router.baseUrl + regexp.toPath(params, options) || '/'; | ||
if (options.stringifyQueryParams && params) { | ||
var queryParams = Object.create(null); | ||
var _keys = Object.keys(params); | ||
for (var _i = 0; _i < _keys.length; _i += 1) { | ||
var key = _keys[_i]; | ||
if (!regexp.keys[key]) { | ||
queryParams[key] = params[key]; | ||
} | ||
} | ||
var query = options.stringifyQueryParams(queryParams); | ||
if (query) { | ||
url += query.charAt(0) === '?' ? query : '?' + query; | ||
} | ||
} | ||
return url; | ||
}; | ||
@@ -76,0 +105,0 @@ } |
@@ -40,3 +40,5 @@ /*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */ | ||
function generateUrls(router, options) { | ||
function generateUrls(router) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!(router instanceof Router)) { | ||
@@ -60,16 +62,43 @@ throw new TypeError('An instance of Router is expected'); | ||
var path = ''; | ||
while (route) { | ||
if (route.path !== '/') { | ||
var toPath = cache.get(route.path); | ||
if (!toPath) { | ||
toPath = Router.pathToRegexp.compile(route.path); | ||
cache.set(route.path, toPath); | ||
var regexp = cache.get(route.fullPath); | ||
if (!regexp) { | ||
var fullPath = ''; | ||
var rt = route; | ||
while (rt) { | ||
if (rt.path !== '/') { | ||
fullPath = rt.path + fullPath; | ||
} | ||
path = toPath(params, options) + path; | ||
rt = rt.parent; | ||
} | ||
route = route.parent; | ||
var tokens = Router.pathToRegexp.parse(fullPath); | ||
var toPath = Router.pathToRegexp.tokensToFunction(tokens); | ||
var keys = Object.create(null); | ||
for (var i = 0; i < tokens.length; i += 1) { | ||
if (typeof tokens[i] !== 'string') { | ||
keys[tokens[i].name] = true; | ||
} | ||
} | ||
regexp = { toPath: toPath, keys: keys }; | ||
cache.set(fullPath, regexp); | ||
route.fullPath = fullPath; | ||
} | ||
return router.baseUrl + path || '/'; | ||
var url = router.baseUrl + regexp.toPath(params, options) || '/'; | ||
if (options.stringifyQueryParams && params) { | ||
var queryParams = Object.create(null); | ||
var _keys = Object.keys(params); | ||
for (var _i = 0; _i < _keys.length; _i += 1) { | ||
var key = _keys[_i]; | ||
if (!regexp.keys[key]) { | ||
queryParams[key] = params[key]; | ||
} | ||
} | ||
var query = options.stringifyQueryParams(queryParams); | ||
if (query) { | ||
url += query.charAt(0) === '?' ? query : '?' + query; | ||
} | ||
} | ||
return url; | ||
}; | ||
@@ -76,0 +105,0 @@ } |
@@ -40,3 +40,3 @@ /*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */ | ||
function generateUrls(router, options) { | ||
function generateUrls(router, options = {}) { | ||
if (!(router instanceof Router)) { | ||
@@ -60,16 +60,43 @@ throw new TypeError('An instance of Router is expected'); | ||
let path = ''; | ||
while (route) { | ||
if (route.path !== '/') { | ||
let toPath = cache.get(route.path); | ||
if (!toPath) { | ||
toPath = Router.pathToRegexp.compile(route.path); | ||
cache.set(route.path, toPath); | ||
let regexp = cache.get(route.fullPath); | ||
if (!regexp) { | ||
let fullPath = ''; | ||
let rt = route; | ||
while (rt) { | ||
if (rt.path !== '/') { | ||
fullPath = rt.path + fullPath; | ||
} | ||
path = toPath(params, options) + path; | ||
rt = rt.parent; | ||
} | ||
route = route.parent; | ||
const tokens = Router.pathToRegexp.parse(fullPath); | ||
const toPath = Router.pathToRegexp.tokensToFunction(tokens); | ||
const keys = Object.create(null); | ||
for (let i = 0; i < tokens.length; i += 1) { | ||
if (typeof tokens[i] !== 'string') { | ||
keys[tokens[i].name] = true; | ||
} | ||
} | ||
regexp = { toPath, keys }; | ||
cache.set(fullPath, regexp); | ||
route.fullPath = fullPath; | ||
} | ||
return router.baseUrl + path || '/'; | ||
let url = router.baseUrl + regexp.toPath(params, options) || '/'; | ||
if (options.stringifyQueryParams && params) { | ||
const queryParams = Object.create(null); | ||
const keys = Object.keys(params); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
if (!regexp.keys[key]) { | ||
queryParams[key] = params[key]; | ||
} | ||
} | ||
const query = options.stringifyQueryParams(queryParams); | ||
if (query) { | ||
url += query.charAt(0) === '?' ? query : `?${query}`; | ||
} | ||
} | ||
return url; | ||
}; | ||
@@ -76,0 +103,0 @@ } |
{ | ||
"name": "generateUrls", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Universal Router Generate URLs Add-on", | ||
@@ -5,0 +5,0 @@ "homepage": "https://www.kriasoft.com/universal-router/", |
{ | ||
"name": "universal-router", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Isomorphic router for JavaScript web applications", | ||
@@ -5,0 +5,0 @@ "homepage": "https://www.kriasoft.com/universal-router/", |
@@ -0,1 +1,6 @@ | ||
<a href="https://www.kriasoft.com/universal-router/" target="_blank"> | ||
<img width="64" height="64" align="right" alt="Visit Universal Router Website" | ||
src="https://raw.githubusercontent.com/kriasoft/universal-router/master/docs/assets/img/tour.png" /> | ||
</a> | ||
# Universal Router | ||
@@ -10,4 +15,4 @@ | ||
> A simple middleware-style router for | ||
[isomorphic](http://nerds.airbnb.com/isomorphic-javascript-future-web-apps/) JavaScript web apps | ||
A simple middleware-style router that can be used in both client-side (e.g. React, Vue.js) | ||
and server-side applications (e.g. Node.js/Express, Koa). | ||
@@ -18,2 +23,68 @@ Visit **[Quickstart Guide](http://slides.com/koistya/universal-router)** (slides) | | ||
## Features | ||
✓ It has [simple code](https://github.com/kriasoft/universal-router/blob/master/src/Router.js) | ||
with only single [path-to-regexp](https://github.com/pillarjs/path-to-regexp) dependency<br> | ||
✓ It can be used with any JavaScript framework such as React, Vue.js etc<br> | ||
✓ It uses the same middleware approach used in Express and Koa, making it easy to learn<br> | ||
✓ It supports both [imperative](https://en.wikipedia.org/wiki/Imperative_programming) and | ||
[declarative](https://en.wikipedia.org/wiki/Declarative_programming) routing style<br> | ||
✓ Routes are plain JavaScript objects with which you can interact as you like<br> | ||
## Installation | ||
Using [npm](https://www.npmjs.com/package/universal-router): | ||
```bash | ||
$ npm install universal-router --save | ||
``` | ||
Or using a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network) like [unpkg.com](https://unpkg.com) | ||
with the following script tag: | ||
```html | ||
<script src="https://unpkg.com/universal-router@3.2.0/universal-router.min.js"></script> | ||
``` | ||
## How does it look like? | ||
```js | ||
import UniversalRouter from 'universal-router'; | ||
const routes = [ | ||
{ | ||
path: '/', | ||
action: () => `<h1>Home</h1>` | ||
}, | ||
{ | ||
path: '/posts', | ||
action: () => console.log('checking child routes for /posts'), | ||
children: [ | ||
{ | ||
path: '/', | ||
action: () => `<h1>Posts</h1>` | ||
}, | ||
{ | ||
path: '/:id', | ||
action: (context) => `<h1>Post #${context.params.id}</h1>` | ||
} | ||
] | ||
}, | ||
]; | ||
const router = new UniversalRouter(routes); | ||
router.resolve('/posts').then(html => { | ||
document.body.innerHTML = html; // renders: <h1>Posts</h1> | ||
}); | ||
``` | ||
Play with an example on [JSFiddle](https://jsfiddle.net/frenzzy/b0w9mjck/), | ||
[CodePen](https://codepen.io/frenzzy/pen/aWLKpb/), | ||
[JS Bin](https://jsbin.com/degedol/edit?js,output) in your browser or try | ||
[RunKit](https://runkit.com/frenzzy/universal-router-demo) node.js playground. | ||
## Documentation | ||
@@ -26,4 +97,18 @@ | ||
## Learn ES6 | ||
## Browser Support | ||
Universal Router supports all popular browsers, including Internet Explorer 9 and above | ||
with polyfills such as [es6-shim](https://github.com/es-shims/es6-shim) for | ||
[`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), | ||
[`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and | ||
[`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) | ||
that must be included before any other code. | ||
For compatibility with older browsers you may also need to include polyfills for | ||
[`Array.isArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) | ||
and [`Object.create`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create). | ||
## Books and Tutorials | ||
:mortar_board: **[ES6 Training Course](https://es6.io/friend/konstantin)** by Wes Bos<br> | ||
@@ -30,0 +115,0 @@ :green_book: **[You Don't Know JS: ES6 & Beyond](http://amzn.to/2bFss85)** by Kyle Simpson (Dec, 2015)<br> |
@@ -42,3 +42,5 @@ /*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */ | ||
function generateUrls(router, options) { | ||
function generateUrls(router) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!(router instanceof Router)) { | ||
@@ -62,16 +64,43 @@ throw new TypeError('An instance of Router is expected'); | ||
var path = ''; | ||
while (route) { | ||
if (route.path !== '/') { | ||
var toPath = cache.get(route.path); | ||
if (!toPath) { | ||
toPath = Router.pathToRegexp.compile(route.path); | ||
cache.set(route.path, toPath); | ||
var regexp = cache.get(route.fullPath); | ||
if (!regexp) { | ||
var fullPath = ''; | ||
var rt = route; | ||
while (rt) { | ||
if (rt.path !== '/') { | ||
fullPath = rt.path + fullPath; | ||
} | ||
path = toPath(params, options) + path; | ||
rt = rt.parent; | ||
} | ||
route = route.parent; | ||
var tokens = Router.pathToRegexp.parse(fullPath); | ||
var toPath = Router.pathToRegexp.tokensToFunction(tokens); | ||
var keys = Object.create(null); | ||
for (var i = 0; i < tokens.length; i += 1) { | ||
if (typeof tokens[i] !== 'string') { | ||
keys[tokens[i].name] = true; | ||
} | ||
} | ||
regexp = { toPath: toPath, keys: keys }; | ||
cache.set(fullPath, regexp); | ||
route.fullPath = fullPath; | ||
} | ||
return router.baseUrl + path || '/'; | ||
var url = router.baseUrl + regexp.toPath(params, options) || '/'; | ||
if (options.stringifyQueryParams && params) { | ||
var queryParams = Object.create(null); | ||
var _keys = Object.keys(params); | ||
for (var _i = 0; _i < _keys.length; _i += 1) { | ||
var key = _keys[_i]; | ||
if (!regexp.keys[key]) { | ||
queryParams[key] = params[key]; | ||
} | ||
} | ||
var query = options.stringifyQueryParams(queryParams); | ||
if (query) { | ||
url += query.charAt(0) === '?' ? query : '?' + query; | ||
} | ||
} | ||
return url; | ||
}; | ||
@@ -78,0 +107,0 @@ } |
/*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("./universal-router.min.js")):"function"==typeof define&&define.amd?define(["./universal-router.min.js"],r):e.generateUrls=r(e.UniversalRouter)}(this,function(e){"use strict";function r(e,t,n){if(e[t.name])throw new Error('Route "'+t.name+'" already exists');if(t.name&&(e[t.name]=t),n)for(var o=0;o<n.length;o+=1){var a=n[o];a.parent=t,r(e,a,a.children)}}function t(t,o){if(!(t instanceof e))throw new TypeError("An instance of Router is expected");return t.routesByName=t.routesByName||{},function(a,i){var u=t.routesByName[a];if(!(u||(t.routesByName={},r(t.routesByName,t.root,t.root.children),u=t.routesByName[a])))throw new Error('Route "'+a+'" not found');for(var f="";u;){if("/"!==u.path){var s=n.get(u.path);s||(s=e.pathToRegexp.compile(u.path),n.set(u.path,s)),f=s(i,o)+f}u=u.parent}return t.baseUrl+f||"/"}}e="default"in e?e.default:e;var n=new Map;return e.generateUrls=t,t}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("./universal-router.min.js")):"function"==typeof define&&define.amd?define(["./universal-router.min.js"],r):e.generateUrls=r(e.UniversalRouter)}(this,function(e){"use strict";function r(e,t,n){if(e[t.name])throw new Error('Route "'+t.name+'" already exists');if(t.name&&(e[t.name]=t),n)for(var a=0;a<n.length;a+=1){var o=n[a];o.parent=t,r(e,o,o.children)}}function t(t){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!(t instanceof e))throw new TypeError("An instance of Router is expected");return t.routesByName=t.routesByName||{},function(o,i){var u=t.routesByName[o];if(!(u||(t.routesByName={},r(t.routesByName,t.root,t.root.children),u=t.routesByName[o])))throw new Error('Route "'+o+'" not found');var s=n.get(u.fullPath);if(!s){for(var f="",l=u;l;)"/"!==l.path&&(f=l.path+f),l=l.parent;for(var c=e.pathToRegexp.parse(f),h=e.pathToRegexp.tokensToFunction(c),y=Object.create(null),m=0;m<c.length;m+=1)"string"!=typeof c[m]&&(y[c[m].name]=!0);s={toPath:h,keys:y},n.set(f,s),u.fullPath=f}var p=t.baseUrl+s.toPath(i,a)||"/";if(a.stringifyQueryParams&&i){for(var d=Object.create(null),v=Object.keys(i),g=0;g<v.length;g+=1){var w=v[g];s.keys[w]||(d[w]=i[w])}var j=a.stringifyQueryParams(d);j&&(p+="?"===j.charAt(0)?j:"?"+j)}return p}}e="default"in e?e.default:e;var n=new Map;return e.generateUrls=t,t}); | ||
//# sourceMappingURL=universal-router-generate-urls.min.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
238019
2004
245