@koine/utils 1.0.69 → 1.0.72

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,52 @@
1
+ declare type NullableRecord<T> = {
2
+ [K in keyof T]: T[K] | null;
3
+ };
1
4
  export declare type CreateStorageConfig = Record<string, any>;
5
+ /**
6
+ * Utility to create a storage instance to interact with `localStorage` using
7
+ * encrypted (encoded) key/values.
8
+ */
2
9
  export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T>) => {
10
+ /**
11
+ * Get all storage value (it uses `localStorage.get()`).
12
+ *
13
+ * Unparseable values with `JSON.parse()` return their value as it is.
14
+ * If the given `key` argument is not found `null` is returned.
15
+ */
3
16
  get<TKey extends keyof T>(key: TKey): T[TKey] | null;
17
+ /**
18
+ * Get all storage values (it uses `localStorage.get()`).
19
+ */
20
+ getAll(): NullableRecord<T>;
21
+ /**
22
+ * Set a storage value (it uses `localStorage.set()`).
23
+ *
24
+ * Non-string values are stringified with `JSON.stringify()`
25
+ */
4
26
  set<TKey_1 extends keyof T>(key: TKey_1, value: T[TKey_1]): void;
27
+ /**
28
+ * Set all given storage values (it uses `localStorage.set()`).
29
+ *
30
+ * Non-string values are stringified with `JSON.stringify()`, `undefined`
31
+ * and `null` values are removed from the storage
32
+ */
33
+ setMany(newValues: Partial<T>): void;
34
+ /**
35
+ * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
36
+ */
5
37
  has<TKey_2 extends keyof T>(key: TKey_2): boolean;
38
+ /**
39
+ * Remove a storage value (it uses `localStorage.remove()`).
40
+ */
6
41
  remove<TKey_3 extends keyof T>(key: TKey_3): void;
42
+ /**
43
+ * Clear all storage values (it uses `localStorage.remove()`).
44
+ */
7
45
  clear(): void;
8
46
  /**
47
+ * Watch a storage value changes, this needs to be executed only in browser
48
+ * context (it uses `window.addEventListener("storage")`).
49
+ *
9
50
  * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
10
51
  */
11
52
  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 { isUndefined } from "./isUndefined";
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,66 @@ 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
+ getAll: function () {
53
+ var all = {};
54
+ for (var key in keys) {
55
+ all[key] = this.get(key);
56
+ }
57
+ return all;
58
+ },
59
+ /**
60
+ * Set a storage value (it uses `localStorage.set()`).
61
+ *
62
+ * Non-string values are stringified with `JSON.stringify()`
63
+ */
38
64
  set: function (key, value) {
39
65
  ls("s", keys[key], isString(value) ? encode(value) : encode(JSON.stringify(value)));
40
66
  },
67
+ /**
68
+ * Set all given storage values (it uses `localStorage.set()`).
69
+ *
70
+ * Non-string values are stringified with `JSON.stringify()`, `undefined`
71
+ * and `null` values are removed from the storage
72
+ */
73
+ setMany: function (newValues) {
74
+ for (var key in newValues) {
75
+ var value = newValues[key];
76
+ if (!isUndefined(value)) {
77
+ this.set(key, value);
78
+ }
79
+ else {
80
+ this.remove(key);
81
+ }
82
+ }
83
+ },
84
+ /**
85
+ * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
86
+ */
41
87
  has: function (key) {
42
88
  var stored = ls("g", keys[key]);
43
89
  return !!stored;
44
90
  },
91
+ /**
92
+ * Remove a storage value (it uses `localStorage.remove()`).
93
+ */
45
94
  remove: function (key) {
46
95
  ls("r", keys[key]);
47
96
  },
97
+ /**
98
+ * Clear all storage values (it uses `localStorage.remove()`).
99
+ */
48
100
  clear: function () {
49
101
  for (var key in keys) {
50
102
  ls("r", keys[key]);
51
103
  }
52
104
  },
53
105
  /**
106
+ * Watch a storage value changes, this needs to be executed only in browser
107
+ * context (it uses `window.addEventListener("storage")`).
108
+ *
54
109
  * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
55
110
  */
56
111
  watch: function (keyToWatch, onRemoved, onAdded) {
@@ -65,6 +120,8 @@ export var createStorage = function (config) {
65
120
  }
66
121
  }
67
122
  };
123
+ if (!isBrowser)
124
+ return function () { return void 0; };
68
125
  window.addEventListener("storage", handler);
69
126
  return function () {
70
127
  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 isUndefined_1 = require("./isUndefined");
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,66 @@ var createStorage = function (config) {
38
49
  }
39
50
  return null;
40
51
  },
52
+ /**
53
+ * Get all storage values (it uses `localStorage.get()`).
54
+ */
55
+ getAll: function () {
56
+ var all = {};
57
+ for (var key in keys) {
58
+ all[key] = this.get(key);
59
+ }
60
+ return all;
61
+ },
62
+ /**
63
+ * Set a storage value (it uses `localStorage.set()`).
64
+ *
65
+ * Non-string values are stringified with `JSON.stringify()`
66
+ */
41
67
  set: function (key, value) {
42
68
  ls("s", keys[key], (0, isString_1.isString)(value) ? (0, encode_1.encode)(value) : (0, encode_1.encode)(JSON.stringify(value)));
43
69
  },
70
+ /**
71
+ * Set all given storage values (it uses `localStorage.set()`).
72
+ *
73
+ * Non-string values are stringified with `JSON.stringify()`, `undefined`
74
+ * and `null` values are removed from the storage
75
+ */
76
+ setMany: function (newValues) {
77
+ for (var key in newValues) {
78
+ var value = newValues[key];
79
+ if (!(0, isUndefined_1.isUndefined)(value)) {
80
+ this.set(key, value);
81
+ }
82
+ else {
83
+ this.remove(key);
84
+ }
85
+ }
86
+ },
87
+ /**
88
+ * Check if a storage value is _truthy_ (it uses `localStorage.get()`).
89
+ */
44
90
  has: function (key) {
45
91
  var stored = ls("g", keys[key]);
46
92
  return !!stored;
47
93
  },
94
+ /**
95
+ * Remove a storage value (it uses `localStorage.remove()`).
96
+ */
48
97
  remove: function (key) {
49
98
  ls("r", keys[key]);
50
99
  },
100
+ /**
101
+ * Clear all storage values (it uses `localStorage.remove()`).
102
+ */
51
103
  clear: function () {
52
104
  for (var key in keys) {
53
105
  ls("r", keys[key]);
54
106
  }
55
107
  },
56
108
  /**
109
+ * Watch a storage value changes, this needs to be executed only in browser
110
+ * context (it uses `window.addEventListener("storage")`).
111
+ *
57
112
  * Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
58
113
  */
59
114
  watch: function (keyToWatch, onRemoved, onAdded) {
@@ -68,6 +123,8 @@ var createStorage = function (config) {
68
123
  }
69
124
  }
70
125
  };
126
+ if (!isBrowser_1.isBrowser)
127
+ return function () { return void 0; };
71
128
  window.addEventListener("storage", handler);
72
129
  return function () {
73
130
  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.69",
10
+ "version": "1.0.72",
11
11
  "module": "./index.js",
12
12
  "types": "./index.d.ts"
13
13
  }