@naturalcycles/js-lib 14.270.1 → 14.271.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -13,3 +13,10 @@ import type { StringMap } from '../types';
|
|
|
13
13
|
* Goal of this function is to produce exactly same output as URLSearchParams would.
|
|
14
14
|
*/
|
|
15
15
|
export declare function _parseQueryString(search: string): StringMap;
|
|
16
|
+
/**
|
|
17
|
+
* A wrapper around `new URL(href)`, but it returns `null` instead of throwing an error.
|
|
18
|
+
* While `URL.parse` exists, and behaves similarly, it's not widely supported.
|
|
19
|
+
*
|
|
20
|
+
* `null` was chosen instead of `undefined` in the return type union to make it easier to move to `URL.parse` if it ever becomes widely supported.
|
|
21
|
+
*/
|
|
22
|
+
export declare function _toUrlOrNull(url: string | undefined, base?: string): URL | null;
|
package/dist/string/url.util.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._parseQueryString = _parseQueryString;
|
|
4
|
+
exports._toUrlOrNull = _toUrlOrNull;
|
|
4
5
|
/**
|
|
5
6
|
* Parses `location.search` string (e.g `?a=1&b=2`) into a StringMap, e.g:
|
|
6
7
|
* `{ a: '1', b: '2' }`
|
|
@@ -27,3 +28,19 @@ function _parseQueryString(search) {
|
|
|
27
28
|
});
|
|
28
29
|
return qs;
|
|
29
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* A wrapper around `new URL(href)`, but it returns `null` instead of throwing an error.
|
|
33
|
+
* While `URL.parse` exists, and behaves similarly, it's not widely supported.
|
|
34
|
+
*
|
|
35
|
+
* `null` was chosen instead of `undefined` in the return type union to make it easier to move to `URL.parse` if it ever becomes widely supported.
|
|
36
|
+
*/
|
|
37
|
+
function _toUrlOrNull(url, base) {
|
|
38
|
+
if (typeof url !== 'string')
|
|
39
|
+
return null;
|
|
40
|
+
try {
|
|
41
|
+
return new URL(url, base || undefined);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -24,3 +24,19 @@ export function _parseQueryString(search) {
|
|
|
24
24
|
});
|
|
25
25
|
return qs;
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* A wrapper around `new URL(href)`, but it returns `null` instead of throwing an error.
|
|
29
|
+
* While `URL.parse` exists, and behaves similarly, it's not widely supported.
|
|
30
|
+
*
|
|
31
|
+
* `null` was chosen instead of `undefined` in the return type union to make it easier to move to `URL.parse` if it ever becomes widely supported.
|
|
32
|
+
*/
|
|
33
|
+
export function _toUrlOrNull(url, base) {
|
|
34
|
+
if (typeof url !== 'string')
|
|
35
|
+
return null;
|
|
36
|
+
try {
|
|
37
|
+
return new URL(url, base || undefined);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
package/package.json
CHANGED
package/src/string/url.util.ts
CHANGED
|
@@ -25,3 +25,19 @@ export function _parseQueryString(search: string): StringMap {
|
|
|
25
25
|
})
|
|
26
26
|
return qs
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A wrapper around `new URL(href)`, but it returns `null` instead of throwing an error.
|
|
31
|
+
* While `URL.parse` exists, and behaves similarly, it's not widely supported.
|
|
32
|
+
*
|
|
33
|
+
* `null` was chosen instead of `undefined` in the return type union to make it easier to move to `URL.parse` if it ever becomes widely supported.
|
|
34
|
+
*/
|
|
35
|
+
export function _toUrlOrNull(url: string | undefined, base?: string): URL | null {
|
|
36
|
+
if (typeof url !== 'string') return null
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
return new URL(url, base || undefined)
|
|
40
|
+
} catch {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
}
|