From 871e93dd1fba03acde67a78d75a1a5c91ef1d34f Mon Sep 17 00:00:00 2001 From: Vladimir Kutepov Date: Tue, 17 Sep 2019 01:05:13 +0300 Subject: [PATCH] 8.3.0 --- CHANGELOG.md | 11 +++++++++-- dist/universal-router-generate-urls.js | 4 ++-- dist/universal-router-generate-urls.js.map | 2 +- dist/universal-router-generate-urls.min.js | 2 +- dist/universal-router-generate-urls.min.js.map | 2 +- package.json | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6163703..01b6fd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [8.3.0] - 2019-09-17 + +- Make `generateUrls` compatible with `UniversalRouterSync` + ([#172](https://github.com/kriasoft/universal-router/pull/172)) + ## [8.2.1] - 2019-07-20 -- Fix `context.next()` to throw `Route not found` instead of `TypeError` ([#169](https://github.com/kriasoft/universal-router/pull/169)) +- Fix `context.next()` to throw `Route not found` instead of `TypeError` + ([#169](https://github.com/kriasoft/universal-router/pull/169)) ## [8.2.0] - 2019-05-10 @@ -263,7 +269,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Small bug fixes and improvements -[Unreleased]: https://github.com/kriasoft/universal-router/compare/v8.2.1...HEAD +[Unreleased]: https://github.com/kriasoft/universal-router/compare/v8.3.0...HEAD +[8.3.0]: https://github.com/kriasoft/universal-router/compare/v8.2.1...v8.3.0 [8.2.1]: https://github.com/kriasoft/universal-router/compare/v8.2.0...v8.2.1 [8.2.0]: https://github.com/kriasoft/universal-router/compare/v8.1.0...v8.2.0 [8.1.0]: https://github.com/kriasoft/universal-router/compare/v8.0.0...v8.1.0 diff --git a/dist/universal-router-generate-urls.js b/dist/universal-router-generate-urls.js index 7bc42ae..5587e3c 100644 --- a/dist/universal-router-generate-urls.js +++ b/dist/universal-router-generate-urls.js @@ -65,8 +65,8 @@ rt = rt.parent; } - var tokens = pathToRegexp.parse(fullPath); - var toPath = pathToRegexp.tokensToFunction(tokens); + var tokens = pathToRegexp.parse(fullPath, options); + var toPath = pathToRegexp.tokensToFunction(tokens, options); var keys = Object.create(null); for (var i = 0; i < tokens.length; i++) { diff --git a/dist/universal-router-generate-urls.js.map b/dist/universal-router-generate-urls.js.map index e7a08c0..16a5a8f 100644 --- a/dist/universal-router-generate-urls.js.map +++ b/dist/universal-router-generate-urls.js.map @@ -1 +1 @@ -{"version":3,"file":"universal-router-generate-urls.js","sources":["src/generateUrls.js"],"sourcesContent":["/**\n * Universal Router (https://www.kriasoft.com/universal-router/)\n *\n * Copyright (c) 2015-present Kriasoft.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE.txt file in the root directory of this source tree.\n */\n\nimport pathToRegexp from 'path-to-regexp'\n\nconst cache = new Map()\n\nfunction cacheRoutes(routesByName, route, routes) {\n if (routesByName.has(route.name)) {\n throw new Error(`Route \"${route.name}\" already exists`)\n }\n\n if (route.name) {\n routesByName.set(route.name, route)\n }\n\n if (routes) {\n for (let i = 0; i < routes.length; i++) {\n const childRoute = routes[i]\n childRoute.parent = route\n cacheRoutes(routesByName, childRoute, childRoute.children)\n }\n }\n}\n\nfunction generateUrls(router, options = {}) {\n if (!router) {\n throw new ReferenceError('Router is not defined')\n }\n\n router.routesByName = router.routesByName || new Map()\n\n return (routeName, params) => {\n let route = router.routesByName.get(routeName)\n if (!route) {\n router.routesByName.clear() // clear cache\n cacheRoutes(router.routesByName, router.root, router.root.children)\n\n route = router.routesByName.get(routeName)\n if (!route) {\n throw new Error(`Route \"${routeName}\" not found`)\n }\n }\n\n let regexp = cache.get(route.fullPath)\n if (!regexp) {\n let fullPath = ''\n let rt = route\n while (rt) {\n const path = Array.isArray(rt.path) ? rt.path[0] : rt.path\n if (path) {\n fullPath = path + fullPath\n }\n rt = rt.parent\n }\n const tokens = pathToRegexp.parse(fullPath)\n const toPath = pathToRegexp.tokensToFunction(tokens)\n const keys = Object.create(null)\n for (let i = 0; i < tokens.length; i++) {\n if (typeof tokens[i] !== 'string') {\n keys[tokens[i].name] = true\n }\n }\n regexp = { toPath, keys }\n cache.set(fullPath, regexp)\n route.fullPath = fullPath\n }\n\n let url = router.baseUrl + regexp.toPath(params, options) || '/'\n\n if (options.stringifyQueryParams && params) {\n const queryParams = {}\n const keys = Object.keys(params)\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i]\n if (!regexp.keys[key]) {\n queryParams[key] = params[key]\n }\n }\n const query = options.stringifyQueryParams(queryParams)\n if (query) {\n url += query.charAt(0) === '?' ? query : `?${query}`\n }\n }\n\n return url\n }\n}\n\nexport default generateUrls\n"],"names":["cache","Map","cacheRoutes","routesByName","route","routes","has","name","Error","set","i","length","childRoute","parent","children","generateUrls","router","options","ReferenceError","routeName","params","get","clear","root","regexp","fullPath","rt","path","Array","isArray","tokens","pathToRegexp","parse","toPath","tokensToFunction","keys","Object","create","url","baseUrl","stringifyQueryParams","queryParams","key","query","charAt"],"mappings":";;;;;;;;EAWA,IAAMA,KAAK,GAAG,IAAIC,GAAJ,EAAd;;EAEA,SAASC,WAAT,CAAqBC,YAArB,EAAmCC,KAAnC,EAA0CC,MAA1C,EAAkD;EAChD,MAAIF,YAAY,CAACG,GAAb,CAAiBF,KAAK,CAACG,IAAvB,CAAJ,EAAkC;EAChC,UAAM,IAAIC,KAAJ,cAAoBJ,KAAK,CAACG,IAA1B,uBAAN;EACD;;EAED,MAAIH,KAAK,CAACG,IAAV,EAAgB;EACdJ,IAAAA,YAAY,CAACM,GAAb,CAAiBL,KAAK,CAACG,IAAvB,EAA6BH,KAA7B;EACD;;EAED,MAAIC,MAAJ,EAAY;EACV,SAAK,IAAIK,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGL,MAAM,CAACM,MA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V,EAAC,GAAG,CAAb,EAAgBA,EAAC,GAAGyB,KAAI,CAACxB,MAAzB,EAAiCD,EAAC,EAAlC,EAAsC;EACpC,YAAMgC,GAAG,GAAGP,KAAI,CAACzB,EAAD,CAAhB;;EACA,YAAI,CAACc,MAAM,CAACW,IAAP,CAAYO,GAAZ,CAAL,EAAuB;EACrBD,UAAAA,WAAW,CAACC,GAAD,CAAX,GAAmBtB,MAAM,CAACsB,GAAD,CAAzB;EACD;EACF;;EACD,UAAMC,KAAK,GAAG1B,OAAO,CAACuB,oBAAR,CAA6BC,WAA7B,CAAd;;EACA,UAAIE,KAAJ,EAAW;EACTL,QAAAA,GAAG,IAAIK,KAAK,CAACC,MAAN,CAAa,CAAb,MAAoB,GAApB,GAA0BD,KAA1B,SAAsCA,KAA7C;EACD;EACF;;EAED,WAAOL,GAAP;EACD,GAtDD;EAuDD;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"universal-router-generate-urls.js","sources":["src/generateUrls.js"],"sourcesContent":["/**\n * Universal Router (https://www.kriasoft.com/universal-router/)\n *\n * Copyright (c) 2015-present Kriasoft.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE.txt file in the root directory of this source tree.\n */\n\nimport pathToRegexp from 'path-to-regexp'\n\nconst cache = new Map()\n\nfunction cacheRoutes(routesByName, route, routes) {\n if (routesByName.has(route.name)) {\n throw new Error(`Route \"${route.name}\" already exists`)\n }\n\n if (route.name) {\n routesByName.set(route.name, route)\n }\n\n if (routes) {\n for (let i = 0; i < routes.length; i++) {\n const childRoute = routes[i]\n childRoute.parent = route\n cacheRoutes(routesByName, childRoute, childRoute.children)\n }\n }\n}\n\nfunction generateUrls(router, options = {}) {\n if (!router) {\n throw new ReferenceError('Router is not defined')\n }\n\n router.routesByName = router.routesByName || new Map()\n\n return (routeName, params) => {\n let route = router.routesByName.get(routeName)\n if (!route) {\n router.routesByName.clear() // clear cache\n cacheRoutes(router.routesByName, router.root, router.root.children)\n\n route = router.routesByName.get(routeName)\n if (!route) {\n throw new Error(`Route \"${routeName}\" not found`)\n }\n }\n\n let regexp = cache.get(route.fullPath)\n if (!regexp) {\n let fullPath = ''\n let rt = route\n while (rt) {\n const path = Array.isArray(rt.path) ? rt.path[0] : rt.path\n if (path) {\n fullPath = path + fullPath\n }\n rt = rt.parent\n }\n const tokens = pathToRegexp.parse(fullPath, options)\n const toPath = pathToRegexp.tokensToFunction(tokens, options)\n const keys = Object.create(null)\n for (let i = 0; i < tokens.length; i++) {\n if (typeof tokens[i] !== 'string') {\n keys[tokens[i].name] = true\n }\n }\n regexp = { toPath, keys }\n cache.set(fullPath, regexp)\n route.fullPath = fullPath\n }\n\n let url = router.baseUrl + regexp.toPath(params, options) || '/'\n\n if (options.stringifyQueryParams && params) {\n const queryParams = {}\n const keys = Object.keys(params)\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i]\n if (!regexp.keys[key]) {\n queryParams[key] = params[key]\n }\n }\n const query = options.stringifyQueryParams(queryParams)\n if (query) {\n url += query.charAt(0) === '?' ? query : `?${query}`\n }\n }\n\n return url\n }\n}\n\nexport default generateUrls\n"],"names":["cache","Map","cacheRoutes","routesByName","route","routes","has","name","Error","set","i","length","childRoute","parent","children","generateUrls","router","options","ReferenceError","routeName","params","get","clear","root","regexp","fullPath","rt","path","Array","isArray","tokens","pathToRegexp","parse","toPath","tokensToFunction","keys","Object","create","url","baseUrl","stringifyQueryParams","queryParams","key","query","charAt"],"mappings":";;;;;;;;EAWA,IAAMA,KAAK,GAAG,IAAIC,GAAJ,EAAd;;EAEA,SAASC,WAAT,CAAqBC,YAArB,EAAmCC,KAAnC,EAA0CC,MAA1C,EAAkD;EAChD,MAAIF,YAAY,CAACG,GAAb,CAAiBF,KAAK,CAACG,IAAvB,CAAJ,EAAkC;EAChC,UAAM,IAAIC,KAAJ,cAAoBJ,KAAK,CAACG,IAA1B,uBAAN;EACD;;EAED,MAAIH,KAAK,CAACG,IAAV,EAAgB;EACdJ,IAAAA,YAAY,CAACM,GAAb,CA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7D;;EAEA,QAAIA,OAAO,CAACuB,oBAAR,IAAgCpB,MAApC,EAA4C;EAC1C,UAAMqB,WAAW,GAAG,EAApB;;EACA,UAAMN,KAAI,GAAGC,MAAM,CAACD,IAAP,CAAYf,MAAZ,CAAb;;EACA,WAAK,IAAIV,EAAC,GAAG,CAAb,EAAgBA,EAAC,GAAGyB,KAAI,CAACxB,MAAzB,EAAiCD,EAAC,EAAlC,EAAsC;EACpC,YAAMgC,GAAG,GAAGP,KAAI,CAACzB,EAAD,CAAhB;;EACA,YAAI,CAACc,MAAM,CAACW,IAAP,CAAYO,GAAZ,CAAL,EAAuB;EACrBD,UAAAA,WAAW,CAACC,GAAD,CAAX,GAAmBtB,MAAM,CAACsB,GAAD,CAAzB;EACD;EACF;;EACD,UAAMC,KAAK,GAAG1B,OAAO,CAACuB,oBAAR,CAA6BC,WAA7B,CAAd;;EACA,UAAIE,KAAJ,EAAW;EACTL,QAAAA,GAAG,IAAIK,KAAK,CAACC,MAAN,CAAa,CAAb,MAAoB,GAApB,GAA0BD,KAA1B,SAAsCA,KAA7C;EACD;EACF;;EAED,WAAOL,GAAP;EACD,GAtDD;EAuDD;;;;;;;;"} \ No newline at end of file diff --git a/dist/universal-router-generate-urls.min.js b/dist/universal-router-generate-urls.min.js index 7f57206..f541241 100644 --- a/dist/universal-router-generate-urls.min.js +++ b/dist/universal-router-generate-urls.min.js @@ -1,3 +1,3 @@ /*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */ -!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r(require("path-to-regexp")):"function"==typeof define&&define.amd?define(["path-to-regexp"],r):(e=e||self).generateUrls=r(e.UniversalRouter.pathToRegexp)}(this,function(w){"use strict";var x=new Map;return function(v,g){if(void 0===g&&(g={}),!v)throw new ReferenceError("Router is not defined");return v.routesByName=v.routesByName||new Map,function(e,r){var t=v.routesByName.get(e);if(!(t||(v.routesByName.clear(),function e(r,t,a){if(r.has(t.name))throw new Error('Route "'+t.name+'" already exists');if(t.name&&r.set(t.name,t),a)for(var n=0;n {\n let route = router.routesByName.get(routeName)\n if (!route) {\n router.routesByName.clear() // clear cache\n cacheRoutes(router.routesByName, router.root, router.root.children)\n\n route = router.routesByName.get(routeName)\n if (!route) {\n throw new Error(`Route \"${routeName}\" not found`)\n }\n }\n\n let regexp = cache.get(route.fullPath)\n if (!regexp) {\n let fullPath = ''\n let rt = route\n while (rt) {\n const path = Array.isArray(rt.path) ? rt.path[0] : rt.path\n if (path) {\n fullPath = path + fullPath\n }\n rt = rt.parent\n }\n const tokens = pathToRegexp.parse(fullPath)\n const toPath = pathToRegexp.tokensToFunction(tokens)\n const keys = Object.create(null)\n for (let i = 0; i < tokens.length; i++) {\n if (typeof tokens[i] !== 'string') {\n keys[tokens[i].name] = true\n }\n }\n regexp = { toPath, keys }\n cache.set(fullPath, regexp)\n route.fullPath = fullPath\n }\n\n let url = router.baseUrl + regexp.toPath(params, options) || '/'\n\n if (options.stringifyQueryParams && params) {\n const queryParams = {}\n const keys = Object.keys(params)\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i]\n if (!regexp.keys[key]) {\n queryParams[key] = params[key]\n }\n }\n const query = options.stringifyQueryParams(queryParams)\n if (query) {\n url += query.charAt(0) === '?' ? query : `?${query}`\n }\n }\n\n return url\n }\n}\n\nexport default generateUrls\n"],"names":["cache","Map","router","options","ReferenceError","routesByName","routeName","params","route","get","clear","cacheRoutes","routes","has","name","Error","set","i","length","childRoute","parent","children","root","regexp","fullPath","rt","path","Array","isArray","tokens","pathToRegexp","parse","toPath","tokensToFunction","keys","Object","create","url","baseUrl","stringifyQueryParams","queryParams","key","query","charAt"],"mappings":";6QAWA,IAAMA,EAAQ,IAAIC,WAoBlB,SAAsBC,EAAQC,eAAAA,IAAAA,EAAU,KACjCD,QACG,IAAIE,eAAe,gCAG3BF,EAAOG,aAAeH,EAAOG,cAAgB,IAAIJ,IAE1C,SAACK,EAAWC,OACbC,EAAQN,EAAOG,aAAaI,IAAI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EAAQoC,qBAAqBC,GACvCE,IACFL,GAA2B,MAApBK,EAAMC,OAAO,GAAaD,MAAYA,UAI1CL"} \ No newline at end of file +{"version":3,"file":"universal-router-generate-urls.min.js","sources":["src/generateUrls.js"],"sourcesContent":["/**\n * Universal Router (https://www.kriasoft.com/universal-router/)\n *\n * Copyright (c) 2015-present Kriasoft.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE.txt file in the root directory of this source tree.\n */\n\nimport pathToRegexp from 'path-to-regexp'\n\nconst cache = new Map()\n\nfunction cacheRoutes(routesByName, route, routes) {\n if (routesByName.has(route.name)) {\n throw new Error(`Route \"${route.name}\" already exists`)\n }\n\n if (route.name) {\n routesByName.set(route.name, route)\n }\n\n if (routes) {\n for (let i = 0; i < routes.length; i++) {\n const childRoute = routes[i]\n childRoute.parent = route\n cacheRoutes(routesByName, childRoute, childRoute.children)\n }\n }\n}\n\nfunction generateUrls(router, options = {}) {\n if (!router) {\n throw new ReferenceError('Router is not defined')\n }\n\n router.routesByName = router.routesByName || new Map()\n\n return (routeName, params) => {\n let route = router.routesByName.get(routeName)\n if (!route) {\n router.routesByName.clear() // clear cache\n cacheRoutes(router.routesByName, router.root, router.root.children)\n\n route = router.routesByName.get(routeName)\n if (!route) {\n throw new Error(`Route \"${routeName}\" not found`)\n }\n }\n\n let regexp = cache.get(route.fullPath)\n if (!regexp) {\n let fullPath = ''\n let rt = route\n while (rt) {\n const path = Array.isArray(rt.path) ? rt.path[0] : rt.path\n if (path) {\n fullPath = path + fullPath\n }\n rt = rt.parent\n }\n const tokens = pathToRegexp.parse(fullPath, options)\n const toPath = pathToRegexp.tokensToFunction(tokens, options)\n const keys = Object.create(null)\n for (let i = 0; i < tokens.length; i++) {\n if (typeof tokens[i] !== 'string') {\n keys[tokens[i].name] = true\n }\n }\n regexp = { toPath, keys }\n cache.set(fullPath, regexp)\n route.fullPath = fullPath\n }\n\n let url = router.baseUrl + regexp.toPath(params, options) || '/'\n\n if (options.stringifyQueryParams && params) {\n const queryParams = {}\n const keys = Object.keys(params)\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i]\n if (!regexp.keys[key]) {\n queryParams[key] = params[key]\n }\n }\n const query = options.stringifyQueryParams(queryParams)\n if (query) {\n url += query.charAt(0) === '?' ? query : `?${query}`\n }\n }\n\n return url\n }\n}\n\nexport default generateUrls\n"],"names":["cache","Map","router","options","ReferenceError","routesByName","routeName","params","route","get","clear","cacheRoutes","routes","has","name","Error","set","i","length","childRoute","parent","children","root","regexp","fullPath","rt","path","Array","isArray","tokens","pathToRegexp","parse","toPath","tokensToFunction","keys","Object","create","url","baseUrl","stringifyQueryParams","queryParams","key","query","charAt"],"mappings":";6QAWA,IAAMA,EAAQ,IAAIC,WAoBlB,SAAsBC,EAAQC,eAAAA,IAAAA,EAAU,KACjCD,QACG,IAAIE,eAAe,gCAG3BF,EAAOG,aAAeH,EAAOG,cAAgB,IAAIJ,IAE1C,SAACK,EAAWC,OACbC,EAAQN,EAAOG,aAAaI,IAAIH,QAC/BE,IACHN,EAAOG,aAAaK,QA5B1B,SAASC,EAAYN,EAAcG,EAAOI,MACpCP,EAAaQ,IAAIL,EAAMM,YACnB,IAAIC,gBAAgBP,EAAMM,4BAG9BN,EAAMM,MACRT,EAAaW,IAAIR,EAAMM,KAAMN,GAG3BI,MACG,IAAIK,EAAI,EAAGA,EAAIL,EAAOM,OAAQD,IAAK,KAChCE,EAAaP,EAAOK,GAC1BE,EAAWC,OAASZ,EACpBG,EAAYN,EAAcc,EAAYA,EAAWE,WAgBjDV,CAAYT,EAAOG,aAAcH,EAAOoB,KAAMpB,EAAOoB,KAAKD,UAE1Db,EAAQN,EAAOG,aAAaI,IAAIH,WAExB,IAAIS,gBAAgBT,qBAI1BiB,EAASvB,EAAMS,IAAID,EAAMgB,cACxBD,EAAQ,SACPC,EAAW,GACXC,EAAKjB,EACFiB,GAAI,KACHC,EAAOC,MAAMC,QAAQH,EAAGC,MAAQD,EAAGC,KAAK,GAAKD,EAAGC,KAClDA,IACFF,EAAWE,EAAOF,GAEpBC,EAAKA,EAAGL,eAEJS,EAASC,EAAaC,MAAMP,EAAUrB,GACtC6B,EAASF,EAAaG,iBAAiBJ,EAAQ1B,GAC/C+B,EAAOC,OAAOC,OAAO,MAClBnB,EAAI,EAAGA,EAAIY,EAAOX,OAAQD,IACR,iBAAdY,EAAOZ,KAChBiB,EAAKL,EAAOZ,GAAGH,OAAQ,GAG3BS,EAAS,CAAES,OAAAA,EAAQE,KAAAA,GACnBlC,EAAMgB,IAAIQ,EAAUD,GACpBf,EAAMgB,SAAWA,MAGfa,EAAMnC,EAAOoC,QAAUf,EAAOS,OAAOzB,EAAQJ,IAAY,OAEzDA,EAAQoC,sBAAwBhC,EAAQ,SACpCiC,EAAc,GACdN,EAAOC,OAAOD,KAAK3B,GAChBU,EAAI,EAAGA,EAAIiB,EAAKhB,OAAQD,IAAK,KAC9BwB,EAAMP,EAAKjB,GACZM,EAAOW,KAAKO,KACfD,EAAYC,GAAOlC,EAAOkC,QAGxBC,EAAQvC,EAAQoC,qBAAqBC,GACvCE,IACFL,GAA2B,MAApBK,EAAMC,OAAO,GAAaD,MAAYA,UAI1CL"} \ No newline at end of file diff --git a/package.json b/package.json index 8249ca9..fb17f6e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "universal-router", - "version": "8.2.1", + "version": "8.3.0", "description": "Isomorphic router for JavaScript web applications", "homepage": "https://www.kriasoft.com/universal-router/", "repository": "kriasoft/universal-router",