ethers.js/utils/properties.js
2018-08-21 13:13:52 +02:00

110 lines
3.1 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) {
if (object === undefined || object === null || opaque[typeof (object)]) {
return object;
}
if (Array.isArray(object)) {
var result_1 = [];
object.forEach(function (item) {
result_1.push(deepCopy(item, frozen));
});
if (frozen) {
Object.freeze(result_1);
}
return result_1;
}
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;
}
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;