-
-
Save grantjbutler/987036 to your computer and use it in GitHub Desktop.
| function( | |
| a, // the query string | |
| b, // placeholder | |
| c, // placeholder | |
| d, // placeholder | |
| e // placeholder | |
| ) { | |
| for( | |
| b = /[?&]?([^=]+)=([^&]*)/g, // Create the regular expression to match key-value pairs. | |
| c = {}, // Create the object to store all the key-value pairs. | |
| e = decodeURIComponent; // Alias the decodeURIComponent function to save bytes. | |
| d = b.exec(a.replace(/\+/g, ' ')); | |
| c[e(d[1])] = e(d[2]) // store in the object. | |
| ); | |
| return c; // return the object with the keys and pairs. | |
| } |
| function(a,b,c,d,e){for(b=/[?&]?([^=]+)=([^&]*)/g,c={},e=decodeURIComponent;d=b.exec(a.replace(/\+/g,' '));c[e(d[1])]=e(d[2]));return c;} |
| DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| Version 2, December 2004 | |
| Copyright (C) 2011 Grant Butler | |
| Everyone is permitted to copy and distribute verbatim or modified | |
| copies of this license document, and changing it is allowed as long | |
| as the name is changed. | |
| DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
| 0. You just DO WHAT THE FUCK YOU WANT TO. |
| { | |
| "name": "query_string", | |
| "description": "Parses a query string and returns key-pairs as in object.", | |
| "keywords": [ | |
| "query string", | |
| "query", | |
| "url" | |
| ] | |
| } |
| function e( | |
| a, // The name of the key that we want the value for. | |
| b, // Placeholder | |
| c // Placeholder | |
| ) { | |
| if(e.a == []._) // Check if we e.a, where we store the object, exists. | |
| for( // If not, we go into this for loop to create it. | |
| b = /[?&]?([^=]+)=([^&]*)/g, // Setup the RegEx. | |
| e.a = {}; // Initialize e.a. | |
| c = b.exec(location.search); // Execute the RegEx. | |
| e.a[c[1]]=c[2] // Assign the pair. | |
| ); | |
| return e.a[a]; // Return the value for the provided key. | |
| } |
| function e(a,b,c){if(e.a==[]._)for(b=/\??([^=]+)=([^&]+)&?/g,e.a={};c=b.exec(location.search);e.a[c[1]]=c[2]);return e.a[a];} |
Alright, I've got that taken care of, but that puts it at 137 bytes. I don't think there's much more I can do to it. Also, I went and used your regular expression, atesgoral, as it looked easier to read and didn't take up any more bytes.
Hmmm. I guess you could squeeze in a:
a=a.replace(/\+/g,' ')
To avoid doing this replacement in each iteration...
I tried that, but it puts me at 141 bytes, which is just over the limit for 140bytes. That's why I ended up doing what I did. It doesn't affect the results, but probably doesn't perform as well as it could.
You could put the [?&] inside the expression part for the parameter key: instead of /[?&]?([^=]+)=([^&]*)/g, you could use /([^=?&]+)=([^&]*)/g, since those characters are not allowed unencoded within those keys anyway.
failed when test "?a=1&b=2#c"
You've still got some space to handle the
+->(space) unescaping (decodeURIComponentdoesn't do that for you). I already have a gist that I had created a while ago and I was planning to minify that. But yours seems to be pretty small already.