literaljs-router
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,2 +0,2 @@ | ||
module.exports={Router:function(t){var a=t.path,e=t.routes;a&&a!==location.pathname||(a=location.pathname+location.search+location.hash);var n=document.createElement("a");if(n.href=location.origin+a,(e=e.find(function(t){return"string"==typeof t.path&&(t.path=new RegExp("^"+t.path+"$","i")),n.pathname.match(t.path)}))&&e.render)return history.pushState({},null,n.pathname+n.search+n.hash),e.render()}}; | ||
var t=!1,a=!1,n=!0;module.exports={Router:function(e){var r=e.path,o=e.routes,h=e.key;void 0===h&&(h="location");var i=e.set;t||(t=!0,window.onpopstate=function(t){var n;a=!0;var e=t.target.location;i(((n={})[h]=e.pathname+e.search+e.hash,n))}),r&&r!==location.pathname||(r=location.pathname+location.search+location.hash);var c=document.createElement("a");c.href=location.origin+r;var s=c.pathname+c.search+c.hash;if((o=o.find(function(t){return"string"==typeof t.path&&(t.path=new RegExp("^"+t.path+"$","i")),c.pathname.match(t.path)}))&&o.render)return a?a=!1:n?n=!1:history.pushState({},null,s),o.render()},getParams:function(){if(!location.search)return{};for(var t,a={},n=location.search.substring(1).split("&"),e=0;e<n.length;e++)a[(t=n[e].split("="))[0]]=t[1];return a}}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
function t(t){var a=t.path,e=t.routes;a&&a!==location.pathname||(a=location.pathname+location.search+location.hash);var n=document.createElement("a");if(n.href=location.origin+a,(e=e.find(function(t){return"string"==typeof t.path&&(t.path=new RegExp("^"+t.path+"$","i")),n.pathname.match(t.path)}))&&e.render)return history.pushState({},null,n.pathname+n.search+n.hash),e.render()}export default{Router:t};export{t as Router}; | ||
var t=!1,a=!1,e=!0;function n(n){var r=n.path,o=n.routes,h=n.key;void 0===h&&(h="location");var i=n.set;t||(t=!0,window.onpopstate=function(t){var e;a=!0;var n=t.target.location;i(((e={})[h]=n.pathname+n.search+n.hash,e))}),r&&r!==location.pathname||(r=location.pathname+location.search+location.hash);var c=document.createElement("a");c.href=location.origin+r;var p=c.pathname+c.search+c.hash;if((o=o.find(function(t){return"string"==typeof t.path&&(t.path=new RegExp("^"+t.path+"$","i")),c.pathname.match(t.path)}))&&o.render)return a?a=!1:e?e=!1:history.pushState({},null,p),o.render()}function r(){if(!location.search)return{};for(var t,a={},e=location.search.substring(1).split("&"),n=0;n<e.length;n++)a[(t=e[n].split("="))[0]]=t[1];return a}export default{Router:n,getParams:r};export{n as Router,r as getParams}; | ||
//# sourceMappingURL=index.m.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.literaljsRouter=t()}(this,function(){return{Router:function(e){var t=e.path,n=e.routes;t&&t!==location.pathname||(t=location.pathname+location.search+location.hash);var a=document.createElement("a");if(a.href=location.origin+t,(n=n.find(function(e){return"string"==typeof e.path&&(e.path=new RegExp("^"+e.path+"$","i")),a.pathname.match(e.path)}))&&n.render)return history.pushState({},null,a.pathname+a.search+a.hash),n.render()}}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.literaljsRouter=e()}(this,function(){var t=!1,e=!1,n=!0;return{Router:function(a){var o=a.path,r=a.routes,i=a.key;void 0===i&&(i="location");var h=a.set;t||(t=!0,window.onpopstate=function(t){var n;e=!0;var a=t.target.location;h(((n={})[i]=a.pathname+a.search+a.hash,n))}),o&&o!==location.pathname||(o=location.pathname+location.search+location.hash);var c=document.createElement("a");c.href=location.origin+o;var s=c.pathname+c.search+c.hash;if((r=r.find(function(t){return"string"==typeof t.path&&(t.path=new RegExp("^"+t.path+"$","i")),c.pathname.match(t.path)}))&&r.render)return e?e=!1:n?n=!1:history.pushState({},null,s),r.render()},getParams:function(){if(!location.search)return{};for(var t,e={},n=location.search.substring(1).split("&"),a=0;a<n.length;a++)e[(t=n[a].split("="))[0]]=t[1];return e}}}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "literaljs-router", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A router built to work with LiteralJS.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -12,9 +12,9 @@ <p align="center"> | ||
Recent 0.0.1 microbundle: | ||
Recent 0.0.4 microbundle: | ||
```js | ||
$ microbundle | ||
Build output to build: | ||
350 B: index.js | ||
363 B: index.m.js | ||
424 B: index.umd.js | ||
460 B: index.js | ||
478 B: index.m.js | ||
529 B: index.umd.js | ||
``` |
@@ -1,2 +0,15 @@ | ||
export function Router({ path, routes }) { | ||
let historyListenerAdded = false; | ||
let popstateTriggered = false; | ||
let initialRender = true; | ||
export function Router({ path, routes, key = 'location', set }) { | ||
if (!historyListenerAdded) { | ||
historyListenerAdded = true; | ||
window.onpopstate = function(e) { | ||
popstateTriggered = true; | ||
const location = e.target.location; | ||
set({ [key]: location.pathname + location.search + location.hash }); | ||
}; | ||
} | ||
if (!path || path === location.pathname) { | ||
@@ -9,2 +22,4 @@ // Loads the path and params if typed and provided initially | ||
hrefParser.href = location.origin + path; | ||
const incomingPath = | ||
hrefParser.pathname + hrefParser.search + hrefParser.hash; | ||
@@ -19,7 +34,12 @@ routes = routes.find(route => { | ||
if (routes && routes.render) { | ||
history.pushState( | ||
{}, | ||
null, | ||
hrefParser.pathname + hrefParser.search + hrefParser.hash | ||
); | ||
if (popstateTriggered) { | ||
popstateTriggered = false; | ||
} else { | ||
if (initialRender) { | ||
initialRender = false; | ||
} else { | ||
history.pushState({}, null, incomingPath); | ||
} | ||
} | ||
return routes.render(); | ||
@@ -29,3 +49,17 @@ } | ||
const LiteralJsRouter = { Router }; | ||
export function getParams() { | ||
if (!location.search) { | ||
return {}; | ||
} | ||
const finalParams = {}; | ||
const searchSplit = location.search.substring(1).split('&'); | ||
let currentParamSplit; | ||
for (let i = 0; i < searchSplit.length; i++) { | ||
currentParamSplit = searchSplit[i].split('='); | ||
finalParams[currentParamSplit[0]] = currentParamSplit[1]; | ||
} | ||
return finalParams; | ||
} | ||
const LiteralJsRouter = { Router, getParams }; | ||
export default LiteralJsRouter; |
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
68
326894