@naturalcycles/js-lib 14.270.0 → 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;
@@ -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
+ }
package/dist/types.d.ts CHANGED
@@ -196,6 +196,13 @@ export type IsoDateTime = Branded<string, 'IsoDateTime'>;
196
196
  * @example '2023-09'
197
197
  */
198
198
  export type MonthId = string;
199
+ /**
200
+ * Identifies IANA timezone name.
201
+ * Branded type.
202
+ *
203
+ * @example 'America/New_York'
204
+ */
205
+ export type IanaTimezone = Branded<string, 'IanaTimezone'>;
199
206
  /**
200
207
  * Branded UnixTimestamp in seconds.
201
208
  * Extends (compatible with) `number`.
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.270.0",
3
+ "version": "14.271.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build": "dev-lib build-esm-cjs",
@@ -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
+ }
package/src/types.ts CHANGED
@@ -254,6 +254,14 @@ export type IsoDateTime = Branded<string, 'IsoDateTime'>
254
254
  */
255
255
  export type MonthId = string
256
256
 
257
+ /**
258
+ * Identifies IANA timezone name.
259
+ * Branded type.
260
+ *
261
+ * @example 'America/New_York'
262
+ */
263
+ export type IanaTimezone = Branded<string, 'IanaTimezone'>
264
+
257
265
  /**
258
266
  * Branded UnixTimestamp in seconds.
259
267
  * Extends (compatible with) `number`.