ethers.js/packages/properties/lib.esm/index.js

103 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
export function defineReadOnly(object, name, value) {
2019-05-15 01:48:48 +03:00
Object.defineProperty(object, name, {
enumerable: true,
value: value,
writable: false,
});
}
2019-06-12 08:01:04 +03:00
// Crawl up the constructor chain to find a static method
export function getStatic(ctor, key) {
for (let i = 0; i < 32; i++) {
2019-06-12 08:01:04 +03:00
if (ctor[key]) {
return ctor[key];
}
if (!ctor.prototype || typeof (ctor.prototype) !== "object") {
break;
}
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
}
return null;
}
export function resolveProperties(object) {
2019-11-20 12:57:38 +03:00
const promises = Object.keys(object).map((key) => {
const value = object[key];
2019-07-28 01:02:24 +03:00
if (!(value instanceof Promise)) {
return Promise.resolve({ key: key, value: value });
2019-05-15 01:48:48 +03:00
}
return value.then((value) => {
2019-07-28 01:02:24 +03:00
return { key: key, value: value };
});
2019-05-15 01:48:48 +03:00
});
return Promise.all(promises).then((results) => {
2019-11-20 12:57:38 +03:00
const result = {};
2020-02-07 02:21:34 +03:00
return (results.reduce((accum, result) => {
2019-07-28 01:02:24 +03:00
accum[result.key] = result.value;
return accum;
2020-02-07 02:21:34 +03:00
}, result));
2019-05-15 01:48:48 +03:00
});
}
export function checkProperties(object, properties) {
2019-05-15 01:48:48 +03:00
if (!object || typeof (object) !== "object") {
2019-08-02 09:10:58 +03:00
logger.throwArgumentError("invalid object", "object", object);
2019-05-15 01:48:48 +03:00
}
Object.keys(object).forEach((key) => {
2019-05-15 01:48:48 +03:00
if (!properties[key]) {
2019-08-02 09:10:58 +03:00
logger.throwArgumentError("invalid object key - " + key, "transaction:" + key, object);
2019-05-15 01:48:48 +03:00
}
});
}
export function shallowCopy(object) {
2019-11-20 12:57:38 +03:00
const result = {};
for (const key in object) {
2019-05-15 01:48:48 +03:00
result[key] = object[key];
}
return result;
}
2019-11-20 12:57:38 +03:00
const opaque = { bigint: true, boolean: true, number: true, string: true };
2019-06-12 00:57:04 +03:00
// Returns a new copy of object, such that no properties may be replaced.
// New properties may be added only to objects.
2020-02-07 02:21:34 +03:00
function _deepCopy(object) {
2019-05-15 01:48:48 +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)) {
return Object.freeze(object.map((item) => deepCopy(item)));
2019-05-15 01:48:48 +03:00
}
if (typeof (object) === "object") {
2019-06-12 00:57:04 +03:00
// Immutable objects are safe to just use
if (Object.isFrozen(object)) {
2019-05-15 01:48:48 +03:00
return object;
}
2019-11-20 12:57:38 +03:00
const result = {};
for (const key in object) {
const value = object[key];
2019-05-15 01:48:48 +03:00
if (value === undefined) {
continue;
}
2019-06-12 00:57:04 +03:00
defineReadOnly(result, key, deepCopy(value));
2019-05-15 01:48:48 +03:00
}
return result;
}
// The function type is also immutable, so safe to copy by assignment
if (typeof (object) === "function") {
return object;
}
2020-02-07 02:21:34 +03:00
logger.throwArgumentError(`Cannot deepCopy ${typeof (object)}`, "object", object);
}
export function deepCopy(object) {
return _deepCopy(object);
2019-05-15 01:48:48 +03:00
}
export class Description {
constructor(info) {
2019-11-20 12:57:38 +03:00
for (const key in info) {
2019-06-12 00:57:04 +03:00
this[key] = deepCopy(info[key]);
2019-05-15 01:48:48 +03:00
}
}
}