Created
February 17, 2016 15:08
-
-
Save bland-industries/6908e1aedc3aaa25bf7e to your computer and use it in GitHub Desktop.
PowerURL: my own simple incomplete js object to handle url manipulation. I know it needs some work.
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 PowerURL(url) { | |
var powerURL = this; | |
powerURL.protocol = ""; | |
powerURL.site = ""; | |
powerURL.path = []; | |
powerURL.get = {}; | |
if(typeof url === 'string') { | |
powerURL.parse(url); | |
} | |
} | |
PowerURL.prototype.parse = function(url) { | |
var powerURL = this; | |
urlArray = url.split("://"); | |
if (urlArray.length === 2) { | |
powerURL.protocol = urlArray[0]; | |
url = urlArray[1]; | |
} else { | |
powerURL.protocol = 'http'; | |
url = urlArray[0]; | |
} | |
urlArray = url.split('?'); | |
url = urlArray[0]; | |
powerURL.path = url.split("/"); | |
powerURL.site = powerURL.path.shift(); | |
if (urlArray.length > 1) { | |
getString = urlArray[1]; | |
getArray = getString.split("&"); | |
for (var i = 0; i < getArray.length; i++) { | |
item = getArray[i].split("="); | |
powerURL.get[item[0]] = item[1]; | |
} | |
} | |
return powerURL; | |
} | |
PowerURL.prototype.add_get = function(key, value) { | |
this.get[key] = value; | |
return this; | |
} | |
PowerURL.prototype.remove_get = function(key) { | |
delete this.get[key] | |
return this; | |
} | |
PowerURL.prototype.set_site = function(value) { | |
this.site = value | |
return this; | |
} | |
PowerURL.prototype.set_path = function(value) { | |
this.path = value.split("/"); | |
return this; | |
} | |
PowerURL.prototype.set_protocol = function(value) { | |
this.protocol = value; | |
return this; | |
} | |
PowerURL.prototype.get_url = function () { | |
var powerURL = this; | |
newURL = powerURL.protocol; | |
newURL += "://"; | |
newURL += powerURL.site; | |
newURL += "/"; | |
newURL += powerURL.path.join("/"); | |
first = true; | |
for (var property in powerURL.get) { | |
if (powerURL.get.hasOwnProperty(property)) { | |
if (first) { | |
newURL += "?"; | |
first = false; | |
} else { | |
newURL += "&"; | |
} | |
newURL += property + "=" + powerURL.get[property]; | |
// do stuff | |
} | |
} | |
return newURL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment