Skip to content

Instantly share code, notes, and snippets.

@grantjbutler
Forked from 140bytes/LICENSE.txt
Created May 23, 2011 16:51
Show Gist options
  • 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];}
@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