ethers.js/utils/properties.js
2018-10-01 17:34:44 -04:00

113 lines
3.3 KiB
JavaScript

'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
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 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;