@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.
@@ -0,0 +1,8 @@
1
+ declare type ForceUpdate = {
2
+ key: string;
3
+ };
4
+ /**
5
+ * Force an update of the cached data
6
+ */
7
+ declare const forceUpdate: ({ key }: ForceUpdate) => Promise<void>;
8
+ export default forceUpdate;
@@ -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;
@@ -0,0 +1,10 @@
1
+ declare type Update = {
2
+ expires: number;
3
+ key: string;
4
+ updateFn: Function;
5
+ };
6
+ /**
7
+ * Initialise a new global data.
8
+ */
9
+ declare const init: ({ expires, key, updateFn }: Update) => Promise<any>;
10
+ export default init;
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;
@@ -0,0 +1,10 @@
1
+ declare type Update = {
2
+ expires: number;
3
+ key: string;
4
+ updateFn: Function;
5
+ };
6
+ /**
7
+ * Retrieve a value from global memory.
8
+ */
9
+ declare const read: ({ key }: Update) => Promise<any>;
10
+ export default read;
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;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.1.112",
4
+ "version": "1.2.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"