Created
December 17, 2018 11:42
-
-
Save VanDalkvist/b82bee64dd326dbb66dcae0b9a0e1278 to your computer and use it in GitHub Desktop.
Strict storage with errors throwing when property is not define and already exists.
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
function buildStrictStorage() { | |
const storage = {}; | |
return new Proxy(storage, { | |
get: (target, property) => { | |
if (!Reflect.has(target, property)) { | |
throw new Error(`Label "${property}" is missing.`); | |
} | |
return Reflect.get(target, property); | |
}, | |
set: (target, property, value) => { | |
if (Reflect.has(target, property)) { | |
throw new Error(`Label "${property}" already exists.`); | |
} | |
return Reflect.set(target, property, value); | |
}, | |
has: function() { | |
return false; | |
}, | |
ownKeys: function() { | |
return []; | |
}, | |
getOwnPropertyDescriptor: function() { | |
return false; | |
} | |
}); | |
} | |
export default buildStrictStorage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment