Skip to content

Commit e0bfe72

Browse files
author
K.C.Ashish Kumar
committed
Release 4.0.1
0 parents  commit e0bfe72

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 NPX Binaries
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# <b>js-queryparams</b>
2+
3+
### <i>A JavaScript library to retrieve the query parameters with cross-browser support.</i>
4+
5+
---
6+
<br/>
7+
8+
## How to Install:
9+
Inside your project, run the following command:
10+
```
11+
npm i js-queryparams
12+
```
13+
The above command will install the js-queryparams module inside the node_modules folder. After this you can either directly refer to the `node_modules/js-queryparams/lib/index.js` file from within your webpage or extract it and host it in your webserver.
14+
15+
---
16+
<br/>
17+
18+
## Usage:
19+
Assume the current browser url is:
20+
21+
```
22+
https://www.example.com/?p1=v1&p2=some=text&p3={"key1":"val1","key2":[1,2,3,4]}&p4=v4&p4=a4&p4=&p5=https://www.example.com&p6=https%3A%2F%2Fwww.example.com%2F%3Fabc%3Ddef%26pqr%3Dxyz&p7=test=01&p8&p9=v9#somehash
23+
```
24+
<br/>
25+
26+
### The library has the following functions:
27+
<br/>
28+
<b>Get a specific query parameter:</b>
29+
<br/>
30+
<br/>
31+
32+
```
33+
queryParams.get(<paramName>);
34+
35+
e.g.:
36+
queryParams.get("p1"); // --> "v1" i.e. a single value.
37+
queryParams.get("p4"); // --> ["v4", "a4", ""] i.e. an Array of values, if the parameter gets repeated in the query string.
38+
```
39+
40+
<br/>
41+
<b>Get all the query parameters:</b>
42+
<br/>
43+
<br/>
44+
45+
```
46+
queryParams.getAll()
47+
48+
// Output:
49+
50+
{
51+
"p1": "v1",
52+
"p2": "some=text",
53+
"p3": "{\"key1\":\"val1\",\"key2\":[1,2,3,4]}",
54+
"p4": [
55+
"v4",
56+
"a4",
57+
""
58+
],
59+
"p5": "https://www.example.com",
60+
"p6": "https://www.example.com/?abc=def&pqr=xyz",
61+
"p7": "test=01",
62+
"p8": "",
63+
"p9": "v9"
64+
}
65+
```
66+
67+
<br/>
68+
<b>Support for custom url:</b>
69+
70+
The `queryParams.get` and `queryParams.getAll` functions also support an optional argument to specify a url.
71+
<br/>
72+
<br/>
73+
74+
```
75+
queryParams.getAll("https://www.example.com/?p1=v1&p2=v2")
76+
77+
// Output:
78+
79+
{
80+
"p1": "v1",
81+
"p2": "v2"
82+
}
83+
84+
queryParams.get("p2", "https://www.example.com/?p1=v1&p2=v2")
85+
86+
// Output:
87+
88+
"v2"
89+
```
90+
91+
<br/>
92+
<b>Change the reference "queryParams":</b>
93+
94+
In case the reference `queryParams` needs to be changed, then it can be done as follows:
95+
96+
```
97+
// queryParams.changeRef(<newRefName>);
98+
99+
// e.g.:
100+
queryParams.changeRef("$qp");
101+
console.log(queryParams); // --> ReferenceError
102+
103+
// use $qp instead of queryParams
104+
$qp.get("p1"); // --> "v1"
105+
```
106+
Once the reference is changed, the old reference is deleted, so trying to use it will result in a ReferenceError.
107+
108+
---
109+
<br/>
110+
111+
## License: MIT (https://mit-license.kcak11.com)

