Skip to content

Instantly share code, notes, and snippets.

@grantjbutler
Forked from 140bytes/LICENSE.txt
Created May 23, 2011 16:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantjbutler/987036 to your computer and use it in GitHub Desktop.
Save grantjbutler/987036 to your computer and use it in GitHub Desktop.
Query string parsing
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];}
@jed
Copy link

jed commented May 23, 2011

still some room left in there!

for(a=a.replace(/^\?/,''),b={},c=a.split('&'),d=0;...

@grantjbutler
Copy link
Author

And the combine that with the c assignment, and that wipes out 10 bytes. Good find!

@jed
Copy link

jed commented May 23, 2011

i'm also pretty sure this whole thing would be shorter if you just used one regexp to capture the keys/values.

@grantjbutler
Copy link
Author

Yeah, you're right again. Updated to use that instead.

@jed
Copy link

jed commented May 23, 2011

i don't think this code will work. you're not looping on any condition!

@grantjbutler
Copy link
Author

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.

@jed
Copy link

jed commented May 24, 2011

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.

@grantjbutler
Copy link
Author

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.

@atesgoral
Copy link

Looks great so far!

  1. The RegExp probably doesn't need the i flag.
  2. Where's the URL-decoding?

@grantjbutler
Copy link
Author

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.

@atesgoral

  1. You're probably right. I'll get rid of it.
  2. For now, it's up to the programmer, but I'll see if I can add it. I should have enough space.

-- EDIT --

  1. Removed i flag from RegExp.
  2. Added URL decoding to the original function.

@atesgoral
Copy link

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.

@grantjbutler
Copy link
Author

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.

@atesgoral
Copy link

Hmmm. I guess you could squeeze in a:

a=a.replace(/\+/g,' ')

To avoid doing this replacement in each iteration...

@grantjbutler
Copy link
Author

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.

@atk
Copy link

atk commented Jun 29, 2011

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.

@defims
Copy link

defims commented Jun 13, 2016

failed when test "?a=1&b=2#c"

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