@boost/core
Version:
Robust pipeline for creating dev tools that separate logic into routines and tasks.
39 lines (38 loc) • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@boost/common");
class Context {
/**
* Create a new instance of the current context and shallow clone all properties.
*/
clone(...args) {
// @ts-ignore Not sure how to reflect the constructor of sub-classes
const context = new this.constructor(...args);
// Copy enumerable properties
Object.keys(this).forEach(key => {
const prop = key;
let value = this[prop];
if (Array.isArray(value)) {
value = [...value];
}
else if (value instanceof Map) {
value = new Map(value);
}
else if (value instanceof Set) {
value = new Set(value);
}
else if (value instanceof Date) {
value = new Date(value.getTime());
}
else if (common_1.isObject(value)) {
// Dont dereference instances, only plain objects
if (value.constructor === Object) {
value = Object.assign({}, value);
}
}
context[prop] = value;
});
return context;
}
}
exports.default = Context;