Skip to content

Instantly share code, notes, and snippets.

@VanDalkvist
Created December 17, 2018 11:42
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 VanDalkvist/b82bee64dd326dbb66dcae0b9a0e1278 to your computer and use it in GitHub Desktop.
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.
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