Skip to content

Commit 90ea499

Browse files
author
K.C.Ashish Kumar
committed
Release 3.0.0
0 parents  commit 90ea499

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
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
22+
```
23+
<br/>
24+
### The library has the following functions:
25+
<br/>
26+
<b>Get a specific query parameter:</b>
27+
28+
```
29+
queryParams.get(<paramName>);
30+
31+
e.g.:
32+
queryParams.get("p1"); // --> "v1" i.e. a single value.
33+
queryParams.get("p4"); // --> ["v4", "a4", ""] i.e. an Array of values, if the parameter gets repeated in the query string.
34+
```
35+
36+
<br/>
37+
<b>Get all the query parameters:</b>
38+
39+
```
40+
queryParams.getAll()
41+
42+
// Output:
43+
44+
{
45+
"p1": "v1",
46+
"p2": "some=text",
47+
"p3": "{\"key1\":\"val1\",\"key2\":[1,2,3,4]}",
48+
"p4": [
49+
"v4",
50+
"a4",
51+
""
52+
],
53+
"p5": "https://www.example.com",
54+
"p6": "https://www.example.com/?abc=def&pqr=xyz",
55+
"p7": "test=01",
56+
"p8": "",
57+
"p9": "v9"
58+
}
59+
```
60+
61+
<br/>
62+
<b>Change the reference "queryParams":</b>
63+
64+
```
65+
queryParams.changeRef(<newRefName>);
66+
67+
e.g.:
68+
queryParams.changeRef("$qp");
69+
console.log(queryParams); --> ReferenceError
70+
$qp.get("p1"); --> ["v1"]
71+
```
72+
Once the reference is changed, the old reference is deleted, so trying to use it will result in a ReferenceError.
73+
74+
---
75+
<br/>
76+
77+
## License: MIT (https://mit-license.kcak11.com)

lib/index.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
var params = window.location.search && window.location.search.substr(1);
24+
if (params) {
25+
var paramsList = params.split("&");
26+
for (var i = 0; i < paramsList.length; i++) {
27+
try {
28+
var keyValue = paramsList[i].split("=");
29+
var key = decodeURIComponent(keyValue[0]);
30+
var value = decodeURIComponent(keyValue.slice(1).join("="));
31+
paramsMap = paramsMap || {};
32+
paramsMap[key] = paramsMap[key] || [];
33+
paramsMap[key].push(value);
34+
} catch (exjs) {
35+
console.log("Error parsing entry:", paramsList[i]);
36+
}
37+
}
38+
}
39+
};
40+
41+
var valueParser = function (obj) {
42+
return JSON.stringify(obj, function (k, v) {
43+
if (v instanceof Array) {
44+
if (v.length === 1) {
45+
return v[0];
46+
}
47+
return v;
48+
}
49+
return v;
50+
});
51+
};
52+
53+
/**
54+
* Retrieves a specific query parameter.
55+
* If the parameter is a single value, the value is returned else an Array is returned.
56+
*
57+
* The returned value is Immutable i.e. updating the returned values does not affect the actual params in any way.
58+
* @param paramName
59+
*/
60+
QueryParams.prototype.get = function (paramName) {
61+
if (paramsMap && paramsMap[paramName]) {
62+
return JSON.parse(valueParser(paramsMap[paramName]));
63+
}
64+
return null;
65+
};
66+
67+
/**
68+
* Retrieve all the parameters.
69+
*
70+
* The returned value is Immutable i.e. updating the returned values does not affect the actual params in any way.
71+
*/
72+
QueryParams.prototype.getAll = function () {
73+
return JSON.parse(valueParser(paramsMap || null));
74+
};
75+
76+
/**
77+
* Use a different name for referencing instead of the default "queryParams".
78+
* After this call i.e. queryParams.changeRef("xyz"), the new reference will be "xyz" i.e.
79+
* All the subsequent calls should be made using xyz e.g.:
80+
* xyz.get("...")
81+
* xyz.getAll()
82+
*
83+
* @param name
84+
*/
85+
QueryParams.prototype.changeRef = function (name) {
86+
if (!window[name]) {
87+
window[name] = window[refName];
88+
delete window[refName];
89+
refName = name;
90+
} else {
91+
throw new Error(name + " is already used in window");
92+
}
93+
};
94+
95+
/**
96+
* Globally exposed singleton instance.
97+
*
98+
* use via "queryParams" in the window object.
99+
* To change the reference, use the queryParams.changeRef("...") function
100+
*/
101+
return new QueryParams();
102+
})());
103+
})();

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": "3.0.0",
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)