ethers.js/utils/properties.js
2018-10-14 19:05:38 -04:00

139 lines
4.2 KiB
JavaScript

'use strict';
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var errors = __importStar(require("../errors"));
function defineReadOnly(object, name, value) {
Object.defineProperty(object, name, {
enumerable: true,
value: value,
writable: false,
});
}
exports.defineReadOnly = defineReadOnly;
// There are some issues with instanceof with npm link, so we use this
// to ensure types are what we expect.
function setType(object, type) {
Object.defineProperty(object, '_ethersType', { configurable: false, value: type, writable: false });
}
exports.setType = setType;
function isType(object, type) {
return (object && object._ethersType === type);
}
exports.isType = isType;
function resolveProperties(object) {
var result = {};
var promises = [];
Object.keys(object).forEach(function (key) {
var value = object[key];
if (value instanceof Promise) {
promises.push(value.then(function (value) {
result[key] = value;
return null;
}));
}
else {
result[key] = value;
}
});
return Promise.all(promises).then(function () {
return result;
});
}
exports.resolveProperties = resolveProperties;
function checkProperties(object, properties) {
if (!object || typeof (object) !== 'object') {
errors.throwError('invalid object', errors.INVALID_ARGUMENT, {
argument: 'object',
value: object
});
}
Object.keys(object).forEach(function (key) {
if (!properties[key]) {
errors.throwError('invalid object key - ' + key, errors.INVALID_ARGUMENT, {
argument: 'transaction',
value: object,
key: key
});
}
});
}
exports.checkProperties = checkProperties;
function shallowCopy(object) {
var result = {};
for (var key in object) {
result[key] = object[key];
}
return result;
}
exports.shallowCopy = shallowCopy;
var opaque = { boolean: true, number: true, string: true };
function deepCopy(object, frozen) {
// 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)) {
var result = object.map(function (item) { return deepCopy(item, frozen); });
if (frozen) {
Object.freeze(result);
}
return result;
}
if (typeof (object) === 'object') {
// Some internal objects, which are already immutable
if (isType(object, 'BigNumber')) {
return object;
}
if (isType(object, 'Description')) {
return object;
}
if (isType(object, 'Indexed')) {
return object;
}
var result = {};
for (var key in object) {
var value = object[key];
if (value === undefined) {
continue;
}
defineReadOnly(result, key, deepCopy(value, frozen));
}
if (frozen) {
Object.freeze(result);
}
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;
// See: https://github.com/isaacs/inherits/blob/master/inherits_browser.js
function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
function inheritable(parent) {
return function (child) {
inherits(child, parent);
defineReadOnly(child, 'inherits', inheritable(child));
};
}
exports.inheritable = inheritable;