@koine/utils 1.0.34 → 1.0.37
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/Emitter.d.ts +2 -2
- package/createStorage.d.ts +13 -0
- package/createStorage.js +75 -0
- package/getNonce.d.ts +1 -1
- package/index.d.ts +10 -2
- package/index.js +10 -2
- package/isNullOrUndefined.d.ts +1 -1
- package/node/createStorage.js +79 -0
- package/node/index.js +10 -2
- package/node/readCookie.js +2 -2
- package/node/setCookie.js +2 -2
- package/package.json +1 -1
- package/pageview.d.ts +1 -1
- package/parseURL.d.ts +1 -1
- package/readCookie.js +2 -2
- package/setCookie.js +2 -2
package/Emitter.d.ts
CHANGED
|
@@ -16,10 +16,10 @@ export declare function Emitter<EventMap extends {
|
|
|
16
16
|
/**
|
|
17
17
|
* Register an event handler for the given type.
|
|
18
18
|
*/
|
|
19
|
-
on<EventName extends keyof EventMap>(name: EventName, handler: (data?: EventMap[EventName]
|
|
19
|
+
on<EventName extends keyof EventMap>(name: EventName, handler: (data?: EventMap[EventName]) => any): void;
|
|
20
20
|
/**
|
|
21
21
|
* Invoke all handlers for the given type.
|
|
22
22
|
*/
|
|
23
|
-
emit<EventName_1 extends keyof EventMap>(name: EventName_1, data?: EventMap[EventName_1]
|
|
23
|
+
emit<EventName_1 extends keyof EventMap>(name: EventName_1, data?: EventMap[EventName_1]): void;
|
|
24
24
|
};
|
|
25
25
|
export default Emitter;
|
|
@@ -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];
|
|
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/getNonce.d.ts
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
* @category security
|
|
3
3
|
* @see https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/utils/nonce.ts
|
|
4
4
|
*/
|
|
5
|
-
export declare function getNonce(): string
|
|
5
|
+
export declare function getNonce(): string;
|
|
6
6
|
export default getNonce;
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* the selection is based on:
|
|
4
|
+
* Libraries to encapsulate and re-export from here, the selection is based on:
|
|
6
5
|
*
|
|
7
6
|
* [x] full typescript support
|
|
8
7
|
* [x] treeshake-ability
|
|
@@ -24,6 +23,14 @@
|
|
|
24
23
|
*
|
|
25
24
|
* About utilities useful examples @see:
|
|
26
25
|
* - https://github.com/chakra-ui/chakra-ui/blob/main/packages/utils/src
|
|
26
|
+
*
|
|
27
|
+
* TODO:
|
|
28
|
+
* We could also re-exports direct dependencies of packages that we often use
|
|
29
|
+
* anyway like [those of `yup`](https://github.com/jquense/yup/blob/master/package.json#L103):
|
|
30
|
+
*
|
|
31
|
+
* - [tiny-case](https://github.com/jquense/tiny-case)
|
|
32
|
+
* - [property-expr](https://github.com/jquense/expr/blob/master/index.js)
|
|
33
|
+
* - [toposort](https://github.com/marcelklehr/toposort)
|
|
27
34
|
*/
|
|
28
35
|
export * from "./accentSets";
|
|
29
36
|
export * from "./addOrReplaceAtIdx";
|
|
@@ -36,6 +43,7 @@ export * from "./clamp";
|
|
|
36
43
|
export * from "./clsx";
|
|
37
44
|
export * from "./convertRange";
|
|
38
45
|
export * from "./cookie";
|
|
46
|
+
export * from "./createStorage";
|
|
39
47
|
export * from "./decode";
|
|
40
48
|
export * from "./Defer";
|
|
41
49
|
export * from "./Emitter";
|
package/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* the selection is based on:
|
|
4
|
+
* Libraries to encapsulate and re-export from here, the selection is based on:
|
|
6
5
|
*
|
|
7
6
|
* [x] full typescript support
|
|
8
7
|
* [x] treeshake-ability
|
|
@@ -24,6 +23,14 @@
|
|
|
24
23
|
*
|
|
25
24
|
* About utilities useful examples @see:
|
|
26
25
|
* - https://github.com/chakra-ui/chakra-ui/blob/main/packages/utils/src
|
|
26
|
+
*
|
|
27
|
+
* TODO:
|
|
28
|
+
* We could also re-exports direct dependencies of packages that we often use
|
|
29
|
+
* anyway like [those of `yup`](https://github.com/jquense/yup/blob/master/package.json#L103):
|
|
30
|
+
*
|
|
31
|
+
* - [tiny-case](https://github.com/jquense/tiny-case)
|
|
32
|
+
* - [property-expr](https://github.com/jquense/expr/blob/master/index.js)
|
|
33
|
+
* - [toposort](https://github.com/marcelklehr/toposort)
|
|
27
34
|
*/
|
|
28
35
|
export * from "./accentSets";
|
|
29
36
|
export * from "./addOrReplaceAtIdx";
|
|
@@ -36,6 +43,7 @@ export * from "./clamp";
|
|
|
36
43
|
export * from "./clsx";
|
|
37
44
|
export * from "./convertRange";
|
|
38
45
|
export * from "./cookie";
|
|
46
|
+
export * from "./createStorage";
|
|
39
47
|
export * from "./decode";
|
|
40
48
|
export * from "./Defer";
|
|
41
49
|
export * from "./Emitter";
|
package/isNullOrUndefined.d.ts
CHANGED
|
@@ -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
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* @file
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* the selection is based on:
|
|
5
|
+
* Libraries to encapsulate and re-export from here, the selection is based on:
|
|
7
6
|
*
|
|
8
7
|
* [x] full typescript support
|
|
9
8
|
* [x] treeshake-ability
|
|
@@ -25,6 +24,14 @@
|
|
|
25
24
|
*
|
|
26
25
|
* About utilities useful examples @see:
|
|
27
26
|
* - https://github.com/chakra-ui/chakra-ui/blob/main/packages/utils/src
|
|
27
|
+
*
|
|
28
|
+
* TODO:
|
|
29
|
+
* We could also re-exports direct dependencies of packages that we often use
|
|
30
|
+
* anyway like [those of `yup`](https://github.com/jquense/yup/blob/master/package.json#L103):
|
|
31
|
+
*
|
|
32
|
+
* - [tiny-case](https://github.com/jquense/tiny-case)
|
|
33
|
+
* - [property-expr](https://github.com/jquense/expr/blob/master/index.js)
|
|
34
|
+
* - [toposort](https://github.com/marcelklehr/toposort)
|
|
28
35
|
*/
|
|
29
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
37
|
var tslib_1 = require("tslib");
|
|
@@ -39,6 +46,7 @@ tslib_1.__exportStar(require("./clamp"), exports);
|
|
|
39
46
|
tslib_1.__exportStar(require("./clsx"), exports);
|
|
40
47
|
tslib_1.__exportStar(require("./convertRange"), exports);
|
|
41
48
|
tslib_1.__exportStar(require("./cookie"), exports);
|
|
49
|
+
tslib_1.__exportStar(require("./createStorage"), exports);
|
|
42
50
|
tslib_1.__exportStar(require("./decode"), exports);
|
|
43
51
|
tslib_1.__exportStar(require("./Defer"), exports);
|
|
44
52
|
tslib_1.__exportStar(require("./Emitter"), exports);
|
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,9 +21,9 @@ 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
|
-
return;
|
|
26
|
+
return undefined;
|
|
27
27
|
}
|
|
28
28
|
if ((0, isNumber_1.default)(expires)) {
|
|
29
29
|
expires = new Date(Date.now() + expires * 864e5);
|
package/package.json
CHANGED
package/pageview.d.ts
CHANGED
|
@@ -6,4 +6,4 @@ export declare type GtmPageviewArgs = [
|
|
|
6
6
|
/**
|
|
7
7
|
* @category analytics-google
|
|
8
8
|
*/
|
|
9
|
-
export declare const pageview: (page_path?: string
|
|
9
|
+
export declare const pageview: (page_path?: string, page_title?: string, page_location?: string) => void;
|
package/parseURL.d.ts
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,9 +18,9 @@ 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
|
-
return;
|
|
23
|
+
return undefined;
|
|
24
24
|
}
|
|
25
25
|
if (isNumber(expires)) {
|
|
26
26
|
expires = new Date(Date.now() + expires * 864e5);
|