UNPKG

4.29 kBJavaScriptView Raw
1
2/**
3 * @url https://bunnyjs.com
4 * @package BunnyJS
5 * @component BunnyURL
6 * @author Mev-Rael (mevrael@gmail.com)
7 */
8export const BunnyURL = {
9
10 /**
11 * Get the value of a URI query HTTP GET param (?param=val) from current URL or string
12 * If param has [] in name, returns Array
13 * If param not set returns undefined
14 * If param exists but has no value returns empty string ''
15 *
16 * @param {String} get_param The field to get the value of
17 * @param {String?} url The URL to get the value from, default current URL
18 * @return {String|Array|undefined} The GET param value or undefined
19 */
20 getParam(get_param, url = window.location.href) {
21 const params = this.getParams(url);
22 if (params === undefined) {
23 return undefined;
24 }
25 return params[get_param];
26 },
27
28
29
30 /**
31 * Get URL query string (after ?) or false if there are no query string
32 * @param {String?} url
33 * @returns {String|undefined}
34 */
35 getQueryString(url = window.location.href) {
36 const pos = url.indexOf('?');
37 let hashPos = url.indexOf('#');
38 if (hashPos > -1) {
39 return pos > -1 ? decodeURI(url.slice(pos + 1, hashPos)) : undefined;
40 }
41 return pos > -1 ? decodeURI(url.slice(pos + 1)) : undefined;
42 },
43
44
45
46 getHash(url = window.location.href) {
47 const pos = url.indexOf('#');
48 return pos > -1 ? decodeURI(url.slice(pos + 1)) : undefined;
49 },
50
51
52
53 getURLWithoutQueryString(url = window.location.href) {
54 const pos = url.indexOf('?');
55 return pos > -1 ? decodeURI(url.slice(0, pos)) : url;
56 },
57
58
59
60 /**
61 * Get URL params as object (name => value) or undefined if there are no query string
62 * @param {String?} url
63 * @returns {Object|undefined}
64 */
65 getParams(url = window.location.href) {
66 const query = this.getQueryString(url);
67 if (query === undefined) {
68 return undefined;
69 }
70 const params = {};
71 const hashes = query.split('&');
72 for(let i = 0; i < hashes.length; i++) {
73 let hash = hashes[i].split('=');
74 if (hash[0].indexOf('[]') !== -1) {
75 // is array
76 if (params[hash[0]] === undefined) {
77 params[hash[0]] = [];
78 }
79 params[hash[0]].push(hash[1]);
80 } else {
81 params[hash[0]] = hash[1];
82 }
83 }
84 return params;
85 },
86
87
88 /**
89 * Check if GET param is in URL even if it has no value
90 *
91 * @param {String} param
92 * @param {String?} url
93 * @returns {Boolean}
94 */
95 hasParam(param, url = window.location.href) {
96 return this.getParam(param, url) !== undefined;
97 },
98
99 setParam(param, value, url = window.location.href) {
100 let params = this.getParams(url);
101 if (params === undefined) {
102 params = {};
103 }
104 params[param] = value;
105 return this.replaceParams(params, url);
106 },
107
108 generateQueryString(params) {
109 let i = 0;
110 let queryStr = '';
111 for(let k in params) {
112 queryStr += i === 0 ? '?' : '&';
113 queryStr += k + '=' + params[k];
114 i++;
115 }
116 return queryStr;
117 },
118
119 replaceParams(params, url = window.location.href) {
120 let newUrl = this.getURLWithoutQueryString(url) + this.generateQueryString(params);
121 const hash = this.getHash(url);
122 if (hash !== undefined) {
123 newUrl += '#' + hash;
124 }
125 return newUrl;
126 },
127
128 setParams(params, url = window.location.href) {
129 const urlParams = this.getParams(url);
130 for (let k in params) {
131 urlParams[k] = params[k];
132 }
133 return this.replaceParams(urlParams, url);
134 },
135
136 removeParam(param, url = window.location.href) {
137 const params = this.getParams(url);
138 if (params[param] !== undefined) {
139 delete params[param];
140 }
141 return this.replaceParams(params, url);
142 }
143
144};