js-component-router
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,1 +0,1 @@ | ||
define("js-component-router",[],(function(){return function(){"use strict";var e,t={d:function(e,o){for(var n in o)t.o(o,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:o[n]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},o={};return t.r(o),t.d(o,{RouterMode:function(){return e}}),function(e){e.history="history",e.hash="hash"}(e||(e={})),o}()})); | ||
define("js-component-router",[],(function(){return function(){"use strict";var t,e={d:function(t,n){for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r:function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},n={};function r(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function o(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}e.r(n),e.d(n,{RouterMode:function(){return t},Router:function(){return i}}),function(t){t.history="history",t.hash="hash"}(t||(t={}));var i=function(){function e(n){var r=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),o(this,"routes",[]),o(this,"root","/#/"),this.mode=window.history.pushState?t.history:t.hash,this.listen(n),window.router=this,window.addEventListener("load",(function(){return r.listen({},!0)})),this.isHistoryMode()?window.history.pushState=new Proxy(window.history.pushState,{apply:function(t,e,n){var o=t.apply(e,n);return r.listen({}),o}}):window.addEventListener("hashchange",(function(){return r.listen({})}))}var n,i;return n=e,i=[{key:"add",value:function(t,e){return this.routes.push({path:t,cb:e}),this}},{key:"remove",value:function(t){for(var e=0;e<this.routes.length;e++)if(this.routes[e].path===t)return this.routes.slice(e,1),this;return this}},{key:"removeAll",value:function(){return this.routes=[],this}},{key:"navigate",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return this.isHistoryMode()?window.history.pushState(null,null,this.root+this.clearSlashes(t)):window.location.href="".concat(window.location.href.replace(/#(.*)$/,""),"#").concat(t),this}},{key:"listen",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];null!=t&&t.mode&&(this.mode=t.mode),null!=t&&t.root&&(this.root=t.root),this.interval(e)}},{key:"interval",value:function(t){var e=this;(this.getFragment()!==this.current||t)&&(this.current=this.getFragment(),this.routes.some((function(t){var n=e.current.match(t.path);return!!n&&(n.shift(),t.cb.apply({},n),n)})))}},{key:"clearSlashes",value:function(t){return t.toString().replace(/\/$/,"").replace(/^\//,"")}},{key:"isHistoryMode",value:function(){return this.mode===t.history}},{key:"getFragment",value:function(){var t,e=window.location.href.match(/#(.*)$/);return this.isHistoryMode()?(t=(t=this.clearSlashes(decodeURIComponent(window.location.pathname+window.location.search))).replace(/\?(.*)$/,""),t="/"!==this.root?t.replace(this.root,""):t):t=e?e[1]:"",this.clearSlashes(t)}}],i&&r(n.prototype,i),Object.defineProperty(n,"prototype",{writable:!1}),e}();return n}()})); |
@@ -1,1 +0,28 @@ | ||
export * from './router'; | ||
export interface Route { | ||
path: RegExp | string; | ||
cb: Function; | ||
} | ||
export interface RouterConfig { | ||
mode?: RouterMode; | ||
root?: string; | ||
} | ||
export declare enum RouterMode { | ||
history = "history", | ||
hash = "hash" | ||
} | ||
export declare class Router { | ||
private routes; | ||
private mode; | ||
private root; | ||
private current?; | ||
constructor(config: RouterConfig); | ||
add(path: RegExp | string, cb: Function): Router; | ||
remove(path: string): Router; | ||
removeAll(): Router; | ||
navigate(path?: string): Router; | ||
listen(config: RouterConfig, isInit?: boolean): void; | ||
private interval; | ||
private clearSlashes; | ||
private isHistoryMode; | ||
private getFragment; | ||
} |
{ | ||
"name": "js-component-router", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
132
src/index.ts
@@ -1,1 +0,131 @@ | ||
export * from './router'; | ||
export interface Route { | ||
path: RegExp | string; | ||
cb: Function; | ||
} | ||
export interface RouterConfig { | ||
mode?: RouterMode; | ||
root?: string; | ||
} | ||
export enum RouterMode { | ||
history = 'history', | ||
hash = 'hash', | ||
} | ||
export class Router { | ||
private routes: Route[] = []; | ||
private mode: RouterMode; | ||
private root = '/#/'; | ||
private current?: string; | ||
public constructor(config: RouterConfig) { | ||
this.mode = window.history.pushState ? RouterMode.history : RouterMode.hash; | ||
this.listen(config); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
window.router = this; | ||
window.addEventListener('load', () => this.listen({}, true)); | ||
if (this.isHistoryMode()) { | ||
window.history.pushState = new Proxy(window.history.pushState, { | ||
apply: (target, thisArg, argArray) => { | ||
const result = target.apply(thisArg, argArray); | ||
this.listen({}); | ||
return result; | ||
}, | ||
}); | ||
} else { | ||
window.addEventListener('hashchange', () => this.listen({})); | ||
} | ||
} | ||
public add(path: RegExp | string, cb: Function): Router { | ||
this.routes.push({ | ||
path, | ||
cb, | ||
}); | ||
return this; | ||
} | ||
public remove(path: string): Router { | ||
for (let i = 0; i < this.routes.length; i++) { | ||
if (this.routes[i].path === path) { | ||
this.routes.slice(i, 1); | ||
return this; | ||
} | ||
} | ||
return this; | ||
} | ||
public removeAll(): Router { | ||
this.routes = []; | ||
return this; | ||
} | ||
public navigate(path = ''): Router { | ||
if (this.isHistoryMode()) { | ||
window.history.pushState(null, null, this.root + this.clearSlashes(path)); | ||
} else { | ||
window.location.href = `${window.location.href.replace( | ||
/#(.*)$/, | ||
'' | ||
)}#${path}`; | ||
} | ||
return this; | ||
} | ||
public listen(config: RouterConfig, isInit = false) { | ||
if (config?.mode) this.mode = config.mode; | ||
if (config?.root) this.root = config.root; | ||
this.interval(isInit); | ||
} | ||
private interval(isInit: boolean): void { | ||
if (this.getFragment() === this.current && !isInit) { | ||
return; | ||
} | ||
this.current = this.getFragment(); | ||
this.routes.some((route) => { | ||
const match = this.current.match(route.path); | ||
if (match) { | ||
match.shift(); | ||
route.cb.apply({}, match); | ||
return match; | ||
} | ||
return false; | ||
}); | ||
} | ||
private clearSlashes(path: string): string { | ||
return path.toString().replace(/\/$/, '').replace(/^\//, ''); | ||
} | ||
private isHistoryMode(): boolean { | ||
return this.mode === RouterMode.history; | ||
} | ||
private getFragment(): string { | ||
const match = window.location.href.match(/#(.*)$/); | ||
let fragment: string; | ||
if (this.isHistoryMode()) { | ||
fragment = this.clearSlashes( | ||
decodeURIComponent(window.location.pathname + window.location.search) | ||
); | ||
fragment = fragment.replace(/\?(.*)$/, ''); | ||
fragment = this.root !== '/' ? fragment.replace(this.root, '') : fragment; | ||
} else { | ||
fragment = match ? match[1] : ''; | ||
} | ||
return this.clearSlashes(fragment); | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
11133
14
257
1