the-killer-router
Advanced tools
Comparing version 0.1.1 to 1.0.0
@@ -7,3 +7,4 @@ import Routes from './routes'; | ||
super(); | ||
this.route = {}; | ||
this.route = new Map(); | ||
this.previousRoute = null; | ||
} | ||
@@ -13,8 +14,11 @@ | ||
this._reactComponent = reactComponent; | ||
this.paramRoutes = new Map(); | ||
this.routes.forEach((v, key) => /(\:\w*)/g.test(key) ? | ||
this.paramRoutes.set(key, key.split('/')) : | ||
null); | ||
} | ||
evaluteRoute(path) { | ||
if (!this[path] && !/^\w\:\/\//gi.test(path)) { | ||
return this['404']; | ||
} | ||
let view; | ||
const pathParams = path.split('/'); | ||
@@ -25,9 +29,35 @@ if (/^\w\:\/\//gi.test(path)) { | ||
return this[path]; | ||
if (this.routes.get(path)) { | ||
this.route.clear(); | ||
return this.routes.get(path); | ||
} | ||
this.previousRoute = this.actualRoute; | ||
this.paramRoutes.forEach((value, key) => { | ||
const matchParams = pathParams | ||
.filter(param => | ||
value.find(val => val === param)); | ||
if (value.length === pathParams.length && | ||
matchParams && matchParams.length | ||
) { | ||
value.forEach((val, index) => { | ||
if (pathParams[index] !== val && /\:/gi.test(val)) | ||
this.route.set(val, pathParams[index]); | ||
}); | ||
view = this.routes.get(key); | ||
} | ||
}); | ||
return view; | ||
} | ||
navigate(path, data, title) { | ||
const {view, config} = this.evaluteRoute(path); | ||
const { view, config} = this.evaluteRoute(path); | ||
this.component = view; | ||
this.activeRouteConfig = { config, data }; | ||
this.activeRoute = { | ||
...config, | ||
...data | ||
}; | ||
history.pushState(null, title, path); | ||
@@ -34,0 +64,0 @@ this._reactComponent && this._reactComponent.forceUpdate(); |
class Routes { | ||
constructor() { | ||
this.routes = new Map(); | ||
} | ||
add(path, view, config) { | ||
this[path] = {}; | ||
if (!arguments.length) return this; | ||
this[path].view = view; | ||
this[path].config = config; | ||
this.routes.set(path, { view, config }); | ||
return this; | ||
@@ -8,0 +10,0 @@ } |
{ | ||
"name": "the-killer-router", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "A router library for a view frameworks based", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5126
93
0