@koine/utils 1.0.70 → 1.0.73

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.
@@ -8,5 +8,5 @@ import { type AnyQueryParams } from "./location";
8
8
  *
9
9
  * @category location
10
10
  */
11
- export declare function buildUrlQueryString(params?: AnyQueryParams): string;
11
+ export declare function buildUrlQueryString<T extends AnyQueryParams>(params: T): string;
12
12
  export default buildUrlQueryString;
@@ -1,5 +1,6 @@
1
1
  import isNull from "./isNull";
2
2
  import isUndefined from "./isUndefined";
3
+ import isArray from "./isArray";
3
4
  /**
4
5
  * Get clean query string for URL
5
6
  *
@@ -10,11 +11,17 @@ import isUndefined from "./isUndefined";
10
11
  * @category location
11
12
  */
12
13
  export function buildUrlQueryString(params) {
13
- if (params === void 0) { params = {}; }
14
14
  var output = "";
15
+ if (!params)
16
+ return output;
15
17
  for (var key in params) {
16
18
  var value = params[key];
17
- if (!isNull(value) && !isUndefined(value)) {
19
+ if (isArray(value)) {
20
+ for (var i = 0; i < value.length; i++) {
21
+ output += "".concat(key, "=").concat(encodeURIComponent(value[i] + ""), "&");
22
+ }
23
+ }
24
+ else if (!isNull(value) && !isUndefined(value)) {
18
25
  output += "".concat(key, "=").concat(encodeURIComponent(value + ""), "&");
19
26
  }
20
27
  }
@@ -1,11 +1,51 @@
1
1
  export declare type CreateStorageConfig = Record<string, any>;
2
+ /**
3
+ * Utility to create a storage instance to interact with `localStorage` using
4
+ * encrypted (encoded) key/values.
5
+ */
2
6
  export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T>) => {
7
+ /**
8
+ * Get all storage value (it uses `localStorage.get()`).
9
+ *
10
+ * Unparseable values with `JSON.parse()` return their value as it is.
11
+ * If the given `key` argument is not found `null` is returned.
12
+ */
3
13
  get<TKey extends keyof T>(key: TKey): T[TKey] | null;
14
+ /**
15
+ * Get all storage values (it uses `localStorage.get()`).
16
+ *
17
+ * `undefined` and `null` values are not returned.
18
+ */
19
+ getAll(): T;
20
+ /**
21
+ * Set a storage value (it uses `localStorage.set()`).
22
+ *
23
+ * Non-string values are stringified with `JSON.stringify()`
24
+ */
4
25
  set<TKey_1 extends keyof T>(key: TKey_1, value: T[TKey_1]): void;
26
+ /**
27
+ * Set all given storage values (it uses `localStorage.set()`).
28
+ *
29
+ * Non-string values are stringified with `JSON.stringify()`, `undefined`
30
+ * and `null` values are removed from the storage
31
+ */
32
+ setMany(newValues: Partial<T>): void;
33
+ /**
34
+ * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
35
+ */
5
36
  has<TKey_2 extends keyof T>(key: TKey_2): boolean;
37
+ /**
38
+ * Remove a storage value (it uses `localStorage.remove()`).
39
+ */
6
40
  remove<TKey_3 extends keyof T>(key: TKey_3): void;
41
+ /**
42
+ * Clear all storage values (it uses `localStorage.remove()`).
43
+ */
7
44
  clear(): void;
8
45
  /**
46
+ * Watch a storage value changes, this needs to be executed only in browser
47
+ * context (it uses `window.addEventListener("storage")`).
48
+ *
9
49
  * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
10
50
  */
11
51
  watch: <TKey_4 extends keyof T>(keyToWatch: TKey_4, onRemoved?: () => void, onAdded?: () => void) => () => void;
package/createStorage.js CHANGED
@@ -1,8 +1,13 @@
1
1
  import { __assign } from "tslib";
2
2
  import { decode } from "./decode";
3
3
  import { encode } from "./encode";
4
- import isBrowser from "./isBrowser";
4
+ import { isBrowser } from "./isBrowser";
5
+ import { isNullOrUndefined } from "./isNullOrUndefined";
5
6
  import { isString } from "./isString";
7
+ /**
8
+ * Utility to create a storage instance to interact with `localStorage` using
9
+ * encrypted (encoded) key/values.
10
+ */
6
11
  export var createStorage = function (config) {
7
12
  var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
8
13
  /**
@@ -22,6 +27,12 @@ export var createStorage = function (config) {
22
27
  return (__assign(__assign({}, map), (_a = {}, _a[key] = encode(key), _a)));
23
28
  }, {});
24
29
  return {
30
+ /**
31
+ * Get all storage value (it uses `localStorage.get()`).
32
+ *
33
+ * Unparseable values with `JSON.parse()` return their value as it is.
34
+ * If the given `key` argument is not found `null` is returned.
35
+ */
25
36
  get: function (key) {
26
37
  var stored = ls("g", keys[key]);
27
38
  if (stored) {
@@ -35,22 +46,71 @@ export var createStorage = function (config) {
35
46
  }
36
47
  return null;
37
48
  },
49
+ /**
50
+ * Get all storage values (it uses `localStorage.get()`).
51
+ *
52
+ * `undefined` and `null` values are not returned.
53
+ */
54
+ getAll: function () {
55
+ var all = {};
56
+ for (var key in keys) {
57
+ var value = this.get(key);
58
+ if (!isNullOrUndefined(value)) {
59
+ all[key] = value;
60
+ }
61
+ }
62
+ return all;
63
+ },
64
+ /**
65
+ * Set a storage value (it uses `localStorage.set()`).
66
+ *
67
+ * Non-string values are stringified with `JSON.stringify()`
68
+ */
38
69
  set: function (key, value) {
39
70
  ls("s", keys[key], isString(value) ? encode(value) : encode(JSON.stringify(value)));
40
71
  },
72
+ /**
73
+ * Set all given storage values (it uses `localStorage.set()`).
74
+ *
75
+ * Non-string values are stringified with `JSON.stringify()`, `undefined`
76
+ * and `null` values are removed from the storage
77
+ */
78
+ setMany: function (newValues) {
79
+ for (var key in newValues) {
80
+ var value = newValues[key];
81
+ if (!isNullOrUndefined(value)) {
82
+ this.set(key, value);
83
+ }
84
+ else {
85
+ this.remove(key);
86
+ }
87
+ }
88
+ },
89
+ /**
90
+ * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
91
+ */
41
92
  has: function (key) {
42
93
  var stored = ls("g", keys[key]);
43
94
  return !!stored;
44
95
  },
96
+ /**
97
+ * Remove a storage value (it uses `localStorage.remove()`).
98
+ */
45
99
  remove: function (key) {
46
100
  ls("r", keys[key]);
47
101
  },
102
+ /**
103
+ * Clear all storage values (it uses `localStorage.remove()`).
104
+ */
48
105
  clear: function () {
49
106
  for (var key in keys) {
50
107
  ls("r", keys[key]);
51
108
  }
52
109
  },
53
110
  /**
111
+ * Watch a storage value changes, this needs to be executed only in browser
112
+ * context (it uses `window.addEventListener("storage")`).
113
+ *
54
114
  * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
55
115
  */
56
116
  watch: function (keyToWatch, onRemoved, onAdded) {
@@ -65,6 +125,8 @@ export var createStorage = function (config) {
65
125
  }
66
126
  }
67
127
  };
128
+ if (!isBrowser)
129
+ return function () { return void 0; };
68
130
  window.addEventListener("storage", handler);
69
131
  return function () {
70
132
  window.removeEventListener("storage", handler);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildUrlQueryString = void 0;
4
4
  var isNull_1 = require("./isNull");
5
5
  var isUndefined_1 = require("./isUndefined");
6
+ var isArray_1 = require("./isArray");
6
7
  /**
7
8
  * Get clean query string for URL
8
9
  *
@@ -13,11 +14,17 @@ var isUndefined_1 = require("./isUndefined");
13
14
  * @category location
14
15
  */
15
16
  function buildUrlQueryString(params) {
16
- if (params === void 0) { params = {}; }
17
17
  var output = "";
18
+ if (!params)
19
+ return output;
18
20
  for (var key in params) {
19
21
  var value = params[key];
20
- if (!(0, isNull_1.default)(value) && !(0, isUndefined_1.default)(value)) {
22
+ if ((0, isArray_1.default)(value)) {
23
+ for (var i = 0; i < value.length; i++) {
24
+ output += "".concat(key, "=").concat(encodeURIComponent(value[i] + ""), "&");
25
+ }
26
+ }
27
+ else if (!(0, isNull_1.default)(value) && !(0, isUndefined_1.default)(value)) {
21
28
  output += "".concat(key, "=").concat(encodeURIComponent(value + ""), "&");
22
29
  }
23
30
  }
@@ -5,14 +5,19 @@ var tslib_1 = require("tslib");
5
5
  var decode_1 = require("./decode");
6
6
  var encode_1 = require("./encode");
7
7
  var isBrowser_1 = require("./isBrowser");
8
+ var isNullOrUndefined_1 = require("./isNullOrUndefined");
8
9
  var isString_1 = require("./isString");
10
+ /**
11
+ * Utility to create a storage instance to interact with `localStorage` using
12
+ * encrypted (encoded) key/values.
13
+ */
9
14
  var createStorage = function (config) {
10
15
  var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
11
16
  /**
12
17
  * Super minifiable localStorage wrapper with SSR safety
13
18
  */
14
19
  var ls = function (method, key, value) {
15
- return isBrowser_1.default
20
+ return isBrowser_1.isBrowser
16
21
  ? localStorage[methodsMap[method]](key, value)
17
22
  : function () {
18
23
  if (process.env["NODE_ENV"] !== "production") {
@@ -25,6 +30,12 @@ var createStorage = function (config) {
25
30
  return (tslib_1.__assign(tslib_1.__assign({}, map), (_a = {}, _a[key] = (0, encode_1.encode)(key), _a)));
26
31
  }, {});
27
32
  return {
33
+ /**
34
+ * Get all storage value (it uses `localStorage.get()`).
35
+ *
36
+ * Unparseable values with `JSON.parse()` return their value as it is.
37
+ * If the given `key` argument is not found `null` is returned.
38
+ */
28
39
  get: function (key) {
29
40
  var stored = ls("g", keys[key]);
30
41
  if (stored) {
@@ -38,22 +49,71 @@ var createStorage = function (config) {
38
49
  }
39
50
  return null;
40
51
  },
52
+ /**
53
+ * Get all storage values (it uses `localStorage.get()`).
54
+ *
55
+ * `undefined` and `null` values are not returned.
56
+ */
57
+ getAll: function () {
58
+ var all = {};
59
+ for (var key in keys) {
60
+ var value = this.get(key);
61
+ if (!(0, isNullOrUndefined_1.isNullOrUndefined)(value)) {
62
+ all[key] = value;
63
+ }
64
+ }
65
+ return all;
66
+ },
67
+ /**
68
+ * Set a storage value (it uses `localStorage.set()`).
69
+ *
70
+ * Non-string values are stringified with `JSON.stringify()`
71
+ */
41
72
  set: function (key, value) {
42
73
  ls("s", keys[key], (0, isString_1.isString)(value) ? (0, encode_1.encode)(value) : (0, encode_1.encode)(JSON.stringify(value)));
43
74
  },
75
+ /**
76
+ * Set all given storage values (it uses `localStorage.set()`).
77
+ *
78
+ * Non-string values are stringified with `JSON.stringify()`, `undefined`
79
+ * and `null` values are removed from the storage
80
+ */
81
+ setMany: function (newValues) {
82
+ for (var key in newValues) {
83
+ var value = newValues[key];
84
+ if (!(0, isNullOrUndefined_1.isNullOrUndefined)(value)) {
85
+ this.set(key, value);
86
+ }
87
+ else {
88
+ this.remove(key);
89
+ }
90
+ }
91
+ },
92
+ /**
93
+ * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
94
+ */
44
95
  has: function (key) {
45
96
  var stored = ls("g", keys[key]);
46
97
  return !!stored;
47
98
  },
99
+ /**
100
+ * Remove a storage value (it uses `localStorage.remove()`).
101
+ */
48
102
  remove: function (key) {
49
103
  ls("r", keys[key]);
50
104
  },
105
+ /**
106
+ * Clear all storage values (it uses `localStorage.remove()`).
107
+ */
51
108
  clear: function () {
52
109
  for (var key in keys) {
53
110
  ls("r", keys[key]);
54
111
  }
55
112
  },
56
113
  /**
114
+ * Watch a storage value changes, this needs to be executed only in browser
115
+ * context (it uses `window.addEventListener("storage")`).
116
+ *
57
117
  * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
58
118
  */
59
119
  watch: function (keyToWatch, onRemoved, onAdded) {
@@ -68,6 +128,8 @@ var createStorage = function (config) {
68
128
  }
69
129
  }
70
130
  };
131
+ if (!isBrowser_1.isBrowser)
132
+ return function () { return void 0; };
71
133
  window.addEventListener("storage", handler);
72
134
  return function () {
73
135
  window.removeEventListener("storage", handler);
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "peerDependencies": {
8
8
  "tslib": "^2.4.0"
9
9
  },
10
- "version": "1.0.70",
10
+ "version": "1.0.73",
11
11
  "module": "./index.js",
12
12
  "types": "./index.d.ts"
13
13
  }
package/typings.d.ts CHANGED
@@ -6,3 +6,7 @@
6
6
 
7
7
  // eslint-disable-next-line no-var,@typescript-eslint/no-explicit-any
8
8
  declare var gtag: (...args: any[]) => any;
9
+
10
+ // declare type NullableRecord<T> = {
11
+ // [K in keyof T]: T[K] | null;
12
+ // };