@koine/utils 1.0.76 → 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/getUrlHashParams.d.ts +2 -2
- package/getUrlHashParams.js +2 -2
- package/getUrlHashPathname.d.ts +1 -1
- package/getUrlHashPathname.js +1 -1
- package/getZonedDate.d.ts +16 -0
- package/getZonedDate.js +37 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/node/getUrlHashParams.js +2 -2
- package/node/getUrlHashPathname.js +1 -1
- package/node/getZonedDate.js +41 -0
- package/node/index.js +1 -0
- package/node/readCookie.js +2 -2
- package/node/setCookie.js +3 -2
- package/package.json +2 -1
- package/readCookie.js +2 -2
- package/setCookie.js +3 -2
package/getUrlHashParams.d.ts
CHANGED
|
@@ -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;
|
package/getUrlHashParams.js
CHANGED
|
@@ -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) {
|
package/getUrlHashPathname.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* It returns the "pathname" cleaned up from the `#` and the initial slashes
|
|
3
|
-
*
|
|
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
|
package/getUrlHashPathname.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* It returns the "pathname" cleaned up from the `#` and the initial slashes
|
|
3
|
-
*
|
|
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;
|
package/getZonedDate.js
ADDED
|
@@ -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";
|
package/node/getUrlHashParams.js
CHANGED
|
@@ -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
|
-
*
|
|
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);
|
package/node/readCookie.js
CHANGED
|
@@ -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]
|
|
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]
|
|
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 (
|
|
23
|
+
if ((0, isUndefined_1.default)(document)) {
|
|
23
24
|
if (process.env["NODE_ENV"] !== "production") {
|
|
24
|
-
console.warn("[@koine/utils]
|
|
25
|
+
console.warn("[@koine/utils:setCookie] document is undefined");
|
|
25
26
|
}
|
|
26
27
|
return undefined;
|
|
27
28
|
}
|
package/package.json
CHANGED
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]
|
|
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]
|
|
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 (
|
|
20
|
+
if (isUndefined(document)) {
|
|
20
21
|
if (process.env["NODE_ENV"] !== "production") {
|
|
21
|
-
console.warn("[@koine/utils]
|
|
22
|
+
console.warn("[@koine/utils:setCookie] document is undefined");
|
|
22
23
|
}
|
|
23
24
|
return undefined;
|
|
24
25
|
}
|