Skip to content

Commit ffa6389

Browse files
author
K.C.Ashish Kumar
committed
Release 3.5.0
0 parents  commit ffa6389

File tree

4 files changed

+228
-0
lines changed

4 files changed

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

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.5.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)