vamoot
Version:
Vamoot creates simple immutable objects
44 lines (43 loc) • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const assert = require("assert");
const freeze_existing_props_1 = require("freeze-existing-props");
class VamootProxy {
constructor(v) {
if (arguments.length > 0) {
assert(v && typeof v === 'object', 'First argument to constructor must be a non-null object.');
}
this.__vamootProxyInstance = true;
this.__internalValue = v || {};
this.__alreadySet = {};
Object.freeze(this);
}
get(prop) {
return this.__internalValue[prop];
}
read(prop) {
return this.__internalValue[prop];
}
getAll() {
return this.__internalValue;
}
clone() {
return new VamootProxy(Object.create(this.__internalValue));
}
set(prop, val) {
if (!this.__alreadySet[prop]) {
this.__alreadySet[prop] = true;
Object.defineProperty(this.__internalValue, prop, {
writable: false,
value: (val && typeof val === 'object') ? freeze_existing_props_1.freezeExistingProps(val) : val
});
return true;
}
throw new Error(`property '${prop}' has already been set.`);
}
;
}
exports.VamootProxy = VamootProxy;
exports.r2gSmokeTest = function () {
return true;
};