@koine/utils 1.0.74 → 1.0.77

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.
package/capitalize.d.ts CHANGED
@@ -4,5 +4,5 @@
4
4
  * @category text
5
5
  * @see https://stackoverflow.com/a/11409944/1938970
6
6
  */
7
- export declare function capitalize<T extends string>(text?: null | T): "" | Capitalize<T>;
7
+ export declare function capitalize<T extends string>(string: null | T): Capitalize<T>;
8
8
  export default capitalize;
package/capitalize.js CHANGED
@@ -4,9 +4,9 @@
4
4
  * @category text
5
5
  * @see https://stackoverflow.com/a/11409944/1938970
6
6
  */
7
- export function capitalize(text) {
8
- return text
9
- ? (text.charAt(0).toUpperCase() + text.slice(1))
10
- : "";
7
+ export function capitalize(string) {
8
+ var ensuredString = string || "";
9
+ return (ensuredString.charAt(0).toUpperCase() +
10
+ ensuredString.slice(1));
11
11
  }
12
12
  export default capitalize;
@@ -6,7 +6,7 @@
6
6
  * @category location
7
7
  *
8
8
  * @param {string} [raw] - The _raw_ query parameter
9
- * @param {number} [fallback=1] - Fallback value, `1` by default
9
+ * @param {number} [fallback] - Fallback number, we return `null` if not provided
10
10
  */
11
- export declare function getParamAsInt(raw?: string | string[], fallback?: number): number;
11
+ export declare function getParamAsInt<TFallback extends number | null | undefined>(raw?: string | string[], fallback?: TFallback): number | TFallback;
12
12
  export default getParamAsInt;
package/getParamAsInt.js CHANGED
@@ -7,10 +7,14 @@ import getParamAsString from "./getParamAsString";
7
7
  * @category location
8
8
  *
9
9
  * @param {string} [raw] - The _raw_ query parameter
10
- * @param {number} [fallback=1] - Fallback value, `1` by default
10
+ * @param {number} [fallback] - Fallback number, we return `null` if not provided
11
11
  */
12
12
  export function getParamAsInt(raw, fallback) {
13
- if (fallback === void 0) { fallback = 1; }
14
- return parseInt(getParamAsString(raw) || String(fallback), 10);
13
+ if (fallback === void 0) { fallback = null; }
14
+ var string = getParamAsString(raw);
15
+ if (string) {
16
+ return parseInt(string, 10);
17
+ }
18
+ return fallback;
15
19
  }
16
20
  export default getParamAsInt;
@@ -1,8 +1,8 @@
1
1
  import { type AnyQueryParams } from "./location";
2
2
  /**
3
3
  * It returns the "query params" as an object extracting it from the given `hash`
4
- *string or, if not provided, failling back reading the `location.hash`
5
-
4
+ * string or, if not provided, failling back reading the `location.hash`
5
+ *
6
6
  * @category location
7
7
  */
8
8
  export declare function getUrlHashParams<T extends NonNullable<AnyQueryParams>>(hash?: string): T;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * It returns the "query params" as an object extracting it from the given `hash`
3
- *string or, if not provided, failling back reading the `location.hash`
4
-
3
+ * string or, if not provided, failling back reading the `location.hash`
4
+ *
5
5
  * @category location
6
6
  */
7
7
  export function getUrlHashParams(hash) {
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * It returns the "pathname" cleaned up from the `#` and the initial slashes
3
- * extracting it from the given `hash` string or, if not provided, failling
3
+ * extracting it from the given `hash` string or, if not provided, failling
4
4
  * back reading the `location.hash`
5
5
  *
6
6
  * @category location
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * It returns the "pathname" cleaned up from the `#` and the initial slashes
3
- * extracting it from the given `hash` string or, if not provided, failling
3
+ * extracting it from the given `hash` string or, if not provided, failling
4
4
  * back reading the `location.hash`
5
5
  *
6
6
  * @category location
@@ -0,0 +1,16 @@
1
+ /**
2
+ * It returns a `Date` object from a date `string` adjusted on the user timeZone,
3
+ * if a timeZone is not provided we try getting it from the `Intl` browwser native
4
+ * API. It gracefully falls back returning a _non-timezone-based_ `Date`.
5
+ *
6
+ * @category date
7
+ *
8
+ * @resources
9
+ * - to get the timeZone client side see [this article](https://attacomsian.com/blog/javascript-current-timezone)
10
+ * - for converting the date based on the time zone [date-fns docs](https://date-fns.org/v2.27.0/docs/Time-Zones) and [date-fns-tz docs](https://github.com/marnusw/date-fns-tz)
11
+ *
12
+ * @param dateString A parseable date as string, `Z` is automatically suffixed if not present to correctly get time zone based time from a UTC date.
13
+ * @param timeZone Optionally pass a timeZone (e.g. from user preference or from the server), it falls back trying to read it from the `Intl` browwser native API.
14
+ */
15
+ export declare function getZonedDate(dateString?: string, timeZone?: string): Date;
16
+ export default getZonedDate;
@@ -0,0 +1,37 @@
1
+ import utcToZonedTime from "date-fns-tz/esm/utcToZonedTime";
2
+ import isBrowser from "./isBrowser";
3
+ /**
4
+ * It returns a `Date` object from a date `string` adjusted on the user timeZone,
5
+ * if a timeZone is not provided we try getting it from the `Intl` browwser native
6
+ * API. It gracefully falls back returning a _non-timezone-based_ `Date`.
7
+ *
8
+ * @category date
9
+ *
10
+ * @resources
11
+ * - to get the timeZone client side see [this article](https://attacomsian.com/blog/javascript-current-timezone)
12
+ * - for converting the date based on the time zone [date-fns docs](https://date-fns.org/v2.27.0/docs/Time-Zones) and [date-fns-tz docs](https://github.com/marnusw/date-fns-tz)
13
+ *
14
+ * @param dateString A parseable date as string, `Z` is automatically suffixed if not present to correctly get time zone based time from a UTC date.
15
+ * @param timeZone Optionally pass a timeZone (e.g. from user preference or from the server), it falls back trying to read it from the `Intl` browwser native API.
16
+ */
17
+ export function getZonedDate(dateString, timeZone) {
18
+ if (dateString === void 0) { dateString = ""; }
19
+ if (!dateString.endsWith("Z"))
20
+ dateString += "Z";
21
+ if (!timeZone && isBrowser) {
22
+ try {
23
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
24
+ }
25
+ catch (e) {
26
+ if (process.env["NODE_ENV"] !== "production") {
27
+ console.warn("[@koine/utils:getZonedDate] failed reading timeZone, error", e);
28
+ }
29
+ // no need to do anything here, it just means `Intl` failed, probably
30
+ // because the browser does not support it
31
+ }
32
+ }
33
+ return timeZone
34
+ ? utcToZonedTime(new Date(dateString), timeZone)
35
+ : new Date(dateString);
36
+ }
37
+ export default getZonedDate;
package/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export * from "./getUrlHashParams";
28
28
  export * from "./getUrlHashPathname";
29
29
  export * from "./getUrlPathnameParts";
30
30
  export * from "./getUrlQueryParams";
31
+ export * from "./getZonedDate";
31
32
  export * from "./imgEmptyPixel";
32
33
  export * from "./isAnyObject";
33
34
  export * from "./isArray";
package/index.js CHANGED
@@ -29,6 +29,7 @@ export * from "./getUrlHashParams";
29
29
  export * from "./getUrlHashPathname";
30
30
  export * from "./getUrlPathnameParts";
31
31
  export * from "./getUrlQueryParams";
32
+ export * from "./getZonedDate";
32
33
  export * from "./imgEmptyPixel";
33
34
  export * from "./isAnyObject";
34
35
  export * from "./isArray";
@@ -7,10 +7,10 @@ exports.capitalize = void 0;
7
7
  * @category text
8
8
  * @see https://stackoverflow.com/a/11409944/1938970
9
9
  */
10
- function capitalize(text) {
11
- return text
12
- ? (text.charAt(0).toUpperCase() + text.slice(1))
13
- : "";
10
+ function capitalize(string) {
11
+ var ensuredString = string || "";
12
+ return (ensuredString.charAt(0).toUpperCase() +
13
+ ensuredString.slice(1));
14
14
  }
15
15
  exports.capitalize = capitalize;
16
16
  exports.default = capitalize;
@@ -10,11 +10,15 @@ var getParamAsString_1 = require("./getParamAsString");
10
10
  * @category location
11
11
  *
12
12
  * @param {string} [raw] - The _raw_ query parameter
13
- * @param {number} [fallback=1] - Fallback value, `1` by default
13
+ * @param {number} [fallback] - Fallback number, we return `null` if not provided
14
14
  */
15
15
  function getParamAsInt(raw, fallback) {
16
- if (fallback === void 0) { fallback = 1; }
17
- return parseInt((0, getParamAsString_1.default)(raw) || String(fallback), 10);
16
+ if (fallback === void 0) { fallback = null; }
17
+ var string = (0, getParamAsString_1.default)(raw);
18
+ if (string) {
19
+ return parseInt(string, 10);
20
+ }
21
+ return fallback;
18
22
  }
19
23
  exports.getParamAsInt = getParamAsInt;
20
24
  exports.default = getParamAsInt;
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getUrlHashParams = void 0;
4
4
  /**
5
5
  * It returns the "query params" as an object extracting it from the given `hash`
6
- *string or, if not provided, failling back reading the `location.hash`
7
-
6
+ * string or, if not provided, failling back reading the `location.hash`
7
+ *
8
8
  * @category location
9
9
  */
10
10
  function getUrlHashParams(hash) {
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getUrlHashPathname = void 0;
4
4
  /**
5
5
  * It returns the "pathname" cleaned up from the `#` and the initial slashes
6
- * extracting it from the given `hash` string or, if not provided, failling
6
+ * extracting it from the given `hash` string or, if not provided, failling
7
7
  * back reading the `location.hash`
8
8
  *
9
9
  * @category location
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getZonedDate = void 0;
4
+ var utcToZonedTime_1 = require("date-fns-tz/esm/utcToZonedTime");
5
+ var isBrowser_1 = require("./isBrowser");
6
+ /**
7
+ * It returns a `Date` object from a date `string` adjusted on the user timeZone,
8
+ * if a timeZone is not provided we try getting it from the `Intl` browwser native
9
+ * API. It gracefully falls back returning a _non-timezone-based_ `Date`.
10
+ *
11
+ * @category date
12
+ *
13
+ * @resources
14
+ * - to get the timeZone client side see [this article](https://attacomsian.com/blog/javascript-current-timezone)
15
+ * - for converting the date based on the time zone [date-fns docs](https://date-fns.org/v2.27.0/docs/Time-Zones) and [date-fns-tz docs](https://github.com/marnusw/date-fns-tz)
16
+ *
17
+ * @param dateString A parseable date as string, `Z` is automatically suffixed if not present to correctly get time zone based time from a UTC date.
18
+ * @param timeZone Optionally pass a timeZone (e.g. from user preference or from the server), it falls back trying to read it from the `Intl` browwser native API.
19
+ */
20
+ function getZonedDate(dateString, timeZone) {
21
+ if (dateString === void 0) { dateString = ""; }
22
+ if (!dateString.endsWith("Z"))
23
+ dateString += "Z";
24
+ if (!timeZone && isBrowser_1.default) {
25
+ try {
26
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
27
+ }
28
+ catch (e) {
29
+ if (process.env["NODE_ENV"] !== "production") {
30
+ console.warn("[@koine/utils:getZonedDate] failed reading timeZone, error", e);
31
+ }
32
+ // no need to do anything here, it just means `Intl` failed, probably
33
+ // because the browser does not support it
34
+ }
35
+ }
36
+ return timeZone
37
+ ? (0, utcToZonedTime_1.default)(new Date(dateString), timeZone)
38
+ : new Date(dateString);
39
+ }
40
+ exports.getZonedDate = getZonedDate;
41
+ exports.default = getZonedDate;
package/node/index.js CHANGED
@@ -32,6 +32,7 @@ tslib_1.__exportStar(require("./getUrlHashParams"), exports);
32
32
  tslib_1.__exportStar(require("./getUrlHashPathname"), exports);
33
33
  tslib_1.__exportStar(require("./getUrlPathnameParts"), exports);
34
34
  tslib_1.__exportStar(require("./getUrlQueryParams"), exports);
35
+ tslib_1.__exportStar(require("./getZonedDate"), exports);
35
36
  tslib_1.__exportStar(require("./imgEmptyPixel"), exports);
36
37
  tslib_1.__exportStar(require("./isAnyObject"), exports);
37
38
  tslib_1.__exportStar(require("./isArray"), exports);
@@ -10,7 +10,7 @@ function converterRead(value) {
10
10
  function readCookie(name) {
11
11
  if (typeof document === "undefined") {
12
12
  if (process.env["NODE_ENV"] !== "production") {
13
- console.warn("[@koine/utils] readCookie: document is undefined");
13
+ console.warn("[@koine/utils:readCookie] document is undefined");
14
14
  }
15
15
  return name ? "" : {};
16
16
  }
@@ -28,7 +28,7 @@ function readCookie(name) {
28
28
  }
29
29
  catch (e) {
30
30
  if (process.env["NODE_ENV"] !== "production") {
31
- console.warn("[@koine/utils] readCookie: failed to decode", value);
31
+ console.warn("[@koine/utils:readCookie] failed to decode", value);
32
32
  }
33
33
  }
34
34
  }
package/node/setCookie.js CHANGED
@@ -4,6 +4,7 @@ exports.setCookie = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var cookie_1 = require("./cookie");
6
6
  var isNumber_1 = require("./isNumber");
7
+ var isUndefined_1 = require("./isUndefined");
7
8
  function converterWrite(value) {
8
9
  return encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent);
9
10
  }
@@ -19,9 +20,9 @@ function setCookie(name, value, attributes) {
19
20
  // eslint-disable-next-line prefer-const
20
21
  var expires = attributes.expires, restAttrs = tslib_1.__rest(attributes, ["expires"]);
21
22
  var cleanedAttrs = tslib_1.__assign(tslib_1.__assign({ expires: "" }, cookie_1.defaultAttributesClient), restAttrs);
22
- if (typeof document === "undefined") {
23
+ if ((0, isUndefined_1.default)(document)) {
23
24
  if (process.env["NODE_ENV"] !== "production") {
24
- console.warn("[@koine/utils] cookie setCookie: document is undefined");
25
+ console.warn("[@koine/utils:setCookie] document is undefined");
25
26
  }
26
27
  return undefined;
27
28
  }
package/package.json CHANGED
@@ -5,9 +5,10 @@
5
5
  "typings": "./index.d.ts",
6
6
  "dependencies": {},
7
7
  "peerDependencies": {
8
+ "date-fns-tz": "^1.3.7",
8
9
  "tslib": "^2.4.0"
9
10
  },
10
- "version": "1.0.74",
11
+ "version": "1.0.77",
11
12
  "module": "./index.js",
12
13
  "types": "./index.d.ts"
13
14
  }
package/readCookie.js CHANGED
@@ -7,7 +7,7 @@ function converterRead(value) {
7
7
  export function readCookie(name) {
8
8
  if (typeof document === "undefined") {
9
9
  if (process.env["NODE_ENV"] !== "production") {
10
- console.warn("[@koine/utils] readCookie: document is undefined");
10
+ console.warn("[@koine/utils:readCookie] document is undefined");
11
11
  }
12
12
  return name ? "" : {};
13
13
  }
@@ -25,7 +25,7 @@ export function readCookie(name) {
25
25
  }
26
26
  catch (e) {
27
27
  if (process.env["NODE_ENV"] !== "production") {
28
- console.warn("[@koine/utils] readCookie: failed to decode", value);
28
+ console.warn("[@koine/utils:readCookie] failed to decode", value);
29
29
  }
30
30
  }
31
31
  }
package/setCookie.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { __assign, __rest } from "tslib";
2
2
  import { defaultAttributesClient } from "./cookie";
3
3
  import isNumber from "./isNumber";
4
+ import isUndefined from "./isUndefined";
4
5
  function converterWrite(value) {
5
6
  return encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent);
6
7
  }
@@ -16,9 +17,9 @@ export function setCookie(name, value, attributes) {
16
17
  // eslint-disable-next-line prefer-const
17
18
  var expires = attributes.expires, restAttrs = __rest(attributes, ["expires"]);
18
19
  var cleanedAttrs = __assign(__assign({ expires: "" }, defaultAttributesClient), restAttrs);
19
- if (typeof document === "undefined") {
20
+ if (isUndefined(document)) {
20
21
  if (process.env["NODE_ENV"] !== "production") {
21
- console.warn("[@koine/utils] cookie setCookie: document is undefined");
22
+ console.warn("[@koine/utils:setCookie] document is undefined");
22
23
  }
23
24
  return undefined;
24
25
  }