Skip to content

Commit a08784e

Browse files
authored
Initial Commit
0 parents  commit a08784e

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# <b>js-queryparams</b>
2+
3+
### A JavaScript library to retrieve the query parameters with cross browser compatibility.
4+
5+
---
6+
7+
## How to Install:
8+
Inside your project, run the following command:
9+
```
10+
npm i js-queryparams
11+
```
12+
The above command will install the js-queryparams module inside the node_modules folder. After this you can either direct reference to the `lib/index.js` file or extract it and host it in your webserver.
13+
14+
---
15+
16+
## Usage:
17+
<br/> Assume the current browser url is:
18+
https://www.example.com/?abc=123&xyz=somevalue&abc=45
19+
<br/>
20+
### The library has the following functions:
21+
<br/>
22+
<b>Get a specific query parameter:</b>
23+
24+
```
25+
queryParams.get(<paramName>);
26+
27+
e.g.:
28+
queryParams.get("abc"); --> ["123", "45"] i.e. an Array of values
29+
queryParams.get("xyz"); --> "somevalue" i.e. a single value
30+
```
31+
32+
<br/>
33+
<b>Get all the query parameters:</b>
34+
35+
```
36+
queryParams.getAll() --> {abc: Array(2), xyz: Array(1)}
37+
```
38+
39+
<br/>
40+
<b>Change the reference "queryParams":</b>
41+
42+
```
43+
queryParams.changeRef(<newRefName>);
44+
45+
e.g.:
46+
queryParams.changeRef("$qp");
47+
console.log(queryParams); --> ReferenceError
48+
$qp.get("abc"); --> ["123", "45"]
49+
```
50+
Once the reference is changed, the old reference is deleted so trying to use it will result in a ReferenceError.
51+
52+
---
53+
54+
## License: MIT (https://mit-license.kcak11.com)

lib/index.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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") && (window[refName] = ((function () {
15+
var paramsMap;
16+
17+
/**
18+
* constructor: QueryParams
19+
* constructs the base map for the query parameters
20+
*/
21+
var QueryParams = function () {
22+
paramsMap
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+
var keyValue = paramsList[i].split("=");
28+
paramsMap = paramsMap || {};
29+
paramsMap[keyValue[0]] = paramsMap[keyValue[0]] || [];
30+
paramsMap[keyValue[0]].push(keyValue[1]);
31+
}
32+
}
33+
};
34+
35+
/**
36+
* Retrieves a specific query parameter.
37+
* If the parameter is a single value, the value is returned else an Array is returned.
38+
*
39+
* The returned value is Immutable i.e. updating the returned values does not affect the actual params in any way.
40+
* @param {*} paramName
41+
*/
42+
QueryParams.prototype.get = function (paramName) {
43+
if (paramsMap && paramsMap[paramName]) {
44+
if (paramsMap[paramName].length === 1) {
45+
return JSON.parse(JSON.stringify(paramsMap[paramName][0]));
46+
} else {
47+
return JSON.parse(JSON.stringify(paramsMap[paramName]));
48+
}
49+
}
50+
return null;
51+
};
52+
53+
/**
54+
* Retrieve all the parameters.
55+
*
56+
* The returned value is Immutable i.e. updating the returned values does not affect the actual params in any way.
57+
*/
58+
QueryParams.prototype.getAll = function () {
59+
return JSON.parse(JSON.stringify(paramsMap));
60+
};
61+
62+
/**
63+
* Use a different name for referencing instead of the default "queryParams".
64+
* After this call i.e. queryParams.changeRef("xyz"), the new reference will be "xyz" i.e.
65+
* All the subsequent calls should be made using xyz e.g.:
66+
* xyz.get("...")
67+
* xyz.getAll()
68+
*
69+
* @param {*} name
70+
*/
71+
QueryParams.prototype.changeRef = function (name) {
72+
if (!window[name]) {
73+
window[name] = window[refName];
74+
delete window[refName];
75+
refName = name;
76+
} else {
77+
throw new Error(name + " is already used in window");
78+
}
79+
};
80+
81+
/**
82+
* Globally exposed singleton instance.
83+
*
84+
* use via "queryParams" in the window object.
85+
* To change the reference, use the queryParams.changeRef("...") function
86+
*/
87+
return new QueryParams();
88+
})()));
89+
})());

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": "1.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)