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 */
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<T>(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 };