Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | 1x 1x 1x 1x 4x 4x 4x 4x 3x 1x 1x 1x | import * as objectf from '../objectf/objectf'; /** * Whether two string urls have the same hostname. */ export function areSameHostNames(url1: string, url2: string) { const urlA = new URL(url1); const urlB = new URL(url2); return urlA.hostname === urlB.hostname; } /** * Checks if a given param is present in the url AND is set to 'true' * * ``` * urlParams.isTrue('sup'); * * // http://mydomain.com?sup=true --> true * // http://mydomain.com?hello&sup=true --> true * * // http://mydomain.com --> false * // http://mydomain.com?sup --> false * // http://mydomain.com?sup=false --> false * ``` */ export function isTrue(paramName: string): boolean { return getValue(paramName) === 'true'; } /** * Returns the value of a given url query param, or null if not present. * * ``` * urlParams.getValue('sup'); * * // http://mydomain.com/?sup=hello --> hello * // http://mydomain.com/?hello&sup=hello --> hello * * // http://mydomain.com --> null * // http://mydomain.com/?sup --> null * * * // For quick sanitization. * const myvalue = stringf.alphaNumeric(urlParams.getValue('sup')); * ``` */ export function getValue(paramName: string): string | null { const params = new URLSearchParams(window.location.search); const value = params.get(paramName); return value || null; } /** * Tests if the given param is in the url. * @param paramName */ export function hasParam(paramName: string): boolean { const urlParams = new URLSearchParams(window.location.search); return urlParams.has(paramName); } /** * Updates the current URL params with the provided value. * Uses history.replaceState. * * ``` * // http://mydomain.com/mypage/ * urlParams.addParamNow('name', 'Scott'); * // http://mydomain.com/mypage/?name=Scott * ``` */ export function addParamNow(paramName: string, value: string) { const url = new URL(window.location.href); url.searchParams.set(paramName, value); if (window.history.replaceState) { window.history.replaceState({}, '', url.toString()); } } /** * Updates the current URL params and removes a parameter. * Uses history.replaceState. * * ``` * // http://mydomain.com/mypage/?name=Scott * urlParams.removeParam('name'); * // http://mydomain.com/mypage/ * ``` */ export function removeParamNow(paramName: string) { const url = new URL(window.location.href); url.searchParams.delete(paramName); if (window.history.replaceState) { window.history.replaceState({}, '', url.toString()); } } /** * Allows you to loop through each url param. * * ``` * Given: mydomain.com?name=scott&location=mars * * const params = urlParams.asObject(window.location.search); * * console.log(params); // { name: "scott", location: "mars"} * * ``` * @param callback */ const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']); export function asObject(url: string): Record<string, string> { const query = url.startsWith('?') ? url.slice(1) : url.slice(1); const result: Record<string, string> = {}; query.split('&').forEach((section: string) => { const eqIndex = section.indexOf('='); const key = eqIndex === -1 ? section : section.slice(0, eqIndex); const rawValue = eqIndex === -1 ? '' : section.slice(eqIndex + 1); if (key === '' || FORBIDDEN_KEYS.has(key)) { return; } try { result[key] = decodeURIComponent(rawValue); } catch { result[key] = rawValue; } }); return result; } /** * Appends the current url params to the href values of a list * of anchor elements. * * * ``` * urlParams.appendUrlParamsToLinks( * Array.from(document.querySelectorAll('a')) * ) * * // Blacklist / Prevent specific url params from gettings carried over to links. * urlParams.appendUrlParamsToLinks( * Array.from(document.querySelectorAll('a')), * [ 'modal', 'email'] * ) * * // Whitelist specific url params. * urlParams.appendUrlParamsToLinks( * Array.from(document.querySelectorAll('a')), * null, * ['gtm'] * ) * ``` * @param elements */ export function appendUrlParamsToLinks( elements: Array<HTMLAnchorElement>, blackListKeys: Array<string> = [], whiteListKeys: Array<string> = [] ) { if ('URLSearchParams' in window) { const params = asObject(window.location.search); elements.forEach((el: HTMLAnchorElement) => { // Don't process for links with href. if (!el.href) { return; } const url = new URL(el.href); objectf.forEach(params, (key: string, value: string) => { if (!url.searchParams.has(key)) { if (whiteListKeys && whiteListKeys.length >= 1) { if (~whiteListKeys.indexOf(key)) { url.searchParams.append(key, value); } } else { if (blackListKeys && blackListKeys.length >= 1) { if (!~blackListKeys.indexOf(key)) { url.searchParams.append(key, value); } } else { url.searchParams.append(key, value); } } } }); el.href = url.toString(); }); } } /** * Adds a hisotry push state with hash. Use with removeUrlHistoryHash. * * ``` * // Update the URL hash with #test. * open() { * urlParams.addHistoryHash('modal', 'test'); * window.addEventListener('popstate', this.close.bind(this), { once: true}); * } * * // Removes the hash and pop state. * close() { * urlParams.removeHistoryHash('modal'); * } * * ``` */ export function addHistoryHash(name: string, value: string) { window.history.pushState({name: value}, '', '#' + value); } /** * Removes a previously set popstate url hash. */ export function removeHistoryHash(name: string | null = null) { window.history.pushState({name: name}, '', window.location.pathname); } /** * Extracts the hostname from a given url. * @param url */ export function getHostName(url: string) { const hostname = new URL(url).hostname; return hostname; } /** * Checks whether if the last page the user was on, was on this current site. */ export function lastPageWasSameHost() { return ( document.referrer && getHostName(document.referrer) === getHostName(window.location.href) ); } /** * Updates a particular URL param value. * This internally, updates window.location.search * value. * * ``` * // Immediately update the url search params. * window.location.search = * urlParams.updateUrlParam('myparam', 'true').toString(); * * // Update multiple * const params = new URLSearchParams(window.location.search); * params = urlParams.updateUrlParam('hello', 'true', params); * params = urlParams.updateUrlParam('name', 'scott', params); * params = urlParams.updateUrlParam('favFood', 'mikan', params); * // Update * window.location.search = params.toString(); * * ``` */ export function updateSearchParams( param: string, value: string, urlSearchParams?: URLSearchParams ): URLSearchParams { if (!urlSearchParams) { urlSearchParams = new URLSearchParams(window.location.search); } urlSearchParams.set(param, value); return urlSearchParams; } |