-
-
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];} |
And the combine that with the c assignment, and that wipes out 10 bytes. Good find!
i'm also pretty sure this whole thing would be shorter if you just used one regexp to capture the keys/values.
Yeah, you're right again. Updated to use that instead.
i don't think this code will work. you're not looping on any condition!
They way that RegExp.exec works is that it returns one match at a time, starting from the beginning of the string, and working its way to the end. When it reaches the end of the string, and there are no more matches, it returns null. It's like how mysql_fetch_array works in PHP. You put it in a while loop, and it returns the next row. This is same concept. Plus, I would not have modified the gist until I was certain that the code worked. When I get a chance later, I'll write up a test file that you can test with.
ah, right! i haven't used exec
like that in ages. thanks for the refresher! this is a great way to save space, would love to see you document it on the wiki if you have a chance.
Updated to remove the beginning question mark when passing window.location.search, as well as modified it to put the assignment into the 3rd part of the for loop. So instead of increasing the length by 3 bytes, I only increase it by 1.
Looks great so far!
- The RegExp probably doesn't need the
i
flag. - Where's the URL-decoding?
Added a version for specific use with window.location.search. Just pass the key you want, and it returns the value. That version will probably not have URL decoding, though, cause it's close to the 140 byte limit.
- You're probably right. I'll get rid of it.
- For now, it's up to the programmer, but I'll see if I can add it. I should have enough space.
-- EDIT --
- Removed
i
flag from RegExp. - Added URL decoding to the original function.
You've still got some space to handle the +
->
(space) unescaping (decodeURIComponent
doesn'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.
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"
still some room left in there!