From 76e6cb4ecc50ab7a1ec406af68973db18aadc915 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Fri, 24 Sep 2021 22:04:30 -0400 Subject: [PATCH] forgot one ts in utils --- utils/JSONReviver.d.ts | 10 -------- utils/{JSONReviver.js => JSONReviver.ts} | 32 ++++++++++++------------ 2 files changed, 16 insertions(+), 26 deletions(-) delete mode 100644 utils/JSONReviver.d.ts rename utils/{JSONReviver.js => JSONReviver.ts} (80%) diff --git a/utils/JSONReviver.d.ts b/utils/JSONReviver.d.ts deleted file mode 100644 index 8676ab325..000000000 --- a/utils/JSONReviver.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -interface IReviverValue { - ctor: string; - data: any; -} -export function Generic_fromJSON(ctor: new () => T, data: any): T; -export function Generic_toJSON(ctorName: string, obj: any, keys?: string[]): string; -export function Reviver(key, value: IReviverValue); -export namespace Reviver { - export let constructors: any; -} diff --git a/utils/JSONReviver.js b/utils/JSONReviver.ts similarity index 80% rename from utils/JSONReviver.js rename to utils/JSONReviver.ts index e3886abf6..045507f18 100644 --- a/utils/JSONReviver.js +++ b/utils/JSONReviver.ts @@ -1,12 +1,16 @@ /* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */ +export interface IReviverValue { + ctor: string; + data: any; +} + // A generic "smart reviver" function. // Looks for object values with a `ctor` property and // a `data` property. If it finds them, and finds a matching // constructor that has a `fromJSON` property on it, it hands // off to that `fromJSON` fuunction, passing in the value. -function Reviver(key, value) { - var ctor; +export function Reviver(key: string, value: IReviverValue | null) { if (value == null) { console.log("Reviver WRONGLY called with key: " + key + ", and value: " + value); return 0; @@ -20,7 +24,7 @@ function Reviver(key, value) { return value.data; } - ctor = Reviver.constructors[value.ctor] || window[value.ctor]; + const ctor = Reviver.constructors[value.ctor]; if (typeof ctor === "function" && typeof ctor.fromJSON === "function") { return ctor.fromJSON(value); @@ -28,7 +32,9 @@ function Reviver(key, value) { } return value; } -Reviver.constructors = {}; // A list of constructors the smart reviver should know about +export namespace Reviver { + export const constructors: { [key: string]: any } = {}; +} // A generic "toJSON" function that creates the data expected // by Reviver. @@ -41,16 +47,14 @@ Reviver.constructors = {}; // A list of constructors the smart reviver should kn // regardless of whether it's an "own" property.) // Returns: The structure (which will then be turned into a string // as part of the JSON.stringify algorithm) -function Generic_toJSON(ctorName, obj, keys) { - var data, key; - +export function Generic_toJSON(ctorName: string, obj: any, keys?: string[]): IReviverValue { if (!keys) { keys = Object.keys(obj); // Only "own" properties are included } - data = {}; + const data: any = {}; for (let index = 0; index < keys.length; ++index) { - key = keys[index]; + const key = keys[index]; data[key] = obj[key]; } return { ctor: ctorName, data: data }; @@ -63,14 +67,10 @@ function Generic_toJSON(ctorName, obj, keys) { // `ctor` The constructor to call // `data` The data to apply // Returns: The object -function Generic_fromJSON(ctor, data) { - var obj, name; - - obj = new ctor(); - for (name in data) { +export function Generic_fromJSON(ctor: new () => T, data: any): T { + const obj: any = new ctor(); + for (const name in data) { obj[name] = data[name]; } return obj; } - -export { Reviver, Generic_toJSON, Generic_fromJSON };