@koine/browser 2.0.0-beta.2 → 2.0.0-beta.21

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.
Files changed (54) hide show
  1. package/createStorage.d.ts +0 -42
  2. package/createStorage.js +24 -74
  3. package/getZonedDate.d.ts +0 -14
  4. package/getZonedDate.js +7 -28
  5. package/gtagPageview.d.ts +0 -3
  6. package/gtagPageview.js +4 -24
  7. package/index.d.ts +17 -15
  8. package/index.js +17 -33
  9. package/isIE.d.ts +0 -5
  10. package/isIE.js +4 -14
  11. package/isMobile.d.ts +0 -5
  12. package/isMobile.js +4 -16
  13. package/listenUrlSearch.d.ts +3 -0
  14. package/listenUrlSearch.js +55 -0
  15. package/listenUrlSearchParams.d.ts +2 -0
  16. package/listenUrlSearchParams.js +12 -0
  17. package/navigateToHash.d.ts +0 -6
  18. package/navigateToHash.js +4 -15
  19. package/navigateToHashParams.d.ts +1 -7
  20. package/navigateToHashParams.js +5 -17
  21. package/navigateToMergedHashParams.d.ts +1 -6
  22. package/navigateToMergedHashParams.js +5 -16
  23. package/navigateToMergedParams.d.ts +1 -7
  24. package/navigateToMergedParams.js +5 -17
  25. package/navigateToParams.d.ts +1 -8
  26. package/navigateToParams.js +7 -20
  27. package/navigateToUrl.d.ts +0 -5
  28. package/navigateToUrl.js +2 -11
  29. package/navigateWithoutUrlParam.d.ts +0 -6
  30. package/navigateWithoutUrlParam.js +6 -17
  31. package/package.json +62 -12
  32. package/redirectTo.d.ts +1 -7
  33. package/redirectTo.js +5 -17
  34. package/storage.d.ts +0 -3
  35. package/storage.js +5 -12
  36. package/storageClient.d.ts +0 -3
  37. package/storageClient.js +20 -34
  38. package/README.md +0 -1
  39. package/createStorage.mjs +0 -143
  40. package/getZonedDate.mjs +0 -37
  41. package/gtagPageview.mjs +0 -30
  42. package/index.mjs +0 -15
  43. package/isIE.mjs +0 -18
  44. package/isMobile.mjs +0 -16
  45. package/navigateToHash.mjs +0 -13
  46. package/navigateToHashParams.mjs +0 -22
  47. package/navigateToMergedHashParams.mjs +0 -14
  48. package/navigateToMergedParams.mjs +0 -14
  49. package/navigateToParams.mjs +0 -19
  50. package/navigateToUrl.mjs +0 -12
  51. package/navigateWithoutUrlParam.mjs +0 -19
  52. package/redirectTo.mjs +0 -15
  53. package/storage.mjs +0 -9
  54. package/storageClient.mjs +0 -103
@@ -1,27 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.navigateToHashParams = void 0;
4
- var tslib_1 = require("tslib");
5
- var buildUrlQueryString_1 = tslib_1.__importDefault(require("@koine/utils/buildUrlQueryString"));
6
- var getUrlHashPathname_1 = tslib_1.__importDefault(require("@koine/utils/getUrlHashPathname"));
7
- /**
8
- * It updates the `location.hash` with the given query params, it uses `location.hash`
9
- * if a second argument `hash` is not provded
10
- *
11
- * @category location
12
- */
13
- function navigateToHashParams(params, hash) {
1
+ import { buildUrlQueryString, getUrlHashPathname, } from "@koine/utils";
2
+ export function navigateToHashParams(params, hash) {
14
3
  if (params === void 0) { params = {}; }
15
4
  if (hash === void 0) { hash = ""; }
16
5
  var useLocation = !hash;
17
6
  hash = hash || location.hash;
18
- var hashQueryLess = (0, getUrlHashPathname_1.default)(hash);
19
- var queryString = typeof params === "string" ? params : (0, buildUrlQueryString_1.default)(params);
7
+ var hashQueryLess = getUrlHashPathname(hash);
8
+ var queryString = typeof params === "string" ? params : buildUrlQueryString(params);
20
9
  var newHash = "#/" + hashQueryLess + queryString;
21
10
  if (useLocation) {
22
11
  location.hash = newHash;
23
12
  }
24
13
  return newHash;
25
14
  }
26
- exports.navigateToHashParams = navigateToHashParams;
27
- exports.default = navigateToHashParams;
15
+ export default navigateToHashParams;
@@ -1,8 +1,3 @@
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
- */
1
+ import { type AnyQueryParams } from "@koine/utils";
7
2
  export declare function navigateToMergedHashParams(params?: NonNullable<AnyQueryParams>, hash?: string): string;
8
3
  export default navigateToMergedHashParams;
@@ -1,19 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.navigateToMergedHashParams = void 0;
4
- var tslib_1 = require("tslib");
5
- var mergeUrlQueryParams_1 = tslib_1.__importDefault(require("@koine/utils/mergeUrlQueryParams"));
6
- var getUrlHashParams_1 = tslib_1.__importDefault(require("@koine/utils/getUrlHashParams"));
7
- var navigateToHashParams_1 = require("./navigateToHashParams");
8
- /**
9
- * It updates the "query params" within the `location.hash`, it uses `location`
10
- *
11
- * @category location
12
- */
13
- function navigateToMergedHashParams(params, hash) {
1
+ import { getUrlHashParams, mergeUrlQueryParams, } from "@koine/utils";
2
+ import { navigateToHashParams } from "./navigateToHashParams.js";
3
+ export function navigateToMergedHashParams(params, hash) {
14
4
  if (params === void 0) { params = {}; }
15
5
  if (hash === void 0) { hash = ""; }
16
- return (0, navigateToHashParams_1.navigateToHashParams)((0, mergeUrlQueryParams_1.default)((0, getUrlHashParams_1.default)(hash), params), hash);
6
+ return navigateToHashParams(mergeUrlQueryParams(getUrlHashParams(hash), params), hash);
17
7
  }
18
- exports.navigateToMergedHashParams = navigateToMergedHashParams;
19
- exports.default = navigateToMergedHashParams;
8
+ export default navigateToMergedHashParams;
@@ -1,9 +1,3 @@
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
- */
1
+ import { type AnyQueryParams } from "@koine/utils";
8
2
  export declare function navigateToMergedParams(params?: NonNullable<AnyQueryParams>, replace?: boolean): string;
9
3
  export default navigateToMergedParams;
@@ -1,19 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.navigateToMergedParams = void 0;
4
- var tslib_1 = require("tslib");
5
- var mergeUrlQueryParams_1 = tslib_1.__importDefault(require("@koine/utils/mergeUrlQueryParams"));
6
- var getUrlQueryParams_1 = tslib_1.__importDefault(require("@koine/utils/getUrlQueryParams"));
7
- var navigateToParams_1 = require("./navigateToParams");
8
- /**
9
- * Merge current URL query parameters with the given ones, it uses `history`.
10
- *
11
- * @category location
12
- * @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
13
- */
14
- function navigateToMergedParams(params, replace) {
1
+ import { getUrlQueryParams, mergeUrlQueryParams, } from "@koine/utils";
2
+ import { navigateToParams } from "./navigateToParams.js";
3
+ export function navigateToMergedParams(params, replace) {
15
4
  if (params === void 0) { params = {}; }
16
- return (0, navigateToParams_1.navigateToParams)((0, mergeUrlQueryParams_1.default)((0, getUrlQueryParams_1.default)(), params), replace);
5
+ return navigateToParams(mergeUrlQueryParams(getUrlQueryParams(), params), replace);
17
6
  }
18
- exports.navigateToMergedParams = navigateToMergedParams;
19
- exports.default = navigateToMergedParams;
7
+ export default navigateToMergedParams;
@@ -1,10 +1,3 @@
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
- */
1
+ import { type AnyQueryParams } from "@koine/utils";
9
2
  export declare function navigateToParams(params?: string | AnyQueryParams, replace?: boolean): string;
10
3
  export default navigateToParams;
@@ -1,24 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.navigateToParams = void 0;
4
- var tslib_1 = require("tslib");
5
- var isBrowser_1 = tslib_1.__importDefault(require("@koine/utils/isBrowser"));
6
- var buildUrlQueryString_1 = tslib_1.__importDefault(require("@koine/utils/buildUrlQueryString"));
7
- var navigateToUrl_1 = tslib_1.__importDefault(require("./navigateToUrl"));
8
- /**
9
- * Change current URL query parameters, it uses `history`.
10
- *
11
- * @category location
12
- * @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
13
- * @returns The query string with initial `?`
14
- */
15
- function navigateToParams(params, replace) {
1
+ import { buildUrlQueryString, isBrowser, } from "@koine/utils";
2
+ import navigateToUrl from "./navigateToUrl.js";
3
+ export function navigateToParams(params, replace) {
16
4
  if (params === void 0) { params = {}; }
17
- var queryString = typeof params === "string" ? params : (0, buildUrlQueryString_1.default)(params);
18
- if (isBrowser_1.default) {
19
- (0, navigateToUrl_1.default)(location.pathname + queryString, replace);
5
+ var queryString = typeof params === "string" ? params : buildUrlQueryString(params);
6
+ if (isBrowser) {
7
+ navigateToUrl(location.pathname + queryString, replace);
20
8
  }
21
9
  return queryString;
22
10
  }
23
- exports.navigateToParams = navigateToParams;
24
- exports.default = navigateToParams;
11
+ export default navigateToParams;
@@ -1,7 +1,2 @@
1
- /**
2
- * It updates the browser's location URL with global `history` object
3
- *
4
- * @category location
5
- */
6
1
  export declare function navigateToUrl(url?: string, replace?: boolean): void;
7
2
  export default navigateToUrl;
package/navigateToUrl.js CHANGED
@@ -1,16 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.navigateToUrl = void 0;
4
- /**
5
- * It updates the browser's location URL with global `history` object
6
- *
7
- * @category location
8
- */
9
- function navigateToUrl(url, replace) {
1
+ export function navigateToUrl(url, replace) {
10
2
  if (url === void 0) { url = ""; }
11
3
  if (url) {
12
4
  history[replace ? "replaceState" : "pushState"](history.state, "", url);
13
5
  }
14
6
  }
15
- exports.navigateToUrl = navigateToUrl;
16
- exports.default = navigateToUrl;
7
+ export default navigateToUrl;
@@ -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;
@@ -1,24 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.navigateWithoutUrlParam = void 0;
4
- var tslib_1 = require("tslib");
5
- var getUrlQueryParams_1 = tslib_1.__importDefault(require("@koine/utils/getUrlQueryParams"));
6
- var navigateToParams_1 = tslib_1.__importDefault(require("./navigateToParams"));
7
- /**
8
- * Remove URL query parameter, it uses `history`
9
- *
10
- * @category location
11
- * @param replace Replace URL instead of pushing it in the history stack. By default it pushes it.
12
- */
13
- function navigateWithoutUrlParam(paramName, replace) {
1
+ import { getUrlQueryParams } from "@koine/utils";
2
+ import navigateToParams from "./navigateToParams.js";
3
+ export function navigateWithoutUrlParam(paramName, replace) {
14
4
  var params = {};
15
- var currentParams = (0, getUrlQueryParams_1.default)();
5
+ var currentParams = getUrlQueryParams();
16
6
  for (var key in currentParams) {
17
7
  if (key !== paramName) {
18
8
  params[key] = currentParams[key];
19
9
  }
20
10
  }
21
- return (0, navigateToParams_1.default)(params, replace);
11
+ return navigateToParams(params, replace);
22
12
  }
23
- exports.navigateWithoutUrlParam = navigateWithoutUrlParam;
24
- exports.default = navigateWithoutUrlParam;
13
+ export default navigateWithoutUrlParam;
package/package.json CHANGED
@@ -2,18 +2,68 @@
2
2
  "name": "@koine/browser",
3
3
  "sideEffects": false,
4
4
  "dependencies": {
5
- "clsx": "1.2.1",
6
- "date-fns-tz": "^2.0.0",
7
- "ts-debounce": "^4.0.0",
8
- "type-fest": "^3.11.1"
5
+ "@koine/dom": "2.0.0-beta.21",
6
+ "@koine/utils": "2.0.0-beta.21"
9
7
  },
10
- "peerDependencies": {
11
- "@koine/dom": "2.0.0-beta.2",
12
- "@koine/utils": "2.0.0-beta.2",
13
- "tslib": "2.5.3"
14
- },
15
- "main": "./index.js",
16
8
  "types": "./index.d.ts",
17
- "version": "2.0.0-beta.2",
18
- "module": "./index.mjs"
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
+ "./listenUrlSearch": {
31
+ "import": "./listenUrlSearch.js"
32
+ },
33
+ "./listenUrlSearchParams": {
34
+ "import": "./listenUrlSearchParams.js"
35
+ },
36
+ "./navigateToHash": {
37
+ "import": "./navigateToHash.js"
38
+ },
39
+ "./navigateToHashParams": {
40
+ "import": "./navigateToHashParams.js"
41
+ },
42
+ "./navigateToMergedHashParams": {
43
+ "import": "./navigateToMergedHashParams.js"
44
+ },
45
+ "./navigateToMergedParams": {
46
+ "import": "./navigateToMergedParams.js"
47
+ },
48
+ "./navigateToParams": {
49
+ "import": "./navigateToParams.js"
50
+ },
51
+ "./navigateToUrl": {
52
+ "import": "./navigateToUrl.js"
53
+ },
54
+ "./navigateWithoutUrlParam": {
55
+ "import": "./navigateWithoutUrlParam.js"
56
+ },
57
+ "./redirectTo": {
58
+ "import": "./redirectTo.js"
59
+ },
60
+ "./storage": {
61
+ "import": "./storage.js"
62
+ },
63
+ "./storageClient": {
64
+ "import": "./storageClient.js"
65
+ }
66
+ },
67
+ "peerDependencies": {},
68
+ "version": "2.0.0-beta.21"
19
69
  }
package/redirectTo.d.ts CHANGED
@@ -1,9 +1,3 @@
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
- */
1
+ import { type AnyQueryParams } from "@koine/utils";
8
2
  export declare function redirectTo(url: string, params?: AnyQueryParams): void;
9
3
  export default redirectTo;
package/redirectTo.js CHANGED
@@ -1,20 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.redirectTo = void 0;
4
- var tslib_1 = require("tslib");
5
- var isBrowser_1 = tslib_1.__importDefault(require("@koine/utils/isBrowser"));
6
- var buildUrlQueryString_1 = tslib_1.__importDefault(require("@koine/utils/buildUrlQueryString"));
7
- /**
8
- * Redirect to url with params {optionally}, removes eventual trailing question
9
- * marks from the given URL, it uses `location`
10
- *
11
- * @category location
12
- */
13
- function redirectTo(url, params) {
14
- if (isBrowser_1.default) {
15
- var queryString = (0, buildUrlQueryString_1.default)(params);
1
+ import { buildUrlQueryString, isBrowser, } from "@koine/utils";
2
+ export function redirectTo(url, params) {
3
+ if (isBrowser) {
4
+ var queryString = buildUrlQueryString(params);
16
5
  location.href = url.replace(/\?+$/g, "") + queryString;
17
6
  }
18
7
  }
19
- exports.redirectTo = redirectTo;
20
- exports.default = redirectTo;
8
+ export default redirectTo;
package/storage.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- /**
2
- * Storage, for `localStorage` and `sessionStorage`
3
- */
4
1
  export declare const storage: {
5
2
  l: {
6
3
  get: <TKey extends string, TValue = any>(key: TKey, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
package/storage.js CHANGED
@@ -1,13 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.storage = void 0;
4
- var tslib_1 = require("tslib");
5
- var storageClient_1 = tslib_1.__importDefault(require("./storageClient"));
6
- /**
7
- * Storage, for `localStorage` and `sessionStorage`
8
- */
9
- exports.storage = {
10
- l: (0, storageClient_1.default)(),
11
- s: (0, storageClient_1.default)(true),
1
+ import storageClient from "./storageClient.js";
2
+ export var storage = {
3
+ l: storageClient(),
4
+ s: storageClient(true),
12
5
  };
13
- exports.default = exports.storage;
6
+ export default storage;
@@ -1,7 +1,4 @@
1
1
  export type StorageClientConfig = Record<string, any>;
2
- /**
3
- * Super minifiable `local/session Storage` client creator with SSR safety
4
- */
5
2
  export declare const storageClient: <TConfig extends StorageClientConfig = StorageClientConfig>(useSessionStorage?: boolean) => {
6
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;
7
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 CHANGED
@@ -1,31 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.storageClient = void 0;
4
- var tslib_1 = require("tslib");
5
- var isBrowser_1 = tslib_1.__importDefault(require("@koine/utils/isBrowser"));
6
- var isString_1 = tslib_1.__importDefault(require("@koine/utils/isString"));
1
+ import { isBrowser, isString } from "@koine/utils";
7
2
  var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
8
- /**
9
- * Super minifiable `local/session Storage` client creator with SSR safety
10
- */
11
- var storageClient = function (useSessionStorage) {
3
+ export var storageClient = function (useSessionStorage) {
12
4
  var nativeMethod = function (method, key, value) {
13
- return isBrowser_1.default
5
+ return isBrowser
14
6
  ? window[useSessionStorage ? "sessionStorage" : "localStorage"][methodsMap[method]](key, value)
15
7
  : function () {
16
- if (process.env["NODE_ENV"] !== "production") {
8
+ if (process.env["NODE_ENV"] === "development") {
17
9
  console.warn("[@koine/utils:storageClient]: ".concat(useSessionStorage ? "sessionStorage" : "localStorage", " does not exists outside of browser."));
18
10
  }
19
11
  };
20
12
  };
21
13
  var get = function (key, transform, defaultValue) {
22
14
  var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
23
- if (process.env["NODE_ENV"] !== "production") {
24
- if (!isBrowser_1.default) {
15
+ if (process.env["NODE_ENV"] === "development") {
16
+ if (!isBrowser) {
25
17
  console.log("[@koine/utils:storage] called 'get' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
26
18
  }
27
19
  }
28
- if (isBrowser_1.default) {
20
+ if (isBrowser) {
29
21
  var stored = nativeMethod("g", key);
30
22
  if (stored) {
31
23
  stored = transform ? transform(stored) : stored;
@@ -36,11 +28,6 @@ var storageClient = function (useSessionStorage) {
36
28
  }
37
29
  catch (_e) {
38
30
  value = stored;
39
- // if (process.env["NODE_ENV"] !== "production") {
40
- // console.warn(
41
- // `[@koine/utils:storage]: 'get' failed to parse stored value as JSON. Plain '${stored}' value is returned.`
42
- // );
43
- // }
44
31
  }
45
32
  }
46
33
  }
@@ -48,37 +35,37 @@ var storageClient = function (useSessionStorage) {
48
35
  };
49
36
  var set = function (key, value, transform) {
50
37
  if (transform === void 0) { transform = function (value) { return value; }; }
51
- if (process.env["NODE_ENV"] !== "production") {
52
- if (!isBrowser_1.default) {
38
+ if (process.env["NODE_ENV"] === "development") {
39
+ if (!isBrowser) {
53
40
  console.log("[@koine/utils:storage] called 'set' outside of browser does not work.");
54
41
  }
55
42
  }
56
- if (isBrowser_1.default) {
43
+ if (isBrowser) {
57
44
  try {
58
- var transformedValue = (0, isString_1.default)(value)
45
+ var transformedValue = isString(value)
59
46
  ? transform(value)
60
47
  : transform(JSON.stringify(value));
61
48
  nativeMethod("s", key, transformedValue);
62
49
  }
63
50
  catch (_e) {
64
- if (process.env["NODE_ENV"] !== "production") {
51
+ if (process.env["NODE_ENV"] === "development") {
65
52
  console.warn("[@koine/utils:createStorage]: 'set' error.", _e);
66
53
  }
67
54
  }
68
55
  }
69
56
  };
70
57
  var remove = function (key) {
71
- if (process.env["NODE_ENV"] !== "production") {
72
- if (!isBrowser_1.default) {
58
+ if (process.env["NODE_ENV"] === "development") {
59
+ if (!isBrowser) {
73
60
  console.log("[@koine/utils:storage] called 'remove' outside of browser does not work.");
74
61
  }
75
62
  }
76
- if (isBrowser_1.default) {
63
+ if (isBrowser) {
77
64
  try {
78
65
  nativeMethod("r", key);
79
66
  }
80
67
  catch (_e) {
81
- if (process.env["NODE_ENV"] !== "production") {
68
+ if (process.env["NODE_ENV"] === "development") {
82
69
  console.warn("[@koine/utils:createStorage]: 'remove' error.", _e);
83
70
  }
84
71
  }
@@ -86,12 +73,12 @@ var storageClient = function (useSessionStorage) {
86
73
  };
87
74
  var has = function (key, defaultValue) {
88
75
  var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : false;
89
- if (process.env["NODE_ENV"] !== "production") {
90
- if (!isBrowser_1.default) {
76
+ if (process.env["NODE_ENV"] === "development") {
77
+ if (!isBrowser) {
91
78
  console.log("[@koine/utils:storage] called 'has' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
92
79
  }
93
80
  }
94
- if (isBrowser_1.default) {
81
+ if (isBrowser) {
95
82
  var stored = nativeMethod("g", key);
96
83
  value = stored !== null && stored !== void 0 ? stored : false;
97
84
  }
@@ -104,5 +91,4 @@ var storageClient = function (useSessionStorage) {
104
91
  has: has,
105
92
  };
106
93
  };
107
- exports.storageClient = storageClient;
108
- exports.default = exports.storageClient;
94
+ export default storageClient;
package/README.md DELETED
@@ -1 +0,0 @@
1
- # @koine/browser
package/createStorage.mjs DELETED
@@ -1,143 +0,0 @@
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";
9
- /**
10
- * Utility to create a storage instance to interact with `localStorage` using
11
- * encrypted (encoded) key/values.
12
- */
13
- export var createStorage = function (config, useSessionStorage) {
14
- var client = useSessionStorage ? storage.s : storage.l;
15
- var keys = Object.keys(config).reduce(function (map, key) {
16
- var _a;
17
- return (__assign(__assign({}, map), (_a = {}, _a[key] = encode(key), _a)));
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
- */
27
- get: function (key, defaultValue) {
28
- return client.get(keys[key], decode, defaultValue);
29
- },
30
- /**
31
- * Get all storage values (it uses `localStorage.get()`).
32
- *
33
- * `undefined` and `null` values are not returned.
34
- */
35
- getAll: function (defaultValues) {
36
- if (!isBrowser) {
37
- if (process.env["NODE_ENV"] !== "production") {
38
- console.log("[@koine/utils:createStorage] attempt to use 'getAll' outside of browser.");
39
- }
40
- return {};
41
- }
42
- var all = {};
43
- for (var key in keys) {
44
- var value = this.get(key);
45
- var defaultValue = defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues[key];
46
- if (!isNullOrUndefined(value)) {
47
- all[key] = value;
48
- }
49
- else if (defaultValue) {
50
- all[key] = defaultValue;
51
- }
52
- }
53
- return all;
54
- },
55
- /**
56
- * Set a storage value (it uses `localStorage.set()`).
57
- *
58
- * Non-string values are stringified with `JSON.stringify()`
59
- */
60
- set: function (key, value) {
61
- client.set(keys[key], value, encode);
62
- },
63
- /**
64
- * Set all given storage values (it uses `localStorage.set()`).
65
- *
66
- * Non-string values are stringified with `JSON.stringify()`, `undefined`
67
- * and `null` values are removed from the storage
68
- */
69
- setMany: function (newValues) {
70
- if (process.env["NODE_ENV"] !== "production") {
71
- if (!isBrowser) {
72
- console.log("[@koine/utils:createStorage] attempt to use 'setMany' outside of browser.");
73
- }
74
- }
75
- if (isBrowser) {
76
- for (var key in newValues) {
77
- var value = newValues[key];
78
- if (!isNullOrUndefined(value)) {
79
- this.set(key, value);
80
- }
81
- else {
82
- this.remove(key);
83
- }
84
- }
85
- }
86
- },
87
- /**
88
- * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
89
- */
90
- has: function (key) {
91
- return client.has(keys[key]);
92
- },
93
- /**
94
- * Remove a storage value (it uses `localStorage.remove()`).
95
- */
96
- remove: function (key) {
97
- client.remove(keys[key]);
98
- },
99
- /**
100
- * Clear all storage values (it uses `localStorage.remove()`).
101
- */
102
- clear: function () {
103
- if (process.env["NODE_ENV"] !== "production") {
104
- if (!isBrowser) {
105
- console.log("[@koine/utils:createStorage] attempt to use 'clear' outside of browser.");
106
- }
107
- }
108
- if (isBrowser) {
109
- for (var key in keys) {
110
- client.remove(keys[key]);
111
- }
112
- }
113
- },
114
- /**
115
- * Watch a storage value changes, this needs to be executed only in browser
116
- * context (it uses `window.addEventListener("storage")`).
117
- *
118
- * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
119
- */
120
- watch: function (keyToWatch, onRemoved, onAdded) {
121
- if (!isBrowser) {
122
- if (process.env["NODE_ENV"] !== "production") {
123
- console.log("[@koine/utils:createStorage] attempt to use 'watch' outside of browser.");
124
- }
125
- return noop;
126
- }
127
- var handler = function (event) {
128
- var key = event.key, oldValue = event.oldValue, newValue = event.newValue;
129
- if (key === keys[keyToWatch]) {
130
- if (oldValue && !newValue) {
131
- onRemoved === null || onRemoved === void 0 ? void 0 : onRemoved();
132
- }
133
- else if (!oldValue && newValue) {
134
- onAdded === null || onAdded === void 0 ? void 0 : onAdded();
135
- }
136
- }
137
- };
138
- var listener = on(window, "storage", handler);
139
- return listener;
140
- },
141
- };
142
- };
143
- export default createStorage;