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

110 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2019-08-02 09:10:58 +03:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-15 01:48:48 +03:00
function defineReadOnly(object, name, value) {
Object.defineProperty(object, name, {
enumerable: true,
value: value,
writable: false,
});
}
exports.defineReadOnly = defineReadOnly;
2019-06-12 08:01:04 +03:00
// Crawl up the constructor chain to find a static method
function getStatic(ctor, key) {
for (var 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;
}
exports.getStatic = getStatic;
2019-05-15 01:48:48 +03:00
function resolveProperties(object) {
2019-07-28 01:02:24 +03:00
var promises = Object.keys(object).map(function (key) {
2019-05-15 01:48:48 +03:00
var 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
}
2019-07-28 01:02:24 +03:00
return value.then(function (value) {
return { key: key, value: value };
});
2019-05-15 01:48:48 +03:00
});
2019-07-28 01:02:24 +03:00
return Promise.all(promises).then(function (results) {
var result = {};
return results.reduce(function (accum, result) {
accum[result.key] = result.value;
return accum;
}, result);
2019-05-15 01:48:48 +03:00
});
}
exports.resolveProperties = resolveProperties;
function checkProperties(object, properties) {
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(function (key) {
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
}
});
}
exports.checkProperties = checkProperties;
function shallowCopy(object) {
var result = {};
for (var key in object) {
result[key] = object[key];
}
return result;
}
exports.shallowCopy = shallowCopy;
2019-06-12 00:57:04 +03:00
var opaque = { bigint: true, boolean: true, number: true, string: true };
// Returns a new copy of object, such that no properties may be replaced.
// New properties may be added only to objects.
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)) {
2019-06-12 00:57:04 +03:00
return Object.freeze(object.map(function (item) { return 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;
}
var result = {};
for (var key in object) {
var value = object[key];
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;
}
throw new Error("Cannot deepCopy " + typeof (object));
}
exports.deepCopy = deepCopy;
var Description = /** @class */ (function () {
function Description(info) {
for (var key in info) {
2019-06-12 00:57:04 +03:00
this[key] = deepCopy(info[key]);
2019-05-15 01:48:48 +03:00
}
Object.freeze(this);
}
return Description;
}());
exports.Description = Description;