"Fossies" - the Fresh Open Source Software Archive 
Member "jitsi-meet-7316/react/features/base/util/parseURLParams.ts" (5 Jun 2023, 2066 Bytes) of package /linux/misc/jitsi-meet-7316.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "parseURLParams.ts":
jitsi-meet_8319_vs_jitsi-meet_8615.
1 // @ts-expect-error
2 import Bourne from '@hapi/bourne';
3
4 import { reportError } from './helpers';
5
6 /**
7 * A list if keys to ignore when parsing.
8 *
9 * @type {string[]}
10 */
11 const blacklist = [ '__proto__', 'constructor', 'prototype' ];
12
13 /**
14 * Parses the query/search or fragment/hash parameters out of a specific URL and
15 * returns them as a JS object.
16 *
17 * @param {URL} url - The URL to parse.
18 * @param {boolean} dontParse - If falsy, some transformations (for parsing the
19 * value as JSON) will be executed.
20 * @param {string} source - If {@code 'search'}, the parameters will parsed out
21 * of {@code url.search}; otherwise, out of {@code url.hash}.
22 * @returns {Object}
23 */
24 export function parseURLParams(
25 url: URL | string,
26 dontParse = false,
27 source = 'hash') {
28 if (typeof url === 'string') {
29 // eslint-disable-next-line no-param-reassign
30 url = new URL(url);
31 }
32 const paramStr = source === 'search' ? url.search : url.hash;
33 const params: any = {};
34 const paramParts = paramStr?.substr(1).split('&') || [];
35
36 // Detect and ignore hash params for hash routers.
37 if (source === 'hash' && paramParts.length === 1) {
38 const firstParam = paramParts[0];
39
40 if (firstParam.startsWith('/') && firstParam.split('&').length === 1) {
41 return params;
42 }
43 }
44
45 paramParts.forEach((part: string) => {
46 const param = part.split('=');
47 const key = param[0];
48
49 if (!key || key.split('.').some((k: string) => blacklist.includes(k))) {
50 return;
51 }
52
53 let value;
54
55 try {
56 value = param[1];
57
58 if (!dontParse) {
59 const decoded = decodeURIComponent(value).replace(/\\&/, '&');
60
61 value = decoded === 'undefined' ? undefined : Bourne.parse(decoded);
62 }
63 } catch (e: any) {
64 reportError(
65 e, `Failed to parse URL parameter value: ${String(value)}`);
66
67 return;
68 }
69 params[key] = value;
70 });
71
72 return params;
73 }