|
| 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 | +})(); |
0 commit comments