@koine/browser 2.0.0-beta.13 → 2.0.0-beta.15
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/createStorage.d.ts +0 -47
- package/createStorage.js +101 -0
- package/getZonedDate.d.ts +0 -14
- package/getZonedDate.js +21 -0
- package/gtagPageview.d.ts +0 -3
- package/gtagPageview.js +15 -0
- package/index.d.ts +15 -15
- package/index.js +15 -0
- package/isIE.d.ts +0 -5
- package/{isIE.mjs → isIE.js} +3 -6
- package/isMobile.d.ts +0 -5
- package/isMobile.js +9 -0
- package/navigateToHash.d.ts +0 -6
- package/navigateToHash.js +7 -0
- package/navigateToHashParams.d.ts +0 -6
- package/navigateToHashParams.js +16 -0
- package/navigateToMergedHashParams.d.ts +0 -5
- package/{navigateToMergedHashParams.mjs → navigateToMergedHashParams.js} +4 -6
- package/navigateToMergedParams.d.ts +0 -6
- package/navigateToMergedParams.js +8 -0
- package/navigateToParams.d.ts +0 -7
- package/navigateToParams.js +12 -0
- package/navigateToUrl.d.ts +0 -5
- package/navigateToUrl.js +7 -0
- package/navigateWithoutUrlParam.d.ts +0 -6
- package/navigateWithoutUrlParam.js +13 -0
- package/package.json +56 -6
- package/redirectTo.d.ts +0 -6
- package/redirectTo.js +9 -0
- package/storage.d.ts +0 -5
- package/storage.js +6 -0
- package/storageClient.d.ts +0 -8
- package/storageClient.js +95 -0
- package/README.md +0 -1
- package/createStorage.mjs +0 -133
- package/getZonedDate.mjs +0 -31
- package/gtagPageview.mjs +0 -23
- package/index.mjs +0 -15
- package/isMobile.mjs +0 -14
- package/navigateToHash.mjs +0 -11
- package/navigateToHashParams.mjs +0 -19
- package/navigateToMergedParams.mjs +0 -12
- package/navigateToParams.mjs +0 -17
- package/navigateToUrl.mjs +0 -10
- package/navigateWithoutUrlParam.mjs +0 -18
- package/redirectTo.mjs +0 -14
- package/storage.mjs +0 -10
- package/storageClient.mjs +0 -97
package/createStorage.d.ts
CHANGED
|
@@ -1,59 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @category storage
|
|
3
|
-
*/
|
|
4
1
|
export type CreateStorageConfig = Record<string, any>;
|
|
5
|
-
/**
|
|
6
|
-
* Utility to create a storage instance to interact with `localStorage` using
|
|
7
|
-
* encrypted (encoded) key/values.
|
|
8
|
-
*
|
|
9
|
-
* @category storage
|
|
10
|
-
*/
|
|
11
2
|
export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T>, useSessionStorage?: boolean) => {
|
|
12
|
-
/**
|
|
13
|
-
* Get all storage value (it uses `localStorage.get()`).
|
|
14
|
-
*
|
|
15
|
-
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
16
|
-
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
17
|
-
* returned, otherwise `null`.
|
|
18
|
-
*/
|
|
19
3
|
get<TKey extends Extract<keyof T, string>>(key: TKey, defaultValue?: T[TKey] | null | undefined): T[TKey] | null;
|
|
20
|
-
/**
|
|
21
|
-
* Get all storage values (it uses `localStorage.get()`).
|
|
22
|
-
*
|
|
23
|
-
* `undefined` and `null` values are not returned.
|
|
24
|
-
*/
|
|
25
4
|
getAll(defaultValues?: Partial<T> | undefined): T;
|
|
26
|
-
/**
|
|
27
|
-
* Set a storage value (it uses `localStorage.set()`).
|
|
28
|
-
*
|
|
29
|
-
* Non-string values are stringified with `JSON.stringify()`
|
|
30
|
-
*/
|
|
31
5
|
set<TKey_1 extends Extract<keyof T, string>>(key: TKey_1, value?: T[TKey_1] | undefined): void;
|
|
32
|
-
/**
|
|
33
|
-
* Set all given storage values (it uses `localStorage.set()`).
|
|
34
|
-
*
|
|
35
|
-
* Non-string values are stringified with `JSON.stringify()`, `undefined`
|
|
36
|
-
* and `null` values are removed from the storage
|
|
37
|
-
*/
|
|
38
6
|
setMany(newValues: Partial<T>): void;
|
|
39
|
-
/**
|
|
40
|
-
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
41
|
-
*/
|
|
42
7
|
has<TKey_2 extends Extract<keyof T, string>>(key: TKey_2): any;
|
|
43
|
-
/**
|
|
44
|
-
* Remove a storage value (it uses `localStorage.remove()`).
|
|
45
|
-
*/
|
|
46
8
|
remove<TKey_3 extends Extract<keyof T, string>>(key: TKey_3): void;
|
|
47
|
-
/**
|
|
48
|
-
* Clear all storage values (it uses `localStorage.remove()`).
|
|
49
|
-
*/
|
|
50
9
|
clear(): void;
|
|
51
|
-
/**
|
|
52
|
-
* Watch a storage value changes, this needs to be executed only in browser
|
|
53
|
-
* context (it uses `window.addEventListener("storage")`).
|
|
54
|
-
*
|
|
55
|
-
* Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
|
|
56
|
-
*/
|
|
57
10
|
watch: <TKey_4 extends keyof T>(keyToWatch: TKey_4, onRemoved?: () => void, onAdded?: () => void) => () => void | undefined;
|
|
58
11
|
};
|
|
59
12
|
export default createStorage;
|
package/createStorage.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import decode from "@koine/utils/decode";
|
|
3
|
+
import encode from "@koine/utils/encode";
|
|
4
|
+
import isBrowser from "@koine/utils/isBrowser";
|
|
5
|
+
import isNullOrUndefined from "@koine/utils/isNullOrUndefined";
|
|
6
|
+
import noop from "@koine/utils/noop";
|
|
7
|
+
import on from "@koine/dom/on";
|
|
8
|
+
import storage from "./storage.js";
|
|
9
|
+
export var createStorage = function (config, useSessionStorage) {
|
|
10
|
+
var client = useSessionStorage ? storage.s : storage.l;
|
|
11
|
+
var keys = Object.keys(config).reduce(function (map, key) {
|
|
12
|
+
var _a;
|
|
13
|
+
return (__assign(__assign({}, map), (_a = {}, _a[key] = encode(key), _a)));
|
|
14
|
+
}, {});
|
|
15
|
+
return {
|
|
16
|
+
get: function (key, defaultValue) {
|
|
17
|
+
return client.get(keys[key], decode, defaultValue);
|
|
18
|
+
},
|
|
19
|
+
getAll: function (defaultValues) {
|
|
20
|
+
if (!isBrowser) {
|
|
21
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
22
|
+
console.log("[@koine/utils:createStorage] attempt to use 'getAll' outside of browser.");
|
|
23
|
+
}
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
var all = {};
|
|
27
|
+
for (var key in keys) {
|
|
28
|
+
var value = this.get(key);
|
|
29
|
+
var defaultValue = defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues[key];
|
|
30
|
+
if (!isNullOrUndefined(value)) {
|
|
31
|
+
all[key] = value;
|
|
32
|
+
}
|
|
33
|
+
else if (defaultValue) {
|
|
34
|
+
all[key] = defaultValue;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return all;
|
|
38
|
+
},
|
|
39
|
+
set: function (key, value) {
|
|
40
|
+
client.set(keys[key], value, encode);
|
|
41
|
+
},
|
|
42
|
+
setMany: function (newValues) {
|
|
43
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
44
|
+
if (!isBrowser) {
|
|
45
|
+
console.log("[@koine/utils:createStorage] attempt to use 'setMany' outside of browser.");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (isBrowser) {
|
|
49
|
+
for (var key in newValues) {
|
|
50
|
+
var value = newValues[key];
|
|
51
|
+
if (!isNullOrUndefined(value)) {
|
|
52
|
+
this.set(key, value);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.remove(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
has: function (key) {
|
|
61
|
+
return client.has(keys[key]);
|
|
62
|
+
},
|
|
63
|
+
remove: function (key) {
|
|
64
|
+
client.remove(keys[key]);
|
|
65
|
+
},
|
|
66
|
+
clear: function () {
|
|
67
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
68
|
+
if (!isBrowser) {
|
|
69
|
+
console.log("[@koine/utils:createStorage] attempt to use 'clear' outside of browser.");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (isBrowser) {
|
|
73
|
+
for (var key in keys) {
|
|
74
|
+
client.remove(keys[key]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
watch: function (keyToWatch, onRemoved, onAdded) {
|
|
79
|
+
if (!isBrowser) {
|
|
80
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
81
|
+
console.log("[@koine/utils:createStorage] attempt to use 'watch' outside of browser.");
|
|
82
|
+
}
|
|
83
|
+
return noop;
|
|
84
|
+
}
|
|
85
|
+
var handler = function (event) {
|
|
86
|
+
var key = event.key, oldValue = event.oldValue, newValue = event.newValue;
|
|
87
|
+
if (key === keys[keyToWatch]) {
|
|
88
|
+
if (oldValue && !newValue) {
|
|
89
|
+
onRemoved === null || onRemoved === void 0 ? void 0 : onRemoved();
|
|
90
|
+
}
|
|
91
|
+
else if (!oldValue && newValue) {
|
|
92
|
+
onAdded === null || onAdded === void 0 ? void 0 : onAdded();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
var listener = on(window, "storage", handler);
|
|
97
|
+
return listener;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
export default createStorage;
|
package/getZonedDate.d.ts
CHANGED
|
@@ -1,16 +1,2 @@
|
|
|
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
1
|
export declare function getZonedDate(dateString?: string, timeZone?: string): Date;
|
|
16
2
|
export default getZonedDate;
|
package/getZonedDate.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import utcToZonedTime from "date-fns-tz/utcToZonedTime";
|
|
2
|
+
import isBrowser from "@koine/utils/isBrowser";
|
|
3
|
+
export function getZonedDate(dateString, timeZone) {
|
|
4
|
+
if (dateString === void 0) { dateString = ""; }
|
|
5
|
+
if (!dateString.endsWith("Z"))
|
|
6
|
+
dateString += "Z";
|
|
7
|
+
if (!timeZone && isBrowser) {
|
|
8
|
+
try {
|
|
9
|
+
timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
13
|
+
console.warn("[@koine/utils:getZonedDate] failed reading timeZone, error", e);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return timeZone
|
|
18
|
+
? utcToZonedTime(new Date(dateString), timeZone)
|
|
19
|
+
: new Date(dateString);
|
|
20
|
+
}
|
|
21
|
+
export default getZonedDate;
|
package/gtagPageview.d.ts
CHANGED
|
@@ -3,8 +3,5 @@ export type GtmPageviewArgs = [
|
|
|
3
3
|
page_title?: string,
|
|
4
4
|
page_location?: string
|
|
5
5
|
];
|
|
6
|
-
/**
|
|
7
|
-
* @category analytics-google
|
|
8
|
-
*/
|
|
9
6
|
export declare const gtagPageview: (page_path?: string | undefined, page_title?: string | undefined, page_location?: string | undefined) => void;
|
|
10
7
|
export default gtagPageview;
|
package/gtagPageview.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import isUndefined from "@koine/utils/isUndefined";
|
|
2
|
+
export var gtagPageview = function () {
|
|
3
|
+
var args = [];
|
|
4
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
5
|
+
args[_i] = arguments[_i];
|
|
6
|
+
}
|
|
7
|
+
if (!isUndefined(window) && !isUndefined(window.gtag)) {
|
|
8
|
+
window.gtag("event", "page_view", {
|
|
9
|
+
page_path: args[0] || location.pathname,
|
|
10
|
+
page_title: args[1] || document.title,
|
|
11
|
+
page_location: args[2] || location.href,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
export default gtagPageview;
|
package/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export { createStorage, type CreateStorageConfig } from "./createStorage";
|
|
2
|
-
export { getZonedDate } from "./getZonedDate";
|
|
3
|
-
export { gtagPageview, type GtmPageviewArgs } from "./gtagPageview";
|
|
4
|
-
export { isIE } from "./isIE";
|
|
5
|
-
export { isMobile } from "./isMobile";
|
|
6
|
-
export { navigateToHash } from "./navigateToHash";
|
|
7
|
-
export { navigateToHashParams } from "./navigateToHashParams";
|
|
8
|
-
export { navigateToMergedHashParams } from "./navigateToMergedHashParams";
|
|
9
|
-
export { navigateToMergedParams } from "./navigateToMergedParams";
|
|
10
|
-
export { navigateToParams } from "./navigateToParams";
|
|
11
|
-
export { navigateToUrl } from "./navigateToUrl";
|
|
12
|
-
export { navigateWithoutUrlParam } from "./navigateWithoutUrlParam";
|
|
13
|
-
export { redirectTo } from "./redirectTo";
|
|
14
|
-
export { storage } from "./storage";
|
|
15
|
-
export { storageClient, type StorageClientConfig } from "./storageClient";
|
|
1
|
+
export { createStorage, type CreateStorageConfig } from "./createStorage.js";
|
|
2
|
+
export { getZonedDate } from "./getZonedDate.js";
|
|
3
|
+
export { gtagPageview, type GtmPageviewArgs } from "./gtagPageview.js";
|
|
4
|
+
export { isIE } from "./isIE.js";
|
|
5
|
+
export { isMobile } from "./isMobile.js";
|
|
6
|
+
export { navigateToHash } from "./navigateToHash.js";
|
|
7
|
+
export { navigateToHashParams } from "./navigateToHashParams.js";
|
|
8
|
+
export { navigateToMergedHashParams } from "./navigateToMergedHashParams.js";
|
|
9
|
+
export { navigateToMergedParams } from "./navigateToMergedParams.js";
|
|
10
|
+
export { navigateToParams } from "./navigateToParams.js";
|
|
11
|
+
export { navigateToUrl } from "./navigateToUrl.js";
|
|
12
|
+
export { navigateWithoutUrlParam } from "./navigateWithoutUrlParam.js";
|
|
13
|
+
export { redirectTo } from "./redirectTo.js";
|
|
14
|
+
export { storage } from "./storage.js";
|
|
15
|
+
export { storageClient, type StorageClientConfig } from "./storageClient.js";
|
package/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { createStorage } from "./createStorage.js";
|
|
2
|
+
export { getZonedDate } from "./getZonedDate.js";
|
|
3
|
+
export { gtagPageview } from "./gtagPageview.js";
|
|
4
|
+
export { isIE } from "./isIE.js";
|
|
5
|
+
export { isMobile } from "./isMobile.js";
|
|
6
|
+
export { navigateToHash } from "./navigateToHash.js";
|
|
7
|
+
export { navigateToHashParams } from "./navigateToHashParams.js";
|
|
8
|
+
export { navigateToMergedHashParams } from "./navigateToMergedHashParams.js";
|
|
9
|
+
export { navigateToMergedParams } from "./navigateToMergedParams.js";
|
|
10
|
+
export { navigateToParams } from "./navigateToParams.js";
|
|
11
|
+
export { navigateToUrl } from "./navigateToUrl.js";
|
|
12
|
+
export { navigateWithoutUrlParam } from "./navigateWithoutUrlParam.js";
|
|
13
|
+
export { redirectTo } from "./redirectTo.js";
|
|
14
|
+
export { storage } from "./storage.js";
|
|
15
|
+
export { storageClient } from "./storageClient.js";
|
package/isIE.d.ts
CHANGED
package/{isIE.mjs → isIE.js}
RENAMED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import isServer from "@koine/utils/isServer";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @category is
|
|
5
|
-
* @see https://stackoverflow.com/a/21712356/12285349
|
|
6
|
-
*/ export function isIE(ssrValue = true) {
|
|
2
|
+
export function isIE(ssrValue) {
|
|
3
|
+
if (ssrValue === void 0) { ssrValue = true; }
|
|
7
4
|
if (isServer) {
|
|
8
5
|
return ssrValue;
|
|
9
6
|
}
|
|
10
|
-
|
|
7
|
+
var ua = window.navigator.userAgent;
|
|
11
8
|
if (ua.indexOf("MSIE ") > 0 || ua.indexOf("Trident/") > 0) {
|
|
12
9
|
return true;
|
|
13
10
|
}
|
package/isMobile.d.ts
CHANGED
package/isMobile.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import isServer from "@koine/utils/isServer";
|
|
2
|
+
export function isMobile(ssrValue) {
|
|
3
|
+
if (ssrValue === void 0) { ssrValue = true; }
|
|
4
|
+
if (isServer) {
|
|
5
|
+
return ssrValue;
|
|
6
|
+
}
|
|
7
|
+
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(window.navigator.userAgent);
|
|
8
|
+
}
|
|
9
|
+
export default isMobile;
|
package/navigateToHash.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* It updates the browser's location hash by replacing the history state.
|
|
3
|
-
* The non-silent standard way would simply be `location.hash = "#new-hash"`
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
*/
|
|
7
1
|
export declare function navigateToHash(hash?: string): void;
|
|
8
2
|
export default navigateToHash;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import navigateToUrl from "./navigateToUrl.js";
|
|
2
|
+
export function navigateToHash(hash) {
|
|
3
|
+
if (hash === void 0) { hash = ""; }
|
|
4
|
+
var pathname = location.pathname, search = location.search;
|
|
5
|
+
navigateToUrl(pathname + (search ? "?" + search : "") + (hash ? "#" + hash : ""), true);
|
|
6
|
+
}
|
|
7
|
+
export default navigateToHash;
|
|
@@ -1,9 +1,3 @@
|
|
|
1
1
|
import type { AnyQueryParams } from "@koine/utils/location";
|
|
2
|
-
/**
|
|
3
|
-
* It updates the `location.hash` with the given query params, it uses `location.hash`
|
|
4
|
-
* if a second argument `hash` is not provded
|
|
5
|
-
*
|
|
6
|
-
* @category location
|
|
7
|
-
*/
|
|
8
2
|
export declare function navigateToHashParams(params?: string | AnyQueryParams, hash?: string): string;
|
|
9
3
|
export default navigateToHashParams;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
2
|
+
import getUrlHashPathname from "@koine/utils/getUrlHashPathname";
|
|
3
|
+
export function navigateToHashParams(params, hash) {
|
|
4
|
+
if (params === void 0) { params = {}; }
|
|
5
|
+
if (hash === void 0) { hash = ""; }
|
|
6
|
+
var useLocation = !hash;
|
|
7
|
+
hash = hash || location.hash;
|
|
8
|
+
var hashQueryLess = getUrlHashPathname(hash);
|
|
9
|
+
var queryString = typeof params === "string" ? params : buildUrlQueryString(params);
|
|
10
|
+
var newHash = "#/" + hashQueryLess + queryString;
|
|
11
|
+
if (useLocation) {
|
|
12
|
+
location.hash = newHash;
|
|
13
|
+
}
|
|
14
|
+
return newHash;
|
|
15
|
+
}
|
|
16
|
+
export default navigateToHashParams;
|
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import type { AnyQueryParams } from "@koine/utils/location";
|
|
2
|
-
/**
|
|
3
|
-
* It updates the "query params" within the `location.hash`, it uses `location`
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
*/
|
|
7
2
|
export declare function navigateToMergedHashParams(params?: NonNullable<AnyQueryParams>, hash?: string): string;
|
|
8
3
|
export default navigateToMergedHashParams;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import getUrlHashParams from "@koine/utils/getUrlHashParams";
|
|
2
2
|
import mergeUrlQueryParams from "@koine/utils/mergeUrlQueryParams";
|
|
3
|
-
import { navigateToHashParams } from "./navigateToHashParams";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @category location
|
|
8
|
-
*/ export function navigateToMergedHashParams(params = {}, hash = "") {
|
|
3
|
+
import { navigateToHashParams } from "./navigateToHashParams.js";
|
|
4
|
+
export function navigateToMergedHashParams(params, hash) {
|
|
5
|
+
if (params === void 0) { params = {}; }
|
|
6
|
+
if (hash === void 0) { hash = ""; }
|
|
9
7
|
return navigateToHashParams(mergeUrlQueryParams(getUrlHashParams(hash), params), hash);
|
|
10
8
|
}
|
|
11
9
|
export default navigateToMergedHashParams;
|
|
@@ -1,9 +1,3 @@
|
|
|
1
1
|
import type { AnyQueryParams } from "@koine/utils/location";
|
|
2
|
-
/**
|
|
3
|
-
* Merge current URL query parameters with the given ones, it uses `history`.
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
7
|
-
*/
|
|
8
2
|
export declare function navigateToMergedParams(params?: NonNullable<AnyQueryParams>, replace?: boolean): string;
|
|
9
3
|
export default navigateToMergedParams;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import getUrlQueryParams from "@koine/utils/getUrlQueryParams";
|
|
2
|
+
import mergeUrlQueryParams from "@koine/utils/mergeUrlQueryParams";
|
|
3
|
+
import { navigateToParams } from "./navigateToParams.js";
|
|
4
|
+
export function navigateToMergedParams(params, replace) {
|
|
5
|
+
if (params === void 0) { params = {}; }
|
|
6
|
+
return navigateToParams(mergeUrlQueryParams(getUrlQueryParams(), params), replace);
|
|
7
|
+
}
|
|
8
|
+
export default navigateToMergedParams;
|
package/navigateToParams.d.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
1
|
import type { AnyQueryParams } from "@koine/utils/location";
|
|
2
|
-
/**
|
|
3
|
-
* Change current URL query parameters, it uses `history`.
|
|
4
|
-
*
|
|
5
|
-
* @category location
|
|
6
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
7
|
-
* @returns The query string with initial `?`
|
|
8
|
-
*/
|
|
9
2
|
export declare function navigateToParams(params?: string | AnyQueryParams, replace?: boolean): string;
|
|
10
3
|
export default navigateToParams;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
2
|
+
import isBrowser from "@koine/utils/isBrowser";
|
|
3
|
+
import navigateToUrl from "./navigateToUrl.js";
|
|
4
|
+
export function navigateToParams(params, replace) {
|
|
5
|
+
if (params === void 0) { params = {}; }
|
|
6
|
+
var queryString = typeof params === "string" ? params : buildUrlQueryString(params);
|
|
7
|
+
if (isBrowser) {
|
|
8
|
+
navigateToUrl(location.pathname + queryString, replace);
|
|
9
|
+
}
|
|
10
|
+
return queryString;
|
|
11
|
+
}
|
|
12
|
+
export default navigateToParams;
|
package/navigateToUrl.d.ts
CHANGED
package/navigateToUrl.js
ADDED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remove URL query parameter, it uses `history`
|
|
3
|
-
*
|
|
4
|
-
* @category location
|
|
5
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
6
|
-
*/
|
|
7
1
|
export declare function navigateWithoutUrlParam(paramName?: string, replace?: boolean): string;
|
|
8
2
|
export default navigateWithoutUrlParam;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import getUrlQueryParams from "@koine/utils/getUrlQueryParams";
|
|
2
|
+
import navigateToParams from "./navigateToParams.js";
|
|
3
|
+
export function navigateWithoutUrlParam(paramName, replace) {
|
|
4
|
+
var params = {};
|
|
5
|
+
var currentParams = getUrlQueryParams();
|
|
6
|
+
for (var key in currentParams) {
|
|
7
|
+
if (key !== paramName) {
|
|
8
|
+
params[key] = currentParams[key];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return navigateToParams(params, replace);
|
|
12
|
+
}
|
|
13
|
+
export default navigateWithoutUrlParam;
|
package/package.json
CHANGED
|
@@ -2,12 +2,62 @@
|
|
|
2
2
|
"name": "@koine/browser",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@koine/dom": "2.0.0-beta.
|
|
6
|
-
"@koine/utils": "2.0.0-beta.
|
|
5
|
+
"@koine/dom": "2.0.0-beta.15",
|
|
6
|
+
"@koine/utils": "2.0.0-beta.15"
|
|
7
7
|
},
|
|
8
|
-
"module": "./index.mjs",
|
|
9
|
-
"main": "./index.js",
|
|
10
8
|
"types": "./index.d.ts",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"module": "./index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./index.js"
|
|
14
|
+
},
|
|
15
|
+
"./createStorage": {
|
|
16
|
+
"import": "./createStorage.js"
|
|
17
|
+
},
|
|
18
|
+
"./getZonedDate": {
|
|
19
|
+
"import": "./getZonedDate.js"
|
|
20
|
+
},
|
|
21
|
+
"./gtagPageview": {
|
|
22
|
+
"import": "./gtagPageview.js"
|
|
23
|
+
},
|
|
24
|
+
"./isIE": {
|
|
25
|
+
"import": "./isIE.js"
|
|
26
|
+
},
|
|
27
|
+
"./isMobile": {
|
|
28
|
+
"import": "./isMobile.js"
|
|
29
|
+
},
|
|
30
|
+
"./navigateToHash": {
|
|
31
|
+
"import": "./navigateToHash.js"
|
|
32
|
+
},
|
|
33
|
+
"./navigateToHashParams": {
|
|
34
|
+
"import": "./navigateToHashParams.js"
|
|
35
|
+
},
|
|
36
|
+
"./navigateToMergedHashParams": {
|
|
37
|
+
"import": "./navigateToMergedHashParams.js"
|
|
38
|
+
},
|
|
39
|
+
"./navigateToMergedParams": {
|
|
40
|
+
"import": "./navigateToMergedParams.js"
|
|
41
|
+
},
|
|
42
|
+
"./navigateToParams": {
|
|
43
|
+
"import": "./navigateToParams.js"
|
|
44
|
+
},
|
|
45
|
+
"./navigateToUrl": {
|
|
46
|
+
"import": "./navigateToUrl.js"
|
|
47
|
+
},
|
|
48
|
+
"./navigateWithoutUrlParam": {
|
|
49
|
+
"import": "./navigateWithoutUrlParam.js"
|
|
50
|
+
},
|
|
51
|
+
"./redirectTo": {
|
|
52
|
+
"import": "./redirectTo.js"
|
|
53
|
+
},
|
|
54
|
+
"./storage": {
|
|
55
|
+
"import": "./storage.js"
|
|
56
|
+
},
|
|
57
|
+
"./storageClient": {
|
|
58
|
+
"import": "./storageClient.js"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
11
61
|
"peerDependencies": {},
|
|
12
|
-
"version": "2.0.0-beta.
|
|
13
|
-
}
|
|
62
|
+
"version": "2.0.0-beta.15"
|
|
63
|
+
}
|
package/redirectTo.d.ts
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
1
|
import type { AnyQueryParams } from "@koine/utils/location";
|
|
2
|
-
/**
|
|
3
|
-
* Redirect to url with params {optionally}, removes eventual trailing question
|
|
4
|
-
* marks from the given URL, it uses `location`
|
|
5
|
-
*
|
|
6
|
-
* @category location
|
|
7
|
-
*/
|
|
8
2
|
export declare function redirectTo(url: string, params?: AnyQueryParams): void;
|
|
9
3
|
export default redirectTo;
|
package/redirectTo.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
2
|
+
import isBrowser from "@koine/utils/isBrowser";
|
|
3
|
+
export function redirectTo(url, params) {
|
|
4
|
+
if (isBrowser) {
|
|
5
|
+
var queryString = buildUrlQueryString(params);
|
|
6
|
+
location.href = url.replace(/\?+$/g, "") + queryString;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export default redirectTo;
|
package/storage.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Storage, for `localStorage` and `sessionStorage`
|
|
3
|
-
*
|
|
4
|
-
* @category storage
|
|
5
|
-
*/
|
|
6
1
|
export declare const storage: {
|
|
7
2
|
l: {
|
|
8
3
|
get: <TKey extends string, TValue = any>(key: TKey, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
package/storage.js
ADDED
package/storageClient.d.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @category storage
|
|
3
|
-
*/
|
|
4
1
|
export type StorageClientConfig = Record<string, any>;
|
|
5
|
-
/**
|
|
6
|
-
* Super minifiable `local/session Storage` client creator with SSR safety
|
|
7
|
-
*
|
|
8
|
-
* @category storage
|
|
9
|
-
*/
|
|
10
2
|
export declare const storageClient: <TConfig extends StorageClientConfig = StorageClientConfig>(useSessionStorage?: boolean) => {
|
|
11
3
|
get: <TKey extends Extract<keyof TConfig, string>, TValue = TConfig[TKey]>(key: TKey, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
|
12
4
|
set: <TKey_1 extends Extract<keyof TConfig, string>, TValue_1 = TConfig[TKey_1]>(key: TKey_1, value?: TValue_1 | undefined, transform?: (value: any) => string) => void;
|
package/storageClient.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import isBrowser from "@koine/utils/isBrowser";
|
|
2
|
+
import isString from "@koine/utils/isString";
|
|
3
|
+
var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
|
|
4
|
+
export var storageClient = function (useSessionStorage) {
|
|
5
|
+
var nativeMethod = function (method, key, value) {
|
|
6
|
+
return isBrowser
|
|
7
|
+
? window[useSessionStorage ? "sessionStorage" : "localStorage"][methodsMap[method]](key, value)
|
|
8
|
+
: function () {
|
|
9
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
10
|
+
console.warn("[@koine/utils:storageClient]: ".concat(useSessionStorage ? "sessionStorage" : "localStorage", " does not exists outside of browser."));
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
var get = function (key, transform, defaultValue) {
|
|
15
|
+
var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
|
|
16
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
17
|
+
if (!isBrowser) {
|
|
18
|
+
console.log("[@koine/utils:storage] called 'get' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (isBrowser) {
|
|
22
|
+
var stored = nativeMethod("g", key);
|
|
23
|
+
if (stored) {
|
|
24
|
+
stored = transform ? transform(stored) : stored;
|
|
25
|
+
try {
|
|
26
|
+
var parsed = JSON.parse(stored);
|
|
27
|
+
if (parsed)
|
|
28
|
+
value = parsed;
|
|
29
|
+
}
|
|
30
|
+
catch (_e) {
|
|
31
|
+
value = stored;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
var set = function (key, value, transform) {
|
|
38
|
+
if (transform === void 0) { transform = function (value) { return value; }; }
|
|
39
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
40
|
+
if (!isBrowser) {
|
|
41
|
+
console.log("[@koine/utils:storage] called 'set' outside of browser does not work.");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (isBrowser) {
|
|
45
|
+
try {
|
|
46
|
+
var transformedValue = isString(value)
|
|
47
|
+
? transform(value)
|
|
48
|
+
: transform(JSON.stringify(value));
|
|
49
|
+
nativeMethod("s", key, transformedValue);
|
|
50
|
+
}
|
|
51
|
+
catch (_e) {
|
|
52
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
53
|
+
console.warn("[@koine/utils:createStorage]: 'set' error.", _e);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var remove = function (key) {
|
|
59
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
60
|
+
if (!isBrowser) {
|
|
61
|
+
console.log("[@koine/utils:storage] called 'remove' outside of browser does not work.");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (isBrowser) {
|
|
65
|
+
try {
|
|
66
|
+
nativeMethod("r", key);
|
|
67
|
+
}
|
|
68
|
+
catch (_e) {
|
|
69
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
70
|
+
console.warn("[@koine/utils:createStorage]: 'remove' error.", _e);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var has = function (key, defaultValue) {
|
|
76
|
+
var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : false;
|
|
77
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
78
|
+
if (!isBrowser) {
|
|
79
|
+
console.log("[@koine/utils:storage] called 'has' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (isBrowser) {
|
|
83
|
+
var stored = nativeMethod("g", key);
|
|
84
|
+
value = stored !== null && stored !== void 0 ? stored : false;
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
get: get,
|
|
90
|
+
set: set,
|
|
91
|
+
remove: remove,
|
|
92
|
+
has: has,
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
export default storageClient;
|
package/README.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# @koine/browser
|
package/createStorage.mjs
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import decode from "@koine/utils/decode";
|
|
2
|
-
import encode from "@koine/utils/encode";
|
|
3
|
-
import isBrowser from "@koine/utils/isBrowser";
|
|
4
|
-
import isNullOrUndefined from "@koine/utils/isNullOrUndefined";
|
|
5
|
-
import noop from "@koine/utils/noop";
|
|
6
|
-
import on from "@koine/dom/on";
|
|
7
|
-
import storage from "./storage";
|
|
8
|
-
/**
|
|
9
|
-
* Utility to create a storage instance to interact with `localStorage` using
|
|
10
|
-
* encrypted (encoded) key/values.
|
|
11
|
-
*
|
|
12
|
-
* @category storage
|
|
13
|
-
*/ export const createStorage = (config, useSessionStorage)=>{
|
|
14
|
-
const client = useSessionStorage ? storage.s : storage.l;
|
|
15
|
-
const keys = Object.keys(config).reduce((map, key)=>({
|
|
16
|
-
...map,
|
|
17
|
-
[key]: encode(key)
|
|
18
|
-
}), {});
|
|
19
|
-
return {
|
|
20
|
-
/**
|
|
21
|
-
* Get all storage value (it uses `localStorage.get()`).
|
|
22
|
-
*
|
|
23
|
-
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
24
|
-
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
25
|
-
* returned, otherwise `null`.
|
|
26
|
-
*/ get (key, defaultValue) {
|
|
27
|
-
return client.get(keys[key], decode, defaultValue);
|
|
28
|
-
},
|
|
29
|
-
/**
|
|
30
|
-
* Get all storage values (it uses `localStorage.get()`).
|
|
31
|
-
*
|
|
32
|
-
* `undefined` and `null` values are not returned.
|
|
33
|
-
*/ getAll (defaultValues) {
|
|
34
|
-
if (!isBrowser) {
|
|
35
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
36
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'getAll' outside of browser.`);
|
|
37
|
-
}
|
|
38
|
-
return {};
|
|
39
|
-
}
|
|
40
|
-
const all = {};
|
|
41
|
-
for(const key in keys){
|
|
42
|
-
const value = this.get(key);
|
|
43
|
-
const defaultValue = defaultValues?.[key];
|
|
44
|
-
if (!isNullOrUndefined(value)) {
|
|
45
|
-
all[key] = value;
|
|
46
|
-
} else if (defaultValue) {
|
|
47
|
-
// NOTE: without the assertion typedoc does not compile
|
|
48
|
-
all[key] = defaultValue;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return all;
|
|
52
|
-
},
|
|
53
|
-
/**
|
|
54
|
-
* Set a storage value (it uses `localStorage.set()`).
|
|
55
|
-
*
|
|
56
|
-
* Non-string values are stringified with `JSON.stringify()`
|
|
57
|
-
*/ set (key, value) {
|
|
58
|
-
client.set(keys[key], value, encode);
|
|
59
|
-
},
|
|
60
|
-
/**
|
|
61
|
-
* Set all given storage values (it uses `localStorage.set()`).
|
|
62
|
-
*
|
|
63
|
-
* Non-string values are stringified with `JSON.stringify()`, `undefined`
|
|
64
|
-
* and `null` values are removed from the storage
|
|
65
|
-
*/ setMany (newValues) {
|
|
66
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
67
|
-
if (!isBrowser) {
|
|
68
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'setMany' outside of browser.`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
if (isBrowser) {
|
|
72
|
-
for(const key in newValues){
|
|
73
|
-
const value = newValues[key];
|
|
74
|
-
if (!isNullOrUndefined(value)) {
|
|
75
|
-
this.set(key, value);
|
|
76
|
-
} else {
|
|
77
|
-
this.remove(key);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
/**
|
|
83
|
-
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
84
|
-
*/ has (key) {
|
|
85
|
-
return client.has(keys[key]);
|
|
86
|
-
},
|
|
87
|
-
/**
|
|
88
|
-
* Remove a storage value (it uses `localStorage.remove()`).
|
|
89
|
-
*/ remove (key) {
|
|
90
|
-
client.remove(keys[key]);
|
|
91
|
-
},
|
|
92
|
-
/**
|
|
93
|
-
* Clear all storage values (it uses `localStorage.remove()`).
|
|
94
|
-
*/ clear () {
|
|
95
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
96
|
-
if (!isBrowser) {
|
|
97
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'clear' outside of browser.`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (isBrowser) {
|
|
101
|
-
for(const key in keys){
|
|
102
|
-
client.remove(keys[key]);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
/**
|
|
107
|
-
* Watch a storage value changes, this needs to be executed only in browser
|
|
108
|
-
* context (it uses `window.addEventListener("storage")`).
|
|
109
|
-
*
|
|
110
|
-
* Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
|
|
111
|
-
*/ watch: (keyToWatch, onRemoved, onAdded)=>{
|
|
112
|
-
if (!isBrowser) {
|
|
113
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
114
|
-
console.log(`[@koine/utils:createStorage] attempt to use 'watch' outside of browser.`);
|
|
115
|
-
}
|
|
116
|
-
return noop;
|
|
117
|
-
}
|
|
118
|
-
const handler = (event)=>{
|
|
119
|
-
const { key, oldValue, newValue } = event;
|
|
120
|
-
if (key === keys[keyToWatch]) {
|
|
121
|
-
if (oldValue && !newValue) {
|
|
122
|
-
onRemoved?.();
|
|
123
|
-
} else if (!oldValue && newValue) {
|
|
124
|
-
onAdded?.();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
const listener = on(window, "storage", handler);
|
|
129
|
-
return listener;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
};
|
|
133
|
-
export default createStorage;
|
package/getZonedDate.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import utcToZonedTime from "date-fns-tz/utcToZonedTime";
|
|
2
|
-
import isBrowser from "@koine/utils/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
|
-
*/ export function getZonedDate(dateString = "", timeZone) {
|
|
17
|
-
if (!dateString.endsWith("Z")) dateString += "Z";
|
|
18
|
-
if (!timeZone && isBrowser) {
|
|
19
|
-
try {
|
|
20
|
-
timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
21
|
-
} catch (e) {
|
|
22
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
23
|
-
console.warn("[@koine/utils:getZonedDate] failed reading timeZone, error", e);
|
|
24
|
-
}
|
|
25
|
-
// no need to do anything here, it just means `Intl` failed, probably
|
|
26
|
-
// because the browser does not support it
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return timeZone ? utcToZonedTime(new Date(dateString), timeZone) : new Date(dateString);
|
|
30
|
-
}
|
|
31
|
-
export default getZonedDate;
|
package/gtagPageview.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import isUndefined from "@koine/utils/isUndefined";
|
|
2
|
-
/**
|
|
3
|
-
* @category analytics-google
|
|
4
|
-
*/ export const gtagPageview = (...args)=>{
|
|
5
|
-
if (!isUndefined(window) && !isUndefined(window.gtag)) {
|
|
6
|
-
window.gtag("event", "page_view", {
|
|
7
|
-
page_path: args[0] || location.pathname,
|
|
8
|
-
page_title: args[1] || document.title,
|
|
9
|
-
page_location: args[2] || location.href
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
export default gtagPageview; // export type GtmEventArgs = [
|
|
14
|
-
// eventCategory?: string,
|
|
15
|
-
// eventAction?: string,
|
|
16
|
-
// eventLabel?: string,
|
|
17
|
-
// eventValue?: string
|
|
18
|
-
// ];
|
|
19
|
-
// export const event = (...args: GtmEventArgs) => {
|
|
20
|
-
// if (!isUndefined(window) && !isUndefined(window.gtag)) {
|
|
21
|
-
// window.gtag("send", "event", ...args);
|
|
22
|
-
// }
|
|
23
|
-
// };
|
package/index.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export { createStorage } from "./createStorage";
|
|
2
|
-
export { getZonedDate } from "./getZonedDate";
|
|
3
|
-
export { gtagPageview } from "./gtagPageview";
|
|
4
|
-
export { isIE } from "./isIE";
|
|
5
|
-
export { isMobile } from "./isMobile";
|
|
6
|
-
export { navigateToHash } from "./navigateToHash";
|
|
7
|
-
export { navigateToHashParams } from "./navigateToHashParams";
|
|
8
|
-
export { navigateToMergedHashParams } from "./navigateToMergedHashParams";
|
|
9
|
-
export { navigateToMergedParams } from "./navigateToMergedParams";
|
|
10
|
-
export { navigateToParams } from "./navigateToParams";
|
|
11
|
-
export { navigateToUrl } from "./navigateToUrl";
|
|
12
|
-
export { navigateWithoutUrlParam } from "./navigateWithoutUrlParam";
|
|
13
|
-
export { redirectTo } from "./redirectTo";
|
|
14
|
-
export { storage } from "./storage";
|
|
15
|
-
export { storageClient } from "./storageClient";
|
package/isMobile.mjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import isServer from "@koine/utils/isServer";
|
|
2
|
-
/**
|
|
3
|
-
* @category detect
|
|
4
|
-
* @category is
|
|
5
|
-
* @see https://stackoverflow.com/a/3540295
|
|
6
|
-
*/ export function isMobile(ssrValue = true) {
|
|
7
|
-
if (isServer) {
|
|
8
|
-
return ssrValue;
|
|
9
|
-
}
|
|
10
|
-
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(window.navigator.userAgent);
|
|
11
|
-
// return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
|
|
12
|
-
// || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(navigator.userAgent.substr(0,4)
|
|
13
|
-
}
|
|
14
|
-
export default isMobile;
|
package/navigateToHash.mjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import navigateToUrl from "./navigateToUrl";
|
|
2
|
-
/**
|
|
3
|
-
* It updates the browser's location hash by replacing the history state.
|
|
4
|
-
* The non-silent standard way would simply be `location.hash = "#new-hash"`
|
|
5
|
-
*
|
|
6
|
-
* @category location
|
|
7
|
-
*/ export function navigateToHash(hash = "") {
|
|
8
|
-
const { pathname, search } = location;
|
|
9
|
-
navigateToUrl(pathname + (search ? "?" + search : "") + (hash ? "#" + hash : ""), true);
|
|
10
|
-
}
|
|
11
|
-
export default navigateToHash;
|
package/navigateToHashParams.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
2
|
-
import getUrlHashPathname from "@koine/utils/getUrlHashPathname";
|
|
3
|
-
/**
|
|
4
|
-
* It updates the `location.hash` with the given query params, it uses `location.hash`
|
|
5
|
-
* if a second argument `hash` is not provded
|
|
6
|
-
*
|
|
7
|
-
* @category location
|
|
8
|
-
*/ export function navigateToHashParams(params = {}, hash = "") {
|
|
9
|
-
const useLocation = !hash;
|
|
10
|
-
hash = hash || location.hash;
|
|
11
|
-
const hashQueryLess = getUrlHashPathname(hash);
|
|
12
|
-
const queryString = typeof params === "string" ? params : buildUrlQueryString(params);
|
|
13
|
-
const newHash = "#/" + hashQueryLess + queryString;
|
|
14
|
-
if (useLocation) {
|
|
15
|
-
location.hash = newHash;
|
|
16
|
-
}
|
|
17
|
-
return newHash;
|
|
18
|
-
}
|
|
19
|
-
export default navigateToHashParams;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import getUrlQueryParams from "@koine/utils/getUrlQueryParams";
|
|
2
|
-
import mergeUrlQueryParams from "@koine/utils/mergeUrlQueryParams";
|
|
3
|
-
import { navigateToParams } from "./navigateToParams";
|
|
4
|
-
/**
|
|
5
|
-
* Merge current URL query parameters with the given ones, it uses `history`.
|
|
6
|
-
*
|
|
7
|
-
* @category location
|
|
8
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
9
|
-
*/ export function navigateToMergedParams(params = {}, replace) {
|
|
10
|
-
return navigateToParams(mergeUrlQueryParams(getUrlQueryParams(), params), replace);
|
|
11
|
-
}
|
|
12
|
-
export default navigateToMergedParams;
|
package/navigateToParams.mjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
2
|
-
import isBrowser from "@koine/utils/isBrowser";
|
|
3
|
-
import navigateToUrl from "./navigateToUrl";
|
|
4
|
-
/**
|
|
5
|
-
* Change current URL query parameters, it uses `history`.
|
|
6
|
-
*
|
|
7
|
-
* @category location
|
|
8
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
9
|
-
* @returns The query string with initial `?`
|
|
10
|
-
*/ export function navigateToParams(params = {}, replace) {
|
|
11
|
-
const queryString = typeof params === "string" ? params : buildUrlQueryString(params);
|
|
12
|
-
if (isBrowser) {
|
|
13
|
-
navigateToUrl(location.pathname + queryString, replace);
|
|
14
|
-
}
|
|
15
|
-
return queryString;
|
|
16
|
-
}
|
|
17
|
-
export default navigateToParams;
|
package/navigateToUrl.mjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* It updates the browser's location URL with global `history` object
|
|
3
|
-
*
|
|
4
|
-
* @category location
|
|
5
|
-
*/ export function navigateToUrl(url = "", replace) {
|
|
6
|
-
if (url) {
|
|
7
|
-
history[replace ? "replaceState" : "pushState"](history.state, "", url);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
export default navigateToUrl;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import getUrlQueryParams from "@koine/utils/getUrlQueryParams";
|
|
2
|
-
import navigateToParams from "./navigateToParams";
|
|
3
|
-
/**
|
|
4
|
-
* Remove URL query parameter, it uses `history`
|
|
5
|
-
*
|
|
6
|
-
* @category location
|
|
7
|
-
* @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
|
|
8
|
-
*/ export function navigateWithoutUrlParam(paramName, replace) {
|
|
9
|
-
const params = {};
|
|
10
|
-
const currentParams = getUrlQueryParams();
|
|
11
|
-
for(const key in currentParams){
|
|
12
|
-
if (key !== paramName) {
|
|
13
|
-
params[key] = currentParams[key];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return navigateToParams(params, replace);
|
|
17
|
-
}
|
|
18
|
-
export default navigateWithoutUrlParam;
|
package/redirectTo.mjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import buildUrlQueryString from "@koine/utils/buildUrlQueryString";
|
|
2
|
-
import isBrowser from "@koine/utils/isBrowser";
|
|
3
|
-
/**
|
|
4
|
-
* Redirect to url with params {optionally}, removes eventual trailing question
|
|
5
|
-
* marks from the given URL, it uses `location`
|
|
6
|
-
*
|
|
7
|
-
* @category location
|
|
8
|
-
*/ export function redirectTo(url, params) {
|
|
9
|
-
if (isBrowser) {
|
|
10
|
-
const queryString = buildUrlQueryString(params);
|
|
11
|
-
location.href = url.replace(/\?+$/g, "") + queryString;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default redirectTo;
|
package/storage.mjs
DELETED
package/storageClient.mjs
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import isBrowser from "@koine/utils/isBrowser";
|
|
2
|
-
import isString from "@koine/utils/isString";
|
|
3
|
-
const methodsMap = {
|
|
4
|
-
g: "getItem",
|
|
5
|
-
s: "setItem",
|
|
6
|
-
r: "removeItem"
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Super minifiable `local/session Storage` client creator with SSR safety
|
|
10
|
-
*
|
|
11
|
-
* @category storage
|
|
12
|
-
*/ export const storageClient = (useSessionStorage)=>{
|
|
13
|
-
const nativeMethod = (method, key, value)=>isBrowser ? window[useSessionStorage ? "sessionStorage" : "localStorage"][methodsMap[method]](key, value) : ()=>{
|
|
14
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
15
|
-
console.warn(`[@koine/utils:storageClient]: ${useSessionStorage ? "sessionStorage" : "localStorage"} does not exists outside of browser.`);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
const get = (key, transform, defaultValue)=>{
|
|
19
|
-
let value = defaultValue ?? null;
|
|
20
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
21
|
-
if (!isBrowser) {
|
|
22
|
-
console.log(`[@koine/utils:storage] called 'get' outside of browser with default value '${JSON.stringify(defaultValue)}'.`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (isBrowser) {
|
|
26
|
-
let stored = nativeMethod("g", key);
|
|
27
|
-
if (stored) {
|
|
28
|
-
stored = transform ? transform(stored) : stored;
|
|
29
|
-
try {
|
|
30
|
-
const parsed = JSON.parse(stored);
|
|
31
|
-
if (parsed) value = parsed;
|
|
32
|
-
} catch (_e) {
|
|
33
|
-
value = stored;
|
|
34
|
-
// if (process.env["NODE_ENV"] !== "production") {
|
|
35
|
-
// console.warn(
|
|
36
|
-
// `[@koine/utils:storage]: 'get' failed to parse stored value as JSON. Plain '${stored}' value is returned.`
|
|
37
|
-
// );
|
|
38
|
-
// }
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return value;
|
|
43
|
-
};
|
|
44
|
-
const set = (key, value, transform = (value)=>value)=>{
|
|
45
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
46
|
-
if (!isBrowser) {
|
|
47
|
-
console.log(`[@koine/utils:storage] called 'set' outside of browser does not work.`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
if (isBrowser) {
|
|
51
|
-
try {
|
|
52
|
-
const transformedValue = isString(value) ? transform(value) : transform(JSON.stringify(value));
|
|
53
|
-
nativeMethod("s", key, transformedValue);
|
|
54
|
-
} catch (_e) {
|
|
55
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
56
|
-
console.warn(`[@koine/utils:createStorage]: 'set' error.`, _e);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
const remove = (key)=>{
|
|
62
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
63
|
-
if (!isBrowser) {
|
|
64
|
-
console.log(`[@koine/utils:storage] called 'remove' outside of browser does not work.`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (isBrowser) {
|
|
68
|
-
try {
|
|
69
|
-
nativeMethod("r", key);
|
|
70
|
-
} catch (_e) {
|
|
71
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
72
|
-
console.warn(`[@koine/utils:createStorage]: 'remove' error.`, _e);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
const has = (key, defaultValue)=>{
|
|
78
|
-
let value = defaultValue ?? false;
|
|
79
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
80
|
-
if (!isBrowser) {
|
|
81
|
-
console.log(`[@koine/utils:storage] called 'has' outside of browser with default value '${JSON.stringify(defaultValue)}'.`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (isBrowser) {
|
|
85
|
-
const stored = nativeMethod("g", key);
|
|
86
|
-
value = stored ?? false;
|
|
87
|
-
}
|
|
88
|
-
return value;
|
|
89
|
-
};
|
|
90
|
-
return {
|
|
91
|
-
get,
|
|
92
|
-
set,
|
|
93
|
-
remove,
|
|
94
|
-
has
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
export default storageClient;
|