Skip to content

Commit c84169a

Browse files
committed
initial checkin: compress and uncompressed JS, manifest, readme, and gitignore
0 parents  commit c84169a

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.DS_Store

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
======================================
2+
jQuery Plugin - Query Parameter Parser
3+
======================================
4+
5+
This is a plugin that adds two functions to the jQuery global object for parsing query parameters. It will automatically decode URI encoded values and restore any spaces that may have been replaced with the plus sign.
6+
7+
You can see the plugin in action at::
8+
9+
http://jsfiddle.net/mattsnider/spHAD/
10+
11+
$.parseQuery(s)
12+
=====================
13+
14+
This function accepts a query string and returns a JavaScript object on which the query string key/value pairs are applied. So passing the following query string::
15+
16+
$.parseQuery('?&key1=value1&key2=value2');
17+
18+
Will return the object::
19+
20+
{
21+
key1: 'value1',
22+
key2: 'value2'
23+
}
24+
25+
26+
$.getQuery()
27+
==================
28+
29+
This is a shortcut method that passing `window.location.search` to `$.parseQuery` and cached the result. So calling `$.getQuery` on the url `http://www.myserver.com/?&foo=bar&foofoo=baz` will return::
30+
31+
Will return the object::
32+
33+
{
34+
foo: 'bar',
35+
foofoo: 'baz'
36+
}
37+
38+
Improperly Formatted Queries
39+
============================
40+
41+
The regex is robust enough handle lots of improperly formatted strings.
42+
43+
Good values, wrapped with crap
44+
------------------------------
45+
46+
The regex can find properly formatted query parameters anyplace in a string, even if proceeded and succeed with crap::
47+
48+
$.parseQuery('?jjsdlgalgdja&foo=bar&adfasdfasdfasdfa');
49+
50+
Will return the object::
51+
52+
{
53+
foo: 'bar'
54+
}
55+
56+
Doesn't start with `?` or `&`
57+
-----------------------------
58+
59+
The function checks if the first letter in the query string is `?`, removing it only as necessary, so the following three strings behave identically::
60+
61+
$.parseQuery('?&foo=bar');
62+
$.parseQuery('&foo=bar');
63+
$.parseQuery('foo=bar');
64+
65+
Returning the object::
66+
67+
{
68+
foo: 'bar'
69+
}
70+
71+
Keys without values are dropped
72+
-------------------------------
73+
74+
If you have a key without a value, it is not written to the object::
75+
76+
$.parseQuery('?&foo=bar&baz');
77+
78+
Will return the object::
79+
80+
{
81+
foo: 'bar'
82+
}
83+
84+
You can change this behavior by providing the *tolerant* option, so empty keys will be set to empty string (`''`::
85+
86+
$.parseQuery('?&foo=bar&baz', {tolerant: 1});
87+
88+
Will return the object::
89+
90+
{
91+
baz: '',
92+
foo: 'bar'
93+
}
94+
95+
License
96+
=======
97+
98+
http://opensource.org/licenses/MIT

jquery-queryParser.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(function ($) {
2+
// encapsulate variables that need only be defined once
3+
var pl = /\+/g, // Regex for replacing addition symbol with a space
4+
searchStrict = /([^&=]+)=+([^&]*)/g,
5+
searchTolerant = /([^&=]+)=?([^&]*)/g,
6+
decode = function (s) {
7+
return decodeURIComponent(s.replace(pl, " "));
8+
};
9+
10+
// parses a query string. by default, will only match good k/v pairs.
11+
// if the tolerant option is truthy, then it will also set keys without values to ''
12+
$.parseQuery = function(query, options) {
13+
var match,
14+
o = {},
15+
opts = options || {},
16+
search = opts.tolerant ? searchTolerant : searchStrict;
17+
18+
if ('?' === query.substring(0, 1)) {
19+
query = query.substring(1);
20+
}
21+
22+
// each match is a query parameter, add them to the object
23+
while (match = search.exec(query)) {
24+
o[decode(match[1])] = decode(match[2]);
25+
}
26+
27+
return o;
28+
}
29+
30+
// parse this URLs query string
31+
$.getQuery = function(options) {
32+
return $.parseQuery(window.location.search, options);
33+
}
34+
35+
$.fn.parseQuery = function (options) {
36+
return $.parseQuery($(this).serialize(), options);
37+
};
38+
}(jQuery));

jquery-queryParser.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

queryParser.jquery.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "queryParser",
3+
"title": "jQuery Query Parameter Parser",
4+
"description": "jQuery plugin for adding query parameter parsing functions.",
5+
"keywords": [
6+
"query",
7+
"parameter",
8+
"window.location.search",
9+
"parser"
10+
],
11+
"version": "1.0.0",
12+
"author": {
13+
"name": "Matt Snider",
14+
"url": "http://www.mattsnider.com"
15+
},
16+
"maintainers": [
17+
{
18+
"name": "Matt Snider",
19+
"email": "admin@mattsnider.com",
20+
"url": "http://www.mattsnider.com"
21+
}
22+
],
23+
"licenses": [
24+
{
25+
"type": "MIT",
26+
"url": "http://opensource.org/licenses/MIT"
27+
}
28+
],
29+
"bugs": "https://github.com/mattsnider/jquery-plugin-query-parser/issues",
30+
"homepage": "https://github.com/mattsnider/jquery-plugin-query-parser",
31+
"docs": "https://github.com/mattsnider/jquery-plugin-query-parser",
32+
"download": "https://github.com/mattsnider/",
33+
"dependencies": {
34+
"jquery": ">=1"
35+
}
36+
}

0 commit comments

Comments
 (0)