forgot one ts in utils

This commit is contained in:
Olivier Gagnon 2021-09-24 22:04:30 -04:00
parent 7b6f9293c7
commit 76e6cb4ecc
2 changed files with 16 additions and 26 deletions

@ -1,10 +0,0 @@
interface IReviverValue {
ctor: string;
data: any;
}
export function Generic_fromJSON<T>(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;
}

@ -1,12 +1,16 @@
/* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */ /* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */
export interface IReviverValue {
ctor: string;
data: any;
}
// A generic "smart reviver" function. // A generic "smart reviver" function.
// Looks for object values with a `ctor` property and // Looks for object values with a `ctor` property and
// a `data` property. If it finds them, and finds a matching // a `data` property. If it finds them, and finds a matching
// constructor that has a `fromJSON` property on it, it hands // constructor that has a `fromJSON` property on it, it hands
// off to that `fromJSON` fuunction, passing in the value. // off to that `fromJSON` fuunction, passing in the value.
function Reviver(key, value) { export function Reviver(key: string, value: IReviverValue | null) {
var ctor;
if (value == null) { if (value == null) {
console.log("Reviver WRONGLY called with key: " + key + ", and value: " + value); console.log("Reviver WRONGLY called with key: " + key + ", and value: " + value);
return 0; return 0;
@ -20,7 +24,7 @@ function Reviver(key, value) {
return value.data; 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") { if (typeof ctor === "function" && typeof ctor.fromJSON === "function") {
return ctor.fromJSON(value); return ctor.fromJSON(value);
@ -28,7 +32,9 @@ function Reviver(key, value) {
} }
return 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 // A generic "toJSON" function that creates the data expected
// by Reviver. // 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.) // regardless of whether it's an "own" property.)
// Returns: The structure (which will then be turned into a string // Returns: The structure (which will then be turned into a string
// as part of the JSON.stringify algorithm) // as part of the JSON.stringify algorithm)
function Generic_toJSON(ctorName, obj, keys) { export function Generic_toJSON(ctorName: string, obj: any, keys?: string[]): IReviverValue {
var data, key;
if (!keys) { if (!keys) {
keys = Object.keys(obj); // Only "own" properties are included keys = Object.keys(obj); // Only "own" properties are included
} }
data = {}; const data: any = {};
for (let index = 0; index < keys.length; ++index) { for (let index = 0; index < keys.length; ++index) {
key = keys[index]; const key = keys[index];
data[key] = obj[key]; data[key] = obj[key];
} }
return { ctor: ctorName, data: data }; return { ctor: ctorName, data: data };
@ -63,14 +67,10 @@ function Generic_toJSON(ctorName, obj, keys) {
// `ctor` The constructor to call // `ctor` The constructor to call
// `data` The data to apply // `data` The data to apply
// Returns: The object // Returns: The object
function Generic_fromJSON(ctor, data) { export function Generic_fromJSON<T>(ctor: new () => T, data: any): T {
var obj, name; const obj: any = new ctor();
for (const name in data) {
obj = new ctor();
for (name in data) {
obj[name] = data[name]; obj[name] = data[name];
} }
return obj; return obj;
} }
export { Reviver, Generic_toJSON, Generic_fromJSON };