Created
October 10, 2011 09:50
-
-
Save greystate/1274961 to your computer and use it in GitHub Desktop.
CoffeeScript QueryString 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
# 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 |
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
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 :)