@koine/utils 1.0.34 → 1.0.35

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.
@@ -0,0 +1,13 @@
1
+ export declare type CreateStorageConfig = Record<string, any>;
2
+ export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T>) => {
3
+ get<TKey extends keyof T>(key: TKey): T[TKey] | null;
4
+ set<TKey_1 extends keyof T>(key: TKey_1, value: T[TKey_1]): void;
5
+ has<TKey_2 extends keyof T>(key: TKey_2): boolean;
6
+ remove<TKey_3 extends keyof T>(key: TKey_3): void;
7
+ clear(): void;
8
+ /**
9
+ * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
10
+ */
11
+ watch: <TKey_4 extends keyof T>(keyToWatch: TKey_4, onRemoved?: () => void, onAdded?: () => void) => () => void;
12
+ };
13
+ export default createStorage;
@@ -0,0 +1,75 @@
1
+ import { __assign } from "tslib";
2
+ import { decode } from "./decode";
3
+ import { encode } from "./encode";
4
+ import isBrowser from "./isBrowser";
5
+ import { isString } from "./isString";
6
+ export var createStorage = function (config) {
7
+ var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
8
+ /**
9
+ * Super minifiable localStorage wrapper with SSR safety
10
+ */
11
+ var ls = function (method, key, value) {
12
+ return isBrowser
13
+ ? localStorage[methodsMap[method]](key, value)
14
+ : function () {
15
+ if (process.env["NODE_ENV"] !== "production") {
16
+ console.warn("[@koine/utils] createStorage: localStorage does not exists in this environment.");
17
+ }
18
+ };
19
+ };
20
+ var keys = Object.keys(config).reduce(function (map, key) {
21
+ var _a;
22
+ return (__assign(__assign({}, map), (_a = {}, _a[key] = encode(key), _a)));
23
+ }, {});
24
+ return {
25
+ get: function (key) {
26
+ var stored = ls("g", keys[key]);
27
+ if (stored) {
28
+ stored = decode(stored);
29
+ try {
30
+ return JSON.parse(stored);
31
+ }
32
+ catch (_e) {
33
+ return stored;
34
+ }
35
+ }
36
+ return null;
37
+ },
38
+ set: function (key, value) {
39
+ ls("s", keys[key], isString(value) ? encode(value) : JSON.stringify(value));
40
+ },
41
+ has: function (key) {
42
+ var stored = ls("g", keys[key]);
43
+ return !!stored;
44
+ },
45
+ remove: function (key) {
46
+ ls("r", keys[key]);
47
+ },
48
+ clear: function () {
49
+ for (var key in keys) {
50
+ ls("r", keys[key]);
51
+ }
52
+ },
53
+ /**
54
+ * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
55
+ */
56
+ watch: function (keyToWatch, onRemoved, onAdded) {
57
+ var handler = function (event) {
58
+ var key = event.key, oldValue = event.oldValue, newValue = event.newValue;
59
+ if (key === keys[keyToWatch]) {
60
+ if (oldValue && !newValue) {
61
+ onRemoved === null || onRemoved === void 0 ? void 0 : onRemoved();
62
+ }
63
+ else if (!oldValue && newValue) {
64
+ onAdded === null || onAdded === void 0 ? void 0 : onAdded();
65
+ }
66
+ }
67
+ };
68
+ window.addEventListener("storage", handler);
69
+ return function () {
70
+ window.removeEventListener("storage", handler);
71
+ };
72
+ },
73
+ };
74
+ };
75
+ export default createStorage;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createStorage = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var decode_1 = require("./decode");
6
+ var encode_1 = require("./encode");
7
+ var isBrowser_1 = require("./isBrowser");
8
+ var isString_1 = require("./isString");
9
+ var createStorage = function (config) {
10
+ var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
11
+ /**
12
+ * Super minifiable localStorage wrapper with SSR safety
13
+ */
14
+ var ls = function (method, key, value) {
15
+ return isBrowser_1.default
16
+ ? localStorage[methodsMap[method]](key, value)
17
+ : function () {
18
+ if (process.env["NODE_ENV"] !== "production") {
19
+ console.warn("[@koine/utils] createStorage: localStorage does not exists in this environment.");
20
+ }
21
+ };
22
+ };
23
+ var keys = Object.keys(config).reduce(function (map, key) {
24
+ var _a;
25
+ return (tslib_1.__assign(tslib_1.__assign({}, map), (_a = {}, _a[key] = (0, encode_1.encode)(key), _a)));
26
+ }, {});
27
+ return {
28
+ get: function (key) {
29
+ var stored = ls("g", keys[key]);
30
+ if (stored) {
31
+ stored = (0, decode_1.decode)(stored);
32
+ try {
33
+ return JSON.parse(stored);
34
+ }
35
+ catch (_e) {
36
+ return stored;
37
+ }
38
+ }
39
+ return null;
40
+ },
41
+ set: function (key, value) {
42
+ ls("s", keys[key], (0, isString_1.isString)(value) ? (0, encode_1.encode)(value) : JSON.stringify(value));
43
+ },
44
+ has: function (key) {
45
+ var stored = ls("g", keys[key]);
46
+ return !!stored;
47
+ },
48
+ remove: function (key) {
49
+ ls("r", keys[key]);
50
+ },
51
+ clear: function () {
52
+ for (var key in keys) {
53
+ ls("r", keys[key]);
54
+ }
55
+ },
56
+ /**
57
+ * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
58
+ */
59
+ watch: function (keyToWatch, onRemoved, onAdded) {
60
+ var handler = function (event) {
61
+ var key = event.key, oldValue = event.oldValue, newValue = event.newValue;
62
+ if (key === keys[keyToWatch]) {
63
+ if (oldValue && !newValue) {
64
+ onRemoved === null || onRemoved === void 0 ? void 0 : onRemoved();
65
+ }
66
+ else if (!oldValue && newValue) {
67
+ onAdded === null || onAdded === void 0 ? void 0 : onAdded();
68
+ }
69
+ }
70
+ };
71
+ window.addEventListener("storage", handler);
72
+ return function () {
73
+ window.removeEventListener("storage", handler);
74
+ };
75
+ },
76
+ };
77
+ };
78
+ exports.createStorage = createStorage;
79
+ exports.default = exports.createStorage;
@@ -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:cookie readCookie, document is undefined");
13
+ console.warn("[@koine/utils] readCookie: document is undefined");
14
14
  }