lib/index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* js-queryparams
3+
*
4+
* Author: K.C.Ashish Kumar
5+
* License: MIT
6+
*/
7+
8+
/**
9+
* This library facilitates retrieval of the query parameters across all the browsers.
10+
*/
11+
12+
(function () {
13+
var refName = "queryParams";
14+
typeof window !== "undefined" &&
15+
(window[refName] = (function () {
16+
var paramsMap;
17+
18+
/**
19+
* constructor: QueryParams
20+
* constructs the base map for the query parameters
21+
*/
22+
var QueryParams = function () { };
23+
24+
var buildParamsMap = function (url) {
25+
paramsMap = null;
26+
var searchStr = window.location.search;
27+
if (url) {
28+
var _t = document.createElement("a");
29+
_t.href = url;
30+
searchStr = _t.search;
31+
}
32+
var params = searchStr && searchStr.substr(1);
33+
if (params) {
34+
var paramsList = params.split("&");
35+
for (var i = 0; i < paramsList.length; i++) {
36+
try {
37+
var keyValue = paramsList[i].split("=");
38+
var key = decodeURIComponent(keyValue[0]);
39+
var value = decodeURIComponent(keyValue.slice(1).join("="));
40+
paramsMap = paramsMap || {};
41+
paramsMap[key] = paramsMap[key] || [];
42+
paramsMap[key].push(value);
43+
} catch (exjs) {
44+
console.log("Error parsing entry:", paramsList[i]);
45+
}
46+
}
47+
}
48+
};
49+
50+
var valueParser = function (obj) {
51+
return JSON.stringify(obj, function (k, v) {
52+
if (v instanceof Array) {
53+
if (v.length === 1) {
54+
return v[0];
55+
}
56+
return v;
57+
}
58+
return v;
59+
});
60+
};
61+
62+
/**
63+
* Retrieves a specific query parameter.
64+
* If the parameter is a single value, the value is returned else an Array is returned.
65+
*
66+
* The returned value is Immutable i.e. updating the returned values does not affect the actual params in any way.
67+
* @param paramName
68+
*/
69+
QueryParams.prototype.get = function (paramName, url) {
70+
buildParamsMap(url);
71+
if (paramsMap && paramsMap[paramName]) {
72+
return JSON.parse(valueParser(paramsMap[paramName]));
73+
}
74+
return null;
75+
};
76+
77+
/**
78+
* Retrieve all the parameters.
79+
*
80+
* The returned value is Immutable i.e. updating the returned values does not affect the actual params in any way.
81+
*/
82+
QueryParams.prototype.getAll = function (url) {
83+
buildParamsMap(url);
84+
return JSON.parse(valueParser(paramsMap || null));
85+
};
86+
87+
/**
88+
* Use a different name for referencing instead of the default "queryParams".
89+
* After this call i.e. queryParams.changeRef("xyz"), the new reference will be "xyz" i.e.
90+
* All the subsequent calls should be made using xyz e.g.:
91+
* xyz.get("...")
92+
* xyz.getAll()
93+
*
94+
* @param name
95+
*/
96+
QueryParams.prototype.changeRef = function (name) {
97+
if (!window[name]) {
98+
window[name] = window[refName];
99+
delete window[refName];
100+
refName = name;
101+
} else {
102+
throw new Error(name + " is already used in window");
103+
}
104+
};
105+
106+
/**
107+
* Globally exposed singleton instance.
108+
*
109+
* use via "queryParams" in the window object.
110+
* To change the reference, use the queryParams.changeRef("...") function
111+
*/
112+
return new QueryParams();
113+
})());
114+
})();

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "js-queryparams",
3+
"version": "4.0.1",
4+
"description": "A JavaScript library to retrieve the query parameters with cross browser compatibility.",
5+
"main": "index.js",
6+
"keywords": [
7+
"queryparams",
8+
"queryparameters",
9+
"js-queryparams",
10+
"query",
11+
"params",
12+
"parameters",
13+
"query parameters",
14+
"query params"
15+
],
16+
"author": "K.C.Ashish Kumar",
17+
"license": "MIT",
18+
"repository": {
19+
"url": "https://github.com/npx-bin/js-queryparams.git",
20+
"type": "GIT"
21+
}
22+
}

0 commit comments

Comments
 (0)