2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
export function defineReadOnly(object: any, name: string, value: any): void {
|
|
|
|
Object.defineProperty(object, name, {
|
|
|
|
enumerable: true,
|
|
|
|
value: value,
|
|
|
|
writable: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-12 06:42:35 +03:00
|
|
|
// Crawl up the constructor chain to find a static method
|
|
|
|
export function getStatic<T>(ctor: any, key: string): T {
|
|
|
|
for (let i = 0; i < 32; i++) {
|
|
|
|
if (ctor[key]) { return ctor[key]; }
|
|
|
|
if (!ctor.prototype || typeof(ctor.prototype) !== "object") { break; }
|
|
|
|
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-28 00:45:51 +03:00
|
|
|
type Result = { key: string, value: any};
|
2019-05-15 01:25:46 +03:00
|
|
|
export function resolveProperties(object: any): Promise<any> {
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const promises: Array<Promise<Result>> = Object.keys(object).map((key) => {
|
|
|
|
const value = object[key];
|
2019-07-28 00:45:51 +03:00
|
|
|
|
|
|
|
if (!(value instanceof Promise)) {
|
|
|
|
return Promise.resolve({ key: key, value: value });
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
2019-07-28 00:45:51 +03:00
|
|
|
|
|
|
|
return value.then((value) => {
|
|
|
|
return { key: key, value: value };
|
|
|
|
});
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
|
2019-07-28 00:45:51 +03:00
|
|
|
return Promise.all(promises).then((results) => {
|
2019-11-01 17:33:51 +03:00
|
|
|
const result: any = { };
|
2019-07-28 00:45:51 +03:00
|
|
|
return results.reduce((accum, result) => {
|
|
|
|
accum[result.key] = result.value;
|
|
|
|
return accum;
|
|
|
|
}, result);
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function checkProperties(object: any, properties: { [ name: string ]: boolean }): void {
|
|
|
|
if (!object || typeof(object) !== "object") {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid object", "object", object);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(object).forEach((key) => {
|
|
|
|
if (!properties[key]) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid object key - " + key, "transaction:" + key, object);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function shallowCopy(object: any): any {
|
2019-11-01 17:33:51 +03:00
|
|
|
const result: any = {};
|
|
|
|
for (const key in object) { result[key] = object[key]; }
|
2019-05-15 01:25:46 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const opaque: { [key: string]: boolean } = { bigint: true, boolean: true, number: true, string: true };
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-06-12 00:52:42 +03:00
|
|
|
// Returns a new copy of object, such that no properties may be replaced.
|
|
|
|
// New properties may be added only to objects.
|
|
|
|
export function deepCopy(object: any): any {
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
// Opaque objects are not mutable, so safe to copy by assignment
|
|
|
|
if (object === undefined || object === null || opaque[typeof(object)]) { return object; }
|
|
|
|
|
|
|
|
// Arrays are mutable, so we need to create a copy
|
|
|
|
if (Array.isArray(object)) {
|
2019-06-12 00:52:42 +03:00
|
|
|
return Object.freeze(object.map((item) => deepCopy(item)));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(object) === "object") {
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
// Immutable objects are safe to just use
|
|
|
|
if (Object.isFrozen(object)) { return object; }
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const result: { [ key: string ]: any } = {};
|
|
|
|
for (const key in object) {
|
|
|
|
const value = object[key];
|
2019-05-15 01:25:46 +03:00
|
|
|
if (value === undefined) { continue; }
|
2019-06-12 00:52:42 +03:00
|
|
|
defineReadOnly(result, key, deepCopy(value));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The function type is also immutable, so safe to copy by assignment
|
|
|
|
if (typeof(object) === "function") {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("Cannot deepCopy " + typeof(object));
|
|
|
|
}
|
|
|
|
|
2020-01-08 03:44:45 +03:00
|
|
|
export class Description<T = any> {
|
|
|
|
constructor(info: T) {
|
2019-11-01 17:33:51 +03:00
|
|
|
for (const key in info) {
|
2019-06-12 00:52:42 +03:00
|
|
|
(<any>this)[key] = deepCopy(info[key]);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|