absolute-router
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -103,65 +103,73 @@ (function (exports) { | ||
function createBranch(parent, phrase){ | ||
var Branch = function Branch(ref){ | ||
if ( ref === void 0 ) ref = {}; | ||
var parent = ref.parent; if ( parent === void 0 ) parent = {}; | ||
var phrase = ref.phrase; if ( phrase === void 0 ) phrase = ''; | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
var info = { | ||
var info = this.__info = { | ||
name: phrase, | ||
type: 'normal', | ||
parent: parent, | ||
property: phrase, | ||
child: null, | ||
parent: parent, | ||
children: [] | ||
}; | ||
var branch = {__info: info}; | ||
parent[phrase] = this; | ||
var setParam = function (type) { | ||
info.type = type; | ||
if(phrase[0] === ':'){ | ||
info.type = 'parameter'; | ||
info.property = phrase.slice(1); | ||
if(parent.__info.child){ | ||
throw new Error(("Parent branch " + (parent.name) + " already has a child.")); | ||
} | ||
parent.__info.child = phrase; | ||
}; | ||
var setPattern = function (type) { | ||
var ref = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref[0]; | ||
var pattern = ref[1]; | ||
var name = ref[2]; | ||
info.type = type; | ||
}else if(phrase[0] === '*'){ | ||
info.type = 'splat'; | ||
info.property = phrase.slice(1); | ||
parent.__info.child = phrase; | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
var ref$1 = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref$1[0]; | ||
var pattern = ref$1[1]; | ||
var name = ref$1[2]; | ||
info.type = 'regex'; | ||
info.property = name; | ||
info.pattern = new RegExp(pattern); | ||
parent.__info.children.push(phrase); | ||
}; | ||
if(phrase.length){ | ||
info.property = phrase; | ||
if(phrase[0] === ':'){ | ||
setParam('parameter'); | ||
}else if(phrase[0] === '*'){ | ||
setParam('splat'); | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
setPattern('regex'); | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
}else{ | ||
info.type = 'normal'; | ||
} | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
} | ||
}; | ||
Branch.leafFrom = function leafFrom (branch, ref){ | ||
if ( ref === void 0 ) ref = {}; | ||
var path = ref.path; if ( path === void 0 ) path = ''; | ||
var handler = ref.handler; if ( handler === void 0 ) handler = null; | ||
var type = ref.type; if ( type === void 0 ) type = null; | ||
parent[phrase] = branch; | ||
branch.__info.type = type | ||
? type | ||
: branch.__info.type; | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return branch; | ||
} | ||
}; | ||
Branch.create = function create (ref){ | ||
var parent = ref.parent; | ||
var phrase = ref.phrase; | ||
function createRouteTree(base, path, handler){ | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
return new Branch({parent: parent, phrase: phrase}); | ||
}; | ||
var createLeaf = function (branch) { | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return base; | ||
}; | ||
var Tree = function Tree () {}; | ||
Tree.prototype.constuctor = function constuctor (){ | ||
this.__info = {}; | ||
}; | ||
Tree.branch = function branch (base, ref){ | ||
var path = ref.path; | ||
var handler = ref.handler; | ||
if(path === '/'){ | ||
@@ -172,9 +180,17 @@ base.__info = { | ||
return createLeaf(base); | ||
return Branch.leafFrom(base, { | ||
path: path, | ||
handler: handler, | ||
type: 'root' | ||
}); | ||
} | ||
var leaf = path.split('/').slice(1).reduce(createBranch, base); | ||
return createLeaf(leaf); | ||
} | ||
var leaf = path.split('/').slice(1) | ||
.reduce(function (parent, phrase){ | ||
return Branch.create({parent: parent, phrase: phrase}) | ||
}, base); | ||
return Branch.leafFrom(leaf, {path: path, handler: handler}); | ||
}; | ||
var RouteResolver = function RouteResolver(router, address, args, base){ | ||
@@ -339,3 +355,4 @@ this.router = router; | ||
this['@@router'] = true; | ||
this.routes = {}; | ||
//this.routes = {}; | ||
this.routes = new Tree(); | ||
@@ -359,3 +376,7 @@ if(typeof relay !== 'function'){ | ||
Object.keys(routes).forEach(function (pattern){ | ||
createRouteTree(this$1.routes, pattern, routes[pattern]); | ||
Tree.branch(this$1.routes, { | ||
path: pattern, | ||
handler: routes[pattern] | ||
}); | ||
//createRouteTree(this.routes, pattern, routes[pattern]); | ||
}); | ||
@@ -362,0 +383,0 @@ return this; |
@@ -1,2 +0,2 @@ | ||
!function(t){"use strict";function e(t,e){if(t[e])return t[e];var r={name:e,child:null,parent:t,children:[]},n={__info:r},i=function(n){if(r.type=n,r.property=e.slice(1),t.__info.child)throw new Error("Parent branch "+t.name+" already has a child.");t.__info.child=e};return e.length&&(r.property=e,":"===e[0]?i("parameter"):"*"===e[0]?i("splat"):/^\{[\s\S]+?\}[\s\S]+$/.test(e)?function(n){var i=e.match(/^\{([\s\S]+?)\}([\s\S]+)$/),o=(i[0],i[1]),u=i[2];r.type=n,r.property=u,r.pattern=new RegExp(o),t.__info.children.push(e)}("regex"):/^[0-5]{3}/.test(e)?r.type="error":r.type="normal"),t[e]=n,n}function r(t,r,n){var i=function(e){return e.__info.handler=n,e.__info.children=[],e.__info.path=r,t};return"/"===r?(t.__info={type:"root"},i(t)):i(r.split("/").slice(1).reduce(e,t))}function n(t,e){void 0===e&&(e={});var r=new Error(""+t);return Object.keys(e).forEach(function(t){return r[t]=e[t]}),Promise.reject(r)}var i="undefined"!=typeof window&&"undefined"!=typeof document&&window.URLSearchParams?function(t){for(var e={},r=new URLSearchParams(t.search).entries();;){var n=r.next();if(n.done)break;var i=n.value,o=i[0],u=i[1];e[o]=u}return e}:function(t){return t.search.length?t.search.slice(1).split("&").reduce(function(t,e){var r=e.split("="),n=r[0],i=r[1];return t[n]=i,t},{}):{}},o="undefined"!=typeof window&&"undefined"!=typeof document?window.URL?function(t,e){return/^http/.test(t)?new URL(t,e):new URL(t,window.location.origin)}:function(t){var e=document.createElement("a");return e.href=t,e}:function(t,e){var r=require("url").URL;return"/"===t[0]?new r(t,e||"http://local"):new r(t)},u=function(t){this.store=t,isNaN(t.expire)&&(t.expire=1/0),this.start=Date.now()},s={expire:{},expired:{}};s.expire.get=function(){return this.store.expire},s.expired.get=function(){return this.expire!==1/0&&Date.now()>=this.start+this.expire},u.prototype.respond=function(){this.store.respond()},Object.defineProperties(u.prototype,s),u.isView=function(t){return"object"==typeof t&&"respond"in t&&"function"==typeof t.respond};var a=function(t,e,r,n){this.router=t,this.args=r,this.address=e,this.url=t.location=o(e,n),this.pathname=this.url.pathname,this.handler=null,this.found=!1,this.params={},t.location=this.url,this._route=null},h={route:{},views:{}};h.route.set=function(t){t&&(this._route=t,this.handler=t.__info.handler,this.found=!0,this.search=i(this.url))},h.route.get=function(){return this._route},h.views.get=function(){return this.route?this.route.__info.views:{}},a.prototype.matchRoute=function(){var t=this.router.routes,e=this.pathname;if("/"===e&&t.__info)return this.route=t,this;var r,n,i=e.split("/").slice(1),o=t,u=i.length,s=this.params,a=0,h=function(t,e){s[t.__info.property]=e};for(a=0;a<u;a++){if(n=i[a],r=o,!((o=o[n])||(r.__info.children.length&&function(t,e){for(var r=t.__info.children,n=0;n<r.length;n++){var i=t[r[n]],u=function(t){return e.match(t.__info.pattern)}(i);if(u)return h(i,u),o=t[i.__info.name]}}(r,n),o=function(t){if(t.__info.child)return t[t.__info.child]}(r))))break;"parameter"===o.__info.type?h(o,n):"splat"===o.__info.type&&function(t){for(var e=[];a<u&&!t[i[a]];a++)e.push(i[a]);h(t,e)}(o)}return o&&(this.route=o),this},a.prototype._getArguments=function(){return[this.params,this.search].concat(this.args)},a.prototype.runRoute=function(){if(!this.found){var t=new Error("not found");return t.status=404,Promise.reject(t)}return this.handler.apply(this.router,this._getArguments())},a.prototype.resolve=function(){var t=this;return Promise.resolve().then(function(){return t.matchRoute().runRoute()})},Object.defineProperties(a.prototype,h);var f=function(t){var e=this,r=t.relay,n=t.base;if(this["@@router"]=!0,this.routes={},"function"!=typeof r)throw new Error("options.relay is not a function");r({navigate:function(t,r){return void 0===r&&(r=[]),new a(e,t,r,n).resolve()}})};f.prototype.route=function(t){var e=this;return Object.keys(t).forEach(function(n){r(e.routes,n,t[n])}),this},f.prototype.reject=function(t,e){return n(t,e)},t.Router=f}(this.absoluteRouter=this.absoluteRouter||{}); | ||
!function(t){"use strict";var e="undefined"!=typeof window&&"undefined"!=typeof document&&window.URLSearchParams?function(t){for(var e={},r=new URLSearchParams(t.search).entries();;){var n=r.next();if(n.done)break;var i=n.value,o=i[0],u=i[1];e[o]=u}return e}:function(t){return t.search.length?t.search.slice(1).split("&").reduce(function(t,e){var r=e.split("="),n=r[0],i=r[1];return t[n]=i,t},{}):{}},r="undefined"!=typeof window&&"undefined"!=typeof document?window.URL?function(t,e){return/^http/.test(t)?new URL(t,e):new URL(t,window.location.origin)}:function(t){var e=document.createElement("a");return e.href=t,e}:function(t,e){var r=require("url").URL;return"/"===t[0]?new r(t,e||"http://local"):new r(t)},n=function(t){this.store=t,isNaN(t.expire)&&(t.expire=1/0),this.start=Date.now()},i={expire:{},expired:{}};i.expire.get=function(){return this.store.expire},i.expired.get=function(){return this.expire!==1/0&&Date.now()>=this.start+this.expire},n.prototype.respond=function(){this.store.respond()},Object.defineProperties(n.prototype,i),n.isView=function(t){return"object"==typeof t&&"respond"in t&&"function"==typeof t.respond};var o=function(t){void 0===t&&(t={});var e=t.parent;void 0===e&&(e={});var r=t.phrase;void 0===r&&(r="");var n=this.__info={name:r,type:"normal",parent:e,property:r,child:null,children:[]};if(e[r]=this,":"===r[0])n.type="parameter",n.property=r.slice(1),e.__info.child=r;else if("*"===r[0])n.type="splat",n.property=r.slice(1),e.__info.child=r;else if(/^\{[\s\S]+?\}[\s\S]+$/.test(r)){var i=r.match(/^\{([\s\S]+?)\}([\s\S]+)$/),o=(i[0],i[1]),u=i[2];n.type="regex",n.property=u,n.pattern=new RegExp(o),e.__info.children.push(r)}else/^[0-5]{3}/.test(r)&&(n.type="error")};o.leafFrom=function(t,e){void 0===e&&(e={});var r=e.path;void 0===r&&(r="");var n=e.handler;void 0===n&&(n=null);var i=e.type;return void 0===i&&(i=null),t.__info.type=i||t.__info.type,t.__info.handler=n,t.__info.children=[],t.__info.path=r,t},o.create=function(t){var e=t.parent,r=t.phrase;return e[r]?e[r]:new o({parent:e,phrase:r})};var u=function(){};u.prototype.constuctor=function(){this.__info={}},u.branch=function(t,e){var r=e.path,n=e.handler;if("/"===r)return t.__info={type:"root"},o.leafFrom(t,{path:r,handler:n,type:"root"});var i=r.split("/").slice(1).reduce(function(t,e){return o.create({parent:t,phrase:e})},t);return o.leafFrom(i,{path:r,handler:n})};var s=function(t,e,n,i){this.router=t,this.args=n,this.address=e,this.url=t.location=r(e,i),this.pathname=this.url.pathname,this.handler=null,this.found=!1,this.params={},t.location=this.url,this._route=null},a={route:{},views:{}};a.route.set=function(t){t&&(this._route=t,this.handler=t.__info.handler,this.found=!0,this.search=e(this.url))},a.route.get=function(){return this._route},a.views.get=function(){return this.route?this.route.__info.views:{}},s.prototype.matchRoute=function(){var t=this.router.routes,e=this.pathname;if("/"===e&&t.__info)return this.route=t,this;var r,n,i=e.split("/").slice(1),o=t,u=i.length,s=this.params,a=0,h=function(t,e){s[t.__info.property]=e},f=function(t){if(t.__info.child)return t[t.__info.child]},p=function(t,e){for(var r=t.__info.children,n=0;n<r.length;n++){var i=t[r[n]],u=(s=i,e.match(s.__info.pattern));if(u)return h(i,u),o=t[i.__info.name]}var s;return t},c=function(t){for(var e=[];a<u&&!t[i[a]];a++)e.push(i[a]);h(t,e)};for(a=0;a<u;a++){if(n=i[a],r=o,!((o=o[n])||(r.__info.children.length&&p(r,n),o=f(r))))break;"parameter"===o.__info.type?h(o,n):"splat"===o.__info.type&&c(o)}return o&&(this.route=o),this},s.prototype._getArguments=function(){return[this.params,this.search].concat(this.args)},s.prototype.runRoute=function(){if(!this.found){var t=new Error("not found");return t.status=404,Promise.reject(t)}return this.handler.apply(this.router,this._getArguments())},s.prototype.resolve=function(){var t=this;return Promise.resolve().then(function(){return t.matchRoute().runRoute()})},Object.defineProperties(s.prototype,a);var h=function(t){var e=this,r=t.relay,n=t.base;if(this["@@router"]=!0,this.routes=new u,"function"!=typeof r)throw new Error("options.relay is not a function");r({navigate:function(t,r){void 0===r&&(r=[]);return new s(e,t,r,n).resolve()}})};h.prototype.route=function(t){var e=this;return Object.keys(t).forEach(function(r){u.branch(e.routes,{path:r,handler:t[r]})}),this},h.prototype.reject=function(t,e){return function(t,e){void 0===e&&(e={});var r=new Error(""+t);return Object.keys(e).forEach(function(t){return r[t]=e[t]}),Promise.reject(r)}(t,e)},t.Router=h}(this.absoluteRouter=this.absoluteRouter||{}); | ||
//# sourceMappingURL=absolute-router.min.js.map |
@@ -100,65 +100,73 @@ var getSearch = (function (){ | ||
function createBranch(parent, phrase){ | ||
var Branch = function Branch(ref){ | ||
if ( ref === void 0 ) ref = {}; | ||
var parent = ref.parent; if ( parent === void 0 ) parent = {}; | ||
var phrase = ref.phrase; if ( phrase === void 0 ) phrase = ''; | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
var info = { | ||
var info = this.__info = { | ||
name: phrase, | ||
type: 'normal', | ||
parent: parent, | ||
property: phrase, | ||
child: null, | ||
parent: parent, | ||
children: [] | ||
}; | ||
var branch = {__info: info}; | ||
parent[phrase] = this; | ||
var setParam = function (type) { | ||
info.type = type; | ||
if(phrase[0] === ':'){ | ||
info.type = 'parameter'; | ||
info.property = phrase.slice(1); | ||
if(parent.__info.child){ | ||
throw new Error(("Parent branch " + (parent.name) + " already has a child.")); | ||
} | ||
parent.__info.child = phrase; | ||
}; | ||
var setPattern = function (type) { | ||
var ref = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref[0]; | ||
var pattern = ref[1]; | ||
var name = ref[2]; | ||
info.type = type; | ||
}else if(phrase[0] === '*'){ | ||
info.type = 'splat'; | ||
info.property = phrase.slice(1); | ||
parent.__info.child = phrase; | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
var ref$1 = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref$1[0]; | ||
var pattern = ref$1[1]; | ||
var name = ref$1[2]; | ||
info.type = 'regex'; | ||
info.property = name; | ||
info.pattern = new RegExp(pattern); | ||
parent.__info.children.push(phrase); | ||
}; | ||
if(phrase.length){ | ||
info.property = phrase; | ||
if(phrase[0] === ':'){ | ||
setParam('parameter'); | ||
}else if(phrase[0] === '*'){ | ||
setParam('splat'); | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
setPattern('regex'); | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
}else{ | ||
info.type = 'normal'; | ||
} | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
} | ||
}; | ||
Branch.leafFrom = function leafFrom (branch, ref){ | ||
if ( ref === void 0 ) ref = {}; | ||
var path = ref.path; if ( path === void 0 ) path = ''; | ||
var handler = ref.handler; if ( handler === void 0 ) handler = null; | ||
var type = ref.type; if ( type === void 0 ) type = null; | ||
parent[phrase] = branch; | ||
branch.__info.type = type | ||
? type | ||
: branch.__info.type; | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return branch; | ||
} | ||
}; | ||
Branch.create = function create (ref){ | ||
var parent = ref.parent; | ||
var phrase = ref.phrase; | ||
function createRouteTree(base, path, handler){ | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
return new Branch({parent: parent, phrase: phrase}); | ||
}; | ||
var createLeaf = function (branch) { | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return base; | ||
}; | ||
var Tree = function Tree () {}; | ||
Tree.prototype.constuctor = function constuctor (){ | ||
this.__info = {}; | ||
}; | ||
Tree.branch = function branch (base, ref){ | ||
var path = ref.path; | ||
var handler = ref.handler; | ||
if(path === '/'){ | ||
@@ -169,9 +177,17 @@ base.__info = { | ||
return createLeaf(base); | ||
return Branch.leafFrom(base, { | ||
path: path, | ||
handler: handler, | ||
type: 'root' | ||
}); | ||
} | ||
var leaf = path.split('/').slice(1).reduce(createBranch, base); | ||
return createLeaf(leaf); | ||
} | ||
var leaf = path.split('/').slice(1) | ||
.reduce(function (parent, phrase){ | ||
return Branch.create({parent: parent, phrase: phrase}) | ||
}, base); | ||
return Branch.leafFrom(leaf, {path: path, handler: handler}); | ||
}; | ||
var RouteResolver = function RouteResolver(router, address, args, base){ | ||
@@ -336,3 +352,4 @@ this.router = router; | ||
this['@@router'] = true; | ||
this.routes = {}; | ||
//this.routes = {}; | ||
this.routes = new Tree(); | ||
@@ -356,3 +373,7 @@ if(typeof relay !== 'function'){ | ||
Object.keys(routes).forEach(function (pattern){ | ||
createRouteTree(this$1.routes, pattern, routes[pattern]); | ||
Tree.branch(this$1.routes, { | ||
path: pattern, | ||
handler: routes[pattern] | ||
}); | ||
//createRouteTree(this.routes, pattern, routes[pattern]); | ||
}); | ||
@@ -359,0 +380,0 @@ return this; |
@@ -104,65 +104,73 @@ 'use strict'; | ||
function createBranch(parent, phrase){ | ||
var Branch = function Branch(ref){ | ||
if ( ref === void 0 ) ref = {}; | ||
var parent = ref.parent; if ( parent === void 0 ) parent = {}; | ||
var phrase = ref.phrase; if ( phrase === void 0 ) phrase = ''; | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
var info = { | ||
var info = this.__info = { | ||
name: phrase, | ||
type: 'normal', | ||
parent: parent, | ||
property: phrase, | ||
child: null, | ||
parent: parent, | ||
children: [] | ||
}; | ||
var branch = {__info: info}; | ||
parent[phrase] = this; | ||
var setParam = function (type) { | ||
info.type = type; | ||
if(phrase[0] === ':'){ | ||
info.type = 'parameter'; | ||
info.property = phrase.slice(1); | ||
if(parent.__info.child){ | ||
throw new Error(("Parent branch " + (parent.name) + " already has a child.")); | ||
} | ||
parent.__info.child = phrase; | ||
}; | ||
var setPattern = function (type) { | ||
var ref = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref[0]; | ||
var pattern = ref[1]; | ||
var name = ref[2]; | ||
info.type = type; | ||
}else if(phrase[0] === '*'){ | ||
info.type = 'splat'; | ||
info.property = phrase.slice(1); | ||
parent.__info.child = phrase; | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
var ref$1 = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref$1[0]; | ||
var pattern = ref$1[1]; | ||
var name = ref$1[2]; | ||
info.type = 'regex'; | ||
info.property = name; | ||
info.pattern = new RegExp(pattern); | ||
parent.__info.children.push(phrase); | ||
}; | ||
if(phrase.length){ | ||
info.property = phrase; | ||
if(phrase[0] === ':'){ | ||
setParam('parameter'); | ||
}else if(phrase[0] === '*'){ | ||
setParam('splat'); | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
setPattern('regex'); | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
}else{ | ||
info.type = 'normal'; | ||
} | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
} | ||
}; | ||
Branch.leafFrom = function leafFrom (branch, ref){ | ||
if ( ref === void 0 ) ref = {}; | ||
var path = ref.path; if ( path === void 0 ) path = ''; | ||
var handler = ref.handler; if ( handler === void 0 ) handler = null; | ||
var type = ref.type; if ( type === void 0 ) type = null; | ||
parent[phrase] = branch; | ||
branch.__info.type = type | ||
? type | ||
: branch.__info.type; | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return branch; | ||
} | ||
}; | ||
Branch.create = function create (ref){ | ||
var parent = ref.parent; | ||
var phrase = ref.phrase; | ||
function createRouteTree(base, path, handler){ | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
return new Branch({parent: parent, phrase: phrase}); | ||
}; | ||
var createLeaf = function (branch) { | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return base; | ||
}; | ||
var Tree = function Tree () {}; | ||
Tree.prototype.constuctor = function constuctor (){ | ||
this.__info = {}; | ||
}; | ||
Tree.branch = function branch (base, ref){ | ||
var path = ref.path; | ||
var handler = ref.handler; | ||
if(path === '/'){ | ||
@@ -173,9 +181,17 @@ base.__info = { | ||
return createLeaf(base); | ||
return Branch.leafFrom(base, { | ||
path: path, | ||
handler: handler, | ||
type: 'root' | ||
}); | ||
} | ||
var leaf = path.split('/').slice(1).reduce(createBranch, base); | ||
return createLeaf(leaf); | ||
} | ||
var leaf = path.split('/').slice(1) | ||
.reduce(function (parent, phrase){ | ||
return Branch.create({parent: parent, phrase: phrase}) | ||
}, base); | ||
return Branch.leafFrom(leaf, {path: path, handler: handler}); | ||
}; | ||
var RouteResolver = function RouteResolver(router, address, args, base){ | ||
@@ -340,3 +356,4 @@ this.router = router; | ||
this['@@router'] = true; | ||
this.routes = {}; | ||
//this.routes = {}; | ||
this.routes = new Tree(); | ||
@@ -360,3 +377,7 @@ if(typeof relay !== 'function'){ | ||
Object.keys(routes).forEach(function (pattern){ | ||
createRouteTree(this$1.routes, pattern, routes[pattern]); | ||
Tree.branch(this$1.routes, { | ||
path: pattern, | ||
handler: routes[pattern] | ||
}); | ||
//createRouteTree(this.routes, pattern, routes[pattern]); | ||
}); | ||
@@ -363,0 +384,0 @@ return this; |
{ | ||
"name": "absolute-router", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A router for all", | ||
@@ -51,2 +51,2 @@ "main": "dist/bundle.js", | ||
"homepage": "https://github.com/hollowdoor/absolute_router#readme" | ||
} | ||
} |
import { getURL, getSearch } from './url_stuff.js'; | ||
import View from './return_view.js'; | ||
import { Tree, Branch } from './tree.js'; | ||
export { Tree }; | ||
function createBranch(parent, phrase){ | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
let info = { | ||
name: phrase, | ||
child: null, | ||
parent: parent, | ||
children: [] | ||
}; | ||
let branch = {__info: info}; | ||
const setParam = type => { | ||
info.type = type; | ||
info.property = phrase.slice(1); | ||
if(parent.__info.child){ | ||
throw new Error(`Parent branch ${parent.name} already has a child.`); | ||
} | ||
parent.__info.child = phrase; | ||
}; | ||
const setPattern = type => { | ||
let [m, pattern, name] = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
info.type = type; | ||
info.property = name; | ||
info.pattern = new RegExp(pattern); | ||
parent.__info.children.push(phrase); | ||
}; | ||
if(phrase.length){ | ||
info.property = phrase; | ||
if(phrase[0] === ':'){ | ||
setParam('parameter'); | ||
}else if(phrase[0] === '*'){ | ||
setParam('splat'); | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
setPattern('regex'); | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
}else{ | ||
info.type = 'normal'; | ||
} | ||
} | ||
parent[phrase] = branch; | ||
return branch; | ||
} | ||
export function createRouteTree(base, path, handler){ | ||
const createLeaf = (branch) => { | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return base; | ||
}; | ||
if(path === '/'){ | ||
base.__info = { | ||
type: 'root' | ||
}; | ||
return createLeaf(base); | ||
} | ||
let leaf = path.split('/').slice(1).reduce(createBranch, base); | ||
return createLeaf(leaf); | ||
} | ||
export class RouteResolver { | ||
@@ -77,0 +7,0 @@ constructor(router, address, args, base){ |
import { getURL, getSearch } from './url_stuff.js'; | ||
import { createRouteTree, RouteResolver } from './route_tree.js'; | ||
import { RouteResolver, Tree } from './route_tree.js'; | ||
import { throwError } from './throw_error.js'; | ||
@@ -11,3 +11,4 @@ | ||
this['@@router'] = true; | ||
this.routes = {}; | ||
//this.routes = {}; | ||
this.routes = new Tree(); | ||
@@ -27,3 +28,7 @@ if(typeof relay !== 'function'){ | ||
Object.keys(routes).forEach(pattern=>{ | ||
createRouteTree(this.routes, pattern, routes[pattern]); | ||
Tree.branch(this.routes, { | ||
path: pattern, | ||
handler: routes[pattern] | ||
}); | ||
//createRouteTree(this.routes, pattern, routes[pattern]); | ||
}); | ||
@@ -30,0 +35,0 @@ return this; |
121
test/code.js
@@ -103,65 +103,73 @@ (function () { | ||
function createBranch(parent, phrase){ | ||
var Branch = function Branch(ref){ | ||
if ( ref === void 0 ) { ref = {}; } | ||
var parent = ref.parent; if ( parent === void 0 ) { parent = {}; } | ||
var phrase = ref.phrase; if ( phrase === void 0 ) { phrase = ''; } | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
var info = { | ||
var info = this.__info = { | ||
name: phrase, | ||
type: 'normal', | ||
parent: parent, | ||
property: phrase, | ||
child: null, | ||
parent: parent, | ||
children: [] | ||
}; | ||
var branch = {__info: info}; | ||
parent[phrase] = this; | ||
var setParam = function (type) { | ||
info.type = type; | ||
if(phrase[0] === ':'){ | ||
info.type = 'parameter'; | ||
info.property = phrase.slice(1); | ||
if(parent.__info.child){ | ||
throw new Error(("Parent branch " + (parent.name) + " already has a child.")); | ||
} | ||
parent.__info.child = phrase; | ||
}; | ||
var setPattern = function (type) { | ||
var ref = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref[0]; | ||
var pattern = ref[1]; | ||
var name = ref[2]; | ||
info.type = type; | ||
}else if(phrase[0] === '*'){ | ||
info.type = 'splat'; | ||
info.property = phrase.slice(1); | ||
parent.__info.child = phrase; | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
var ref$1 = phrase.match(/^\{([\s\S]+?)\}([\s\S]+)$/); | ||
var m = ref$1[0]; | ||
var pattern = ref$1[1]; | ||
var name = ref$1[2]; | ||
info.type = 'regex'; | ||
info.property = name; | ||
info.pattern = new RegExp(pattern); | ||
parent.__info.children.push(phrase); | ||
}; | ||
if(phrase.length){ | ||
info.property = phrase; | ||
if(phrase[0] === ':'){ | ||
setParam('parameter'); | ||
}else if(phrase[0] === '*'){ | ||
setParam('splat'); | ||
}else if(/^\{[\s\S]+?\}[\s\S]+$/.test(phrase)){ | ||
setPattern('regex'); | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
}else{ | ||
info.type = 'normal'; | ||
} | ||
}else if(/^[0-5]{3}/.test(phrase)){ | ||
info.type = 'error'; | ||
} | ||
}; | ||
Branch.leafFrom = function leafFrom (branch, ref){ | ||
if ( ref === void 0 ) { ref = {}; } | ||
var path = ref.path; if ( path === void 0 ) { path = ''; } | ||
var handler = ref.handler; if ( handler === void 0 ) { handler = null; } | ||
var type = ref.type; if ( type === void 0 ) { type = null; } | ||
parent[phrase] = branch; | ||
branch.__info.type = type | ||
? type | ||
: branch.__info.type; | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return branch; | ||
} | ||
}; | ||
Branch.create = function create (ref){ | ||
var parent = ref.parent; | ||
var phrase = ref.phrase; | ||
function createRouteTree(base, path, handler){ | ||
if(parent[phrase]){ | ||
return parent[phrase]; | ||
} | ||
return new Branch({parent: parent, phrase: phrase}); | ||
}; | ||
var createLeaf = function (branch) { | ||
branch.__info.handler = handler; | ||
branch.__info.children = []; | ||
branch.__info.path = path; | ||
return base; | ||
}; | ||
var Tree = function Tree () {}; | ||
Tree.prototype.constuctor = function constuctor (){ | ||
this.__info = {}; | ||
}; | ||
Tree.branch = function branch (base, ref){ | ||
var path = ref.path; | ||
var handler = ref.handler; | ||
if(path === '/'){ | ||
@@ -172,9 +180,17 @@ base.__info = { | ||
return createLeaf(base); | ||
return Branch.leafFrom(base, { | ||
path: path, | ||
handler: handler, | ||
type: 'root' | ||
}); | ||
} | ||
var leaf = path.split('/').slice(1).reduce(createBranch, base); | ||
return createLeaf(leaf); | ||
} | ||
var leaf = path.split('/').slice(1) | ||
.reduce(function (parent, phrase){ | ||
return Branch.create({parent: parent, phrase: phrase}) | ||
}, base); | ||
return Branch.leafFrom(leaf, {path: path, handler: handler}); | ||
}; | ||
var RouteResolver = function RouteResolver(router, address, args, base){ | ||
@@ -339,3 +355,4 @@ this.router = router; | ||
this['@@router'] = true; | ||
this.routes = {}; | ||
//this.routes = {}; | ||
this.routes = new Tree(); | ||
@@ -359,3 +376,7 @@ if(typeof relay !== 'function'){ | ||
Object.keys(routes).forEach(function (pattern){ | ||
createRouteTree(this$1.routes, pattern, routes[pattern]); | ||
Tree.branch(this$1.routes, { | ||
path: pattern, | ||
handler: routes[pattern] | ||
}); | ||
//createRouteTree(this.routes, pattern, routes[pattern]); | ||
}); | ||
@@ -362,0 +383,0 @@ return this; |
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
233265
2739