15
15
  return name ? "" : {};
16
16
  }
@@ -28,7 +28,7 @@ function readCookie(name) {
28
28
  }
29
29
  catch (e) {
30
30
  if (process.env["NODE_ENV"] !== "production") {
31
- console.warn("@koine/utils:cookie readCookie, failed to decode", value);
31
+ console.warn("[@koine/utils] readCookie: failed to decode", value);
32
32
  }
33
33
  }
34
34
  }
package/node/setCookie.js CHANGED
@@ -21,7 +21,7 @@ function setCookie(name, value, attributes) {
21
21
  var cleanedAttrs = tslib_1.__assign(tslib_1.__assign({ expires: "" }, cookie_1.defaultAttributesClient), restAttrs);
22
22
  if (typeof document === "undefined") {
23
23
  if (process.env["NODE_ENV"] !== "production") {
24
- console.warn("@koine/utils:cookie setCookie, document is undefined");
24
+ console.warn("[@koine/utils] cookie setCookie: document is undefined");
25
25
  }
26
26
  return;
27
27
  }
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "peerDependencies": {
8
8
  "tslib": "^2.4.0"
9
9
  },
10
- "version": "1.0.34",
10
+ "version": "1.0.35",
11
11
  "module": "./index.js",
12
12
  "types": "./index.d.ts"
13
13
  }
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:cookie readCookie, document is undefined");
10
+ console.warn("[@koine/utils] readCookie: document is undefined");
11
11
  }
12
12
  return name ? "" : {};
13
13
  }
@@ -25,7 +25,7 @@ export function readCookie(name) {
25
25
  }
26
26
  catch (e) {
27
27
  if (process.env["NODE_ENV"] !== "production") {
28
- console.warn("@koine/utils:cookie readCookie, failed to decode", value);
28
+ console.warn("[@koine/utils] readCookie: failed to decode", value);
29
29
  }
30
30
  }
31
31
  }
package/setCookie.js CHANGED
@@ -18,7 +18,7 @@ export function setCookie(name, value, attributes) {
18
18
  var cleanedAttrs = __assign(__assign({ expires: "" }, defaultAttributesClient), restAttrs);
19
19
  if (typeof document === "undefined") {
20
20
  if (process.env["NODE_ENV"] !== "production") {
21
- console.warn("@koine/utils:cookie setCookie, document is undefined");
21
+ console.warn("[@koine/utils] cookie setCookie: document is undefined");
22
22
  }
23
23
  return;
24
24
  }