17337: Added more tests, fixed whitespace issue
[arvados-workbench2.git] / src / common / url.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export function getUrlParameter(search: string, name: string) {
6     const safeName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
7     const regex = new RegExp('[\\?&]' + safeName + '=([^&#]*)');
8     const results = regex.exec(search);
9     return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
10 }
11
12 export function normalizeURLPath(url: string) {
13     const u = new URL(url);
14     u.pathname = u.pathname.replace(/\/\//, '/');
15     if (u.pathname[u.pathname.length - 1] === '/') {
16         u.pathname = u.pathname.substr(0, u.pathname.length - 1);
17     }
18     return u.toString();
19 }
20
21 export const escapeHashIfRequired = (name: string, defaultTransformation?: Function) =>
22     name.indexOf('#') > -1 ?
23         encodeURIComponent(name) :
24         defaultTransformation ?
25             defaultTransformation(name) :
26             name;