@koine/utils 1.0.32 → 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.
- package/createStorage.d.ts +13 -0
- package/createStorage.js +75 -0
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/node/createStorage.js +79 -0
- package/node/index.js +2 -1
- package/node/objectOmit.js +23 -0
- package/node/{whitelistObject.js → objectPick.js} +5 -5
- package/node/readCookie.js +2 -2
- package/node/setCookie.js +1 -1
- package/objectOmit.d.ts +11 -0
- package/objectOmit.js +19 -0
- package/objectPick.d.ts +8 -0
- package/objectPick.js +15 -0
- package/package.json +1 -1
- package/readCookie.js +2 -2
- package/setCookie.js +1 -1
|
@@ -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;
|
package/createStorage.js
ADDED
|
@@ -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;
|
package/index.d.ts
CHANGED
|
@@ -104,6 +104,8 @@ export * from "./navigateToParams";
|
|
|
104
104
|
export * from "./navigateWithoutUrlParam";
|
|
105
105
|
export * from "./normaliseUrlPathname";
|
|
106
106
|
export * from "./normaliseUrl";
|
|
107
|
+
export * from "./objectPick";
|
|
108
|
+
export * from "./objectOmit";
|
|
107
109
|
export * from "./pageview";
|
|
108
110
|
export * from "./parseCookie";
|
|
109
111
|
export * from "./parseURL";
|
|
@@ -133,5 +135,4 @@ export * from "./updateLinkParams";
|
|
|
133
135
|
export * from "./updateUrlQueryParams";
|
|
134
136
|
export * from "./uuid";
|
|
135
137
|
export * from "./wait";
|
|
136
|
-
export * from "./whitelistObject";
|
|
137
138
|
export type { Primitive, Class, Constructor, TypedArray, ObservableLike, Except, Mutable, Merge, MergeExclusive, RequireAtLeastOne, RequireExactlyOne, RequireAllOrNone, RemoveIndexSignature, PartialDeep, ReadonlyDeep, LiteralUnion, Opaque, InvariantOf, SetOptional, SetRequired, ValueOf, ConditionalKeys, ConditionalPick, ConditionalExcept, UnionToIntersection, LiteralToPrimitive, Stringified, IterableElement, Entry, Entries, SetReturnType, Simplify, Get, StringKeyOf, Schema, Jsonify, JsonPrimitive, JsonObject, JsonArray, JsonValue, Promisable, AsyncReturnType, Asyncify, Trim, Split, Includes, Join, LastArrayElement, FixedLengthArray, MultidimensionalArray, MultidimensionalReadonlyArray, PositiveInfinity, NegativeInfinity, Finite, Integer, Float, NegativeFloat, Negative, NonNegative, NegativeInteger, NonNegativeInteger, CamelCase, CamelCasedProperties, CamelCasedPropertiesDeep, KebabCase, KebabCasedProperties, KebabCasedPropertiesDeep, PascalCase, PascalCasedProperties, PascalCasedPropertiesDeep, SnakeCase, SnakeCasedProperties, SnakeCasedPropertiesDeep, ScreamingSnakeCase, DelimiterCase, DelimiterCasedProperties, DelimiterCasedPropertiesDeep, PackageJson, TsConfigJson, } from "type-fest";
|
package/index.js
CHANGED
|
@@ -105,6 +105,8 @@ export * from "./navigateToParams";
|
|
|
105
105
|
export * from "./navigateWithoutUrlParam";
|
|
106
106
|
export * from "./normaliseUrlPathname";
|
|
107
107
|
export * from "./normaliseUrl";
|
|
108
|
+
export * from "./objectPick";
|
|
109
|
+
export * from "./objectOmit";
|
|
108
110
|
export * from "./pageview";
|
|
109
111
|
export * from "./parseCookie";
|
|
110
112
|
export * from "./parseURL";
|
|
@@ -134,4 +136,3 @@ export * from "./updateLinkParams";
|
|
|
134
136
|
export * from "./updateUrlQueryParams";
|
|
135
137
|
export * from "./uuid";
|
|
136
138
|
export * from "./wait";
|
|
137
|
-
export * from "./whitelistObject";
|
|
@@ -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;
|
package/node/index.js
CHANGED
|
@@ -108,6 +108,8 @@ tslib_1.__exportStar(require("./navigateToParams"), exports);
|
|
|
108
108
|
tslib_1.__exportStar(require("./navigateWithoutUrlParam"), exports);
|
|
109
109
|
tslib_1.__exportStar(require("./normaliseUrlPathname"), exports);
|
|
110
110
|
tslib_1.__exportStar(require("./normaliseUrl"), exports);
|
|
111
|
+
tslib_1.__exportStar(require("./objectPick"), exports);
|
|
112
|
+
tslib_1.__exportStar(require("./objectOmit"), exports);
|
|
111
113
|
tslib_1.__exportStar(require("./pageview"), exports);
|
|
112
114
|
tslib_1.__exportStar(require("./parseCookie"), exports);
|
|
113
115
|
tslib_1.__exportStar(require("./parseURL"), exports);
|
|
@@ -137,4 +139,3 @@ tslib_1.__exportStar(require("./updateLinkParams"), exports);
|
|
|
137
139
|
tslib_1.__exportStar(require("./updateUrlQueryParams"), exports);
|
|
138
140
|
tslib_1.__exportStar(require("./uuid"), exports);
|
|
139
141
|
tslib_1.__exportStar(require("./wait"), exports);
|
|
140
|
-
tslib_1.__exportStar(require("./whitelistObject"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.objectOmit = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Omit object properties by removing the given keys, it returns a
|
|
6
|
+
* new object.
|
|
7
|
+
*
|
|
8
|
+
* NOTE: most of the time using a normal [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) is enough,
|
|
9
|
+
* use this utility only when it makes sense.
|
|
10
|
+
*
|
|
11
|
+
* @category objects
|
|
12
|
+
*/
|
|
13
|
+
function objectOmit(object, keys) {
|
|
14
|
+
return Object.keys(object).reduce(function (output, key) {
|
|
15
|
+
if (!keys.includes(key)) {
|
|
16
|
+
output[key] =
|
|
17
|
+
object[key];
|
|
18
|
+
}
|
|
19
|
+
return output;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
22
|
+
exports.objectOmit = objectOmit;
|
|
23
|
+
exports.default = objectOmit;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.objectPick = void 0;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Pick object properties by selecting only the given keys, it returns a
|
|
6
6
|
* new object.
|
|
7
7
|
*
|
|
8
8
|
* @category objects
|
|
9
9
|
*/
|
|
10
|
-
function
|
|
10
|
+
function objectPick(object, keys) {
|
|
11
11
|
var output = {};
|
|
12
12
|
var len = keys.length;
|
|
13
13
|
while (len--) {
|
|
@@ -15,5 +15,5 @@ function whitelistObject(object, keys) {
|
|
|
15
15
|
}
|
|
16
16
|
return output;
|
|
17
17
|
}
|
|
18
|
-
exports.
|
|
19
|
-
exports.default =
|
|
18
|
+
exports.objectPick = objectPick;
|
|
19
|
+
exports.default = objectPick;
|
package/node/readCookie.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
24
|
+
console.warn("[@koine/utils] cookie setCookie: document is undefined");
|
|
25
25
|
}
|
|
26
26
|
return;
|
|
27
27
|
}
|
package/objectOmit.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Omit object properties by removing the given keys, it returns a
|
|
3
|
+
* new object.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: most of the time using a normal [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) is enough,
|
|
6
|
+
* use this utility only when it makes sense.
|
|
7
|
+
*
|
|
8
|
+
* @category objects
|
|
9
|
+
*/
|
|
10
|
+
export declare function objectOmit<T extends object, Keys extends (keyof T)[]>(object: T, keys: Keys): Omit<T, Keys[number]>;
|
|
11
|
+
export default objectOmit;
|
package/objectOmit.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Omit object properties by removing the given keys, it returns a
|
|
3
|
+
* new object.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: most of the time using a normal [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) is enough,
|
|
6
|
+
* use this utility only when it makes sense.
|
|
7
|
+
*
|
|
8
|
+
* @category objects
|
|
9
|
+
*/
|
|
10
|
+
export function objectOmit(object, keys) {
|
|
11
|
+
return Object.keys(object).reduce(function (output, key) {
|
|
12
|
+
if (!keys.includes(key)) {
|
|
13
|
+
output[key] =
|
|
14
|
+
object[key];
|
|
15
|
+
}
|
|
16
|
+
return output;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
export default objectOmit;
|
package/objectPick.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pick object properties by selecting only the given keys, it returns a
|
|
3
|
+
* new object.
|
|
4
|
+
*
|
|
5
|
+
* @category objects
|
|
6
|
+
*/
|
|
7
|
+
export declare function objectPick<T extends object, Keys extends (keyof T)[]>(object: T, keys: Keys): Pick<T, Keys[number]>;
|
|
8
|
+
export default objectPick;
|
package/objectPick.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pick object properties by selecting only the given keys, it returns a
|
|
3
|
+
* new object.
|
|
4
|
+
*
|
|
5
|
+
* @category objects
|
|
6
|
+
*/
|
|
7
|
+
export function objectPick(object, keys) {
|
|
8
|
+
var output = {};
|
|
9
|
+
var len = keys.length;
|
|
10
|
+
while (len--) {
|
|
11
|
+
output[keys[len]] = object[keys[len]];
|
|
12
|
+
}
|
|
13
|
+
return output;
|
|
14
|
+
}
|
|
15
|
+
export default objectPick;
|
package/package.json
CHANGED
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
|
|
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
|
|
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
|
|
21
|
+
console.warn("[@koine/utils] cookie setCookie: document is undefined");
|
|
22
22
|
}
|
|
23
23
|
return;
|
|
24
24
|
}
|