Last active
December 20, 2015 16:18
-
-
Save katanacrimson/6159971 to your computer and use it in GitHub Desktop.
This file contains 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 uriCompressor() | |
for(var i = 0, this.convArray = []; i <= 61; i++) { | |
if(i < 10) { | |
this.convArray.push(i) | |
} else if(i < 36) { | |
this.convArray.push(i.toString(36)) | |
} else { | |
this.convArray.push((i - 26).toString(36).toUpperCase() | |
} | |
} | |
this.character = this.build = this.order = null | |
} | |
uriCompressor.prototype.parseURI = function(uri) { | |
var parts = uri.split('/'), compressedURI = !!(parts[1] != undefined && parts[1].length !== 28) | |
// complete reset before we parse | |
this.character = this.build = this.order = null | |
if(parts[0] != undefined) | |
this.character = parts[0] | |
if(parts[1] != undefined) | |
this.build = compressedURI ? this.buildDecompress(parts[1]) : parts[1] | |
if(parts[2] != undefined) | |
this.order = compressedURI ? this.orderDecompress(parts[2]) : parts[2] | |
} | |
uriCompressor.prototype.buildCompress = function(build) { | |
return parseInt(build, 2).toString(36) | |
} | |
uriCompressor.prototype.buildDecompress = function(buildStr) { | |
return parseInt(build, 36).toString(2) | |
} | |
// @param Array<int> | |
uriCompressor.prototype.orderCompress = function(order) { | |
var res = [] | |
for(key in order) { | |
if(order.hasOwnProperty(key)) | |
res.push(this.convArray[order[key]]) | |
} | |
return res.join('') | |
} | |
// @param string | |
uriCompressor.prototype.orderDecompress = function(orderStr) { | |
var order = orderStr.split(''), res = [] | |
for(key in order) { | |
if(order.hasOwnProperty(key)) | |
res.push(this.convArray.indexOf(order[key])) | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment