Created
May 25, 2018 18:28
-
-
Save janus/e544f83aa372ce25d39aa546e2dc2bec to your computer and use it in GitHub Desktop.
Skeleton for Server Router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Route() { | |
| this.routeMatch = new RouterMatch(); | |
| } | |
| Route.prototype.execReg = function(url) { | |
| let re = new RegExp(this.regs); | |
| let myArray = re.exec(url); | |
| return myArray; | |
| }; | |
| Route.prototype.addPath = function(path, callback) { | |
| this.routeMatch.build(path, callback); | |
| }; | |
| function RouterMatch(){ | |
| this.paths = {}; | |
| this.default = {[':d']:'(\d+)', [':name']:'([a-z][a-z0-9]+)'}; | |
| this.captureGroup = {}; | |
| } | |
| RouterMatch.prototype.build = function(path, callback) { | |
| if(path.includes(':')){ | |
| let parsed = parser(path, this.default); | |
| parsed.callback = callback; | |
| parsed.path = path; | |
| //console.log(parsed); | |
| this.classify(parsed); | |
| } else { | |
| this.classify({string_acc: path, result: [], callback}); | |
| } | |
| } | |
| RouterMatch.prototype.classify = function(parsed){ | |
| let count = countDefined(parsed.result); | |
| //console.log(count); | |
| let arr = this.captureGroup[String(count)] || []; | |
| arr.push(parsed); | |
| //console.log(arr); | |
| if (arr.length === 1){ | |
| this.captureGroup[String(count)] = arr; | |
| } | |
| //console.log(this.captureGroup[String(count)]); | |
| } | |
| function countDefined(arr){ | |
| let count = 0; | |
| let i = 0; | |
| let len = arr.length; | |
| while(i < len){ | |
| if(arr[i] !== undefined){ | |
| count++; | |
| } | |
| i++; | |
| } | |
| return count; | |
| } | |
| function parser(string, replace){ | |
| let len = string.length; | |
| let i = 0; | |
| let string_acc = ''; | |
| let result = []; | |
| let numPos = 0; | |
| let strPos = 0; | |
| let local_acc; | |
| while(i < len){ | |
| if(string[i] !== ':'){ | |
| string_acc += string[i]; | |
| i++; | |
| console.log('' + string_acc); | |
| } else { | |
| if(isalpha(string[i + 1]) && !isalnum(string[i + 2])){ | |
| string_acc += replace[':d']; | |
| result.push({type:'num', ident:string[i + 1], position:numPos}); | |
| i += 2; | |
| numPos++; | |
| console.log('' + string_acc); | |
| } else { | |
| if(isalpha(string[i + 1]) && isalnum(string[i + 1])){ | |
| local_acc = '' + string[i + 1] + string[i + 2]; | |
| i += 3; | |
| while(i < len && isalnum(string[i])){ | |
| local_acc += string[i]; | |
| i++; | |
| } | |
| string_acc += replace[':name']; | |
| result.push({type:'str', ident:local_acc, position:strPos}); | |
| strPos++; | |
| console.log('' + string_acc); | |
| } else { | |
| console.log('' + result); | |
| console.log('' + string_acc); | |
| throw new Error('Wrong path format'); | |
| } | |
| } | |
| } | |
| } | |
| return {string_acc, result}; | |
| } | |
| function isalpha(c) { | |
| return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))); | |
| } | |
| function isdigit(c) { | |
| return ((c >= '0') && (c <= '9')); | |
| } | |
| function isalnum(c) { | |
| return (isalpha(c) || isdigit(c)); | |
| } | |
| let mypath = 'google/:d/country/:name'; | |
| let statd = new Route(); | |
| statd.addPath(mypath, isalnum); | |
| let dfn = statd.routeMatch.captureGroup['2']; | |
| console.log(dfn[0]); | |
| console.log(parser('google/:d/country/:name', {[':d']:'(\\d+)', [':name']:'([a-z][a-z0-9]+)'})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment