ethers.js/utils/properties.js

113 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2018-06-13 22:39:39 +03:00
'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;
2018-07-26 04:20:21 +03:00
// 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) {
2018-08-13 19:01:44 +03:00
return (object && object._ethersType === type);
2018-07-26 04:20:21 +03:00
}
exports.isType = isType;
2018-06-13 22:39:39 +03:00
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;
2018-07-03 23:44:05 +03:00
return null;
}));
2018-06-13 22:39:39 +03:00
}
else {
result[key] = value;
}
});
return Promise.all(promises).then(function () {
return result;
2018-06-13 22:39:39 +03:00
});
}
exports.resolveProperties = resolveProperties;
2018-06-15 11:18:17 +03:00
function shallowCopy(object) {
var result = {};
for (var key in object) {
result[key] = object[key];
}
return result;
}
exports.shallowCopy = shallowCopy;
2018-08-21 14:13:52 +03:00
var opaque = { boolean: true, number: true, string: true };
function deepCopy(object, frozen) {
2018-10-02 00:34:44 +03:00
// Opaque objects are not mutable, so safe to copy by assignment
2018-08-21 14:13:52 +03:00
if (object === undefined || object === null || opaque[typeof (object)]) {
return object;
}
2018-10-02 00:34:44 +03:00
// Arrays are mutable, so we need to create a copy
2018-08-21 14:13:52 +03:00
if (Array.isArray(object)) {
2018-10-02 00:34:44 +03:00
var result = object.map(function (item) { return deepCopy(item, frozen); });
2018-08-21 14:13:52 +03:00
if (frozen) {
2018-10-02 00:34:44 +03:00
Object.freeze(result);
2018-08-21 14:13:52 +03:00
}
2018-10-02 00:34:44 +03:00
return result;
2018-08-21 14:13:52 +03:00
}
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;
}
2018-10-02 00:34:44 +03:00
// The function type is also immutable, so safe to copy by assignment
if (typeof (object) === 'function') {
return object;
}
2018-08-21 14:13:52 +03:00
throw new Error('Cannot deepCopy ' + typeof (object));
2018-06-15 11:18:17 +03:00
}
2018-08-21 14:13:52 +03:00
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;