Created
August 15, 2011 20:43
-
-
Save dux/1147801 to your computer and use it in GitHub Desktop.
Simple coffee script Url class
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
class Url | |
constructor: (@path_and_qs=location.href) -> | |
[@path, @qs_data] = @path_and_qs.split '?',2 | |
@qs = {} | |
for elms in @qs_data.split '&' | |
el = elms.split '=', 2 | |
@qs[el[0]] = el[1] | |
build: -> | |
ret = new Array | |
for key,val of @qs | |
ret.push "#{key}=#{val}" | |
@path+'?'+ret.join '&' | |
url = new Url 'pero/lala?name=dux&age=jos' | |
url.qs['test'] = 123 | |
alert url.build() | |
// compiles to JS | |
var Url, url; | |
Url = (function() { | |
function Url(path_and_qs) { | |
var el, elms, _i, _len, _ref, _ref2; | |
this.path_and_qs = path_and_qs != null ? path_and_qs : location.href; | |
_ref = this.path_and_qs.split('?', 2), this.path = _ref[0], this.qs_data = _ref[1]; | |
this.qs = {}; | |
_ref2 = this.qs_data.split('&'); | |
for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | |
elms = _ref2[_i]; | |
el = elms.split('=', 2); | |
this.qs[el[0]] = el[1]; | |
} | |
} | |
Url.prototype.build = function() { | |
var key, ret, val, _ref; | |
ret = new Array; | |
_ref = this.qs; | |
for (key in _ref) { | |
val = _ref[key]; | |
ret.push("" + key + "=" + val); | |
} | |
return this.path + '?' + ret.join('&'); | |
}; | |
return Url; | |
})(); | |
url = new Url('pero/lala?name=dux&age=jos'); | |
url.qs['test'] = 123; | |
alert(url.build()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment