@8ms/helpers 1.1.112 → 1.2.0
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/global/forceUpdate.d.ts +8 -0
- package/global/forceUpdate.js +22 -0
- package/global/init.d.ts +10 -0
- package/global/init.js +15 -0
- package/global/read.d.ts +10 -0
- package/global/read.js +28 -0
- package/object/replaceKeys.d.ts +10 -0
- package/object/replaceKeys.js +30 -0
- package/package.json +1 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Force an update of the cached data
|
|
5
|
+
*/
|
|
6
|
+
const forceUpdate = async ({ key }) => {
|
|
7
|
+
// Doesn't exist
|
|
8
|
+
if (undefined === global[key] || null === global[key]) {
|
|
9
|
+
// Throw error - should always exist
|
|
10
|
+
throw `Global key '${key}' does not exist, requires initialisation.`;
|
|
11
|
+
}
|
|
12
|
+
// Reassign the global key
|
|
13
|
+
else {
|
|
14
|
+
global[key] = {
|
|
15
|
+
expires: global[key].expires,
|
|
16
|
+
insertedAt: Date.now(),
|
|
17
|
+
updateFn: global[key].updateFn,
|
|
18
|
+
value: await global[key].updateFn(),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.default = forceUpdate;
|
package/global/init.d.ts
ADDED
package/global/init.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Initialise a new global data.
|
|
5
|
+
*/
|
|
6
|
+
const init = async ({ expires, key, updateFn }) => {
|
|
7
|
+
global[key] = {
|
|
8
|
+
expires,
|
|
9
|
+
insertedAt: Date.now(),
|
|
10
|
+
updateFn,
|
|
11
|
+
value: await updateFn(),
|
|
12
|
+
};
|
|
13
|
+
return global[key];
|
|
14
|
+
};
|
|
15
|
+
exports.default = init;
|
package/global/read.d.ts
ADDED
package/global/read.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const forceUpdate_1 = __importDefault(require("./forceUpdate"));
|
|
7
|
+
/**
|
|
8
|
+
* Retrieve a value from global memory.
|
|
9
|
+
*/
|
|
10
|
+
const read = async ({ key }) => {
|
|
11
|
+
// Doesn't exist
|
|
12
|
+
if (undefined === global[key] || null === global[key]) {
|
|
13
|
+
// Throw error - should always exist
|
|
14
|
+
throw `Global key '${key}' does not exist, requires initialisation.`;
|
|
15
|
+
}
|
|
16
|
+
// Exists (0 is determined as forever)
|
|
17
|
+
else if (0 != global[key].expires) {
|
|
18
|
+
// https://ui.dev/get-current-timestamp-javascript
|
|
19
|
+
const expiresAt = Math.floor(global[key].insertedAt / 1000) + global[key].expires;
|
|
20
|
+
const now = Math.floor(Date.now() / 1000);
|
|
21
|
+
// If we are now beyond the expiry date, update the data
|
|
22
|
+
if (now > expiresAt) {
|
|
23
|
+
await (0, forceUpdate_1.default)({ key });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return global[key];
|
|
27
|
+
};
|
|
28
|
+
exports.default = read;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare type ReplaceKeys = {
|
|
2
|
+
instance: any;
|
|
3
|
+
getNewKey: Function;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Iteratively replace all the keys in an object.
|
|
7
|
+
* https://www.techighness.com/post/replace-all-keys-of-deeply-nested-objects-or-array-of-objects/
|
|
8
|
+
*/
|
|
9
|
+
declare const replaceKeys: ({ instance, getNewKey }: ReplaceKeys) => object;
|
|
10
|
+
export default replaceKeys;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Iteratively replace all the keys in an object.
|
|
5
|
+
* https://www.techighness.com/post/replace-all-keys-of-deeply-nested-objects-or-array-of-objects/
|
|
6
|
+
*/
|
|
7
|
+
const replaceKeys = ({ instance, getNewKey }) => {
|
|
8
|
+
let response = instance;
|
|
9
|
+
if (Array.isArray(instance)) {
|
|
10
|
+
response = [];
|
|
11
|
+
for (let i = 0; i < instance.length; i++) {
|
|
12
|
+
response[i] = replaceKeys({
|
|
13
|
+
instance: instance[i],
|
|
14
|
+
getNewKey,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else if ("object" === typeof instance) {
|
|
19
|
+
response = {};
|
|
20
|
+
for (const key in instance) {
|
|
21
|
+
const newKey = getNewKey(key);
|
|
22
|
+
response[newKey] = replaceKeys({
|
|
23
|
+
instance: instance[key],
|
|
24
|
+
getNewKey,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return response;
|
|
29
|
+
};
|
|
30
|
+
exports.default = replaceKeys;
|