Skip to content

Instantly share code, notes, and snippets.

@greystate
Created October 10, 2011 09:50
Show Gist options
  • Save greystate/1274961 to your computer and use it in GitHub Desktop.
Save greystate/1274961 to your computer and use it in GitHub Desktop.
CoffeeScript QueryString class
# Provide easy access to QueryString data
class QueryString
constructor: (@queryString) ->
@queryString or= window.document.location.search?.substr 1
@variables = @queryString.split '&'
@pairs = ([key, value] = pair.split '=' for pair in @variables)
get: (name) ->
for [key, value] in @pairs
return value if key is name
@greystate
Copy link
Author

Note: This was used for a simple "allow me to try these different config-values from the QueryString", so doesn't decode URI-encoded stuff.

@Offroadcode
Copy link

Pros/cons of caching in the constructor. I'm a big fan of only doing work if its needed so I would lazy load the hash when first accessed via the get method (as you might include this code in a library but not actually use it?). But if you know you will be using it then by all means do it in the constructor. QS is never going to be that big so this should be blazingly fast anyway. As usual don't get too hung up on optimising, some stuff I just do "as the norm" but its rare you need to go much beyond getting it just working, just me being picky :)

@MrJoy
Copy link

MrJoy commented Jul 29, 2013

All you need to do to address decoding is run both key and value through decodeURIComponent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment