Skip to content

Instantly share code, notes, and snippets.

@aloerina01
Last active March 14, 2017 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloerina01/9c931d3918242767b9c9729a63b0952f to your computer and use it in GitHub Desktop.
Save aloerina01/9c931d3918242767b9c9729a63b0952f to your computer and use it in GitHub Desktop.
ES6 Proxy - EnumBuilder
export default class {
constructor() {
this._obj;
}
object(obj) {
this._obj = obj;
return this;
}
keyValuePair(key, value) {
if (!this._obj) {
this._obj = {};
}
this._obj[key] = value;
return this;
}
build() {
if (!this._obj) {
throw new ReferenceError(`Target object is not exist. _obj=${this._obj}`);
}
return new Proxy(this._obj, handler);
}
}
const handler = {
set (obj, prop, value) {
throw TypeError('Enum is read only.');
},
get (obj, prop) {
if (!(prop in obj)) {
throw new ReferenceError(`Unknown enum key "${prop}"`);
}
return Reflect.get(obj, prop);
},
deleteProperty (obj, prop) {
throw new TypeError('Enum is read only');
}
}
import EnumBuilder from './EnumBuilder';
const Type = {
INSERT: 'insert',
DELETE: 'delete',
UPDATE: 'update'
}
export default new EnumBuilder().object(Type).build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment