Last active
March 14, 2017 08:37
-
-
Save aloerina01/9c931d3918242767b9c9729a63b0952f to your computer and use it in GitHub Desktop.
ES6 Proxy - EnumBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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