snootforge

chee/exceptional-objects

create objects that throw exceptions when accessing missing keys

exceptional objects

create strict objects that will throw an exception if you try to get a property from them that they donโ€™t have.

uses Proxy.

hereโ€™s some typed up:

  1. const exceptional = require('exceptional-objects')
  2. const dog = exceptional({bark: 'woof', color: 'tan'})
  3. dog.bark
  4. //-> 'woof'
  5. dog.color
  6. //-> 'tan'
  7. dog.name
  8. //-> throws "Error: Object does not contain name"

you can still set things like normal:

  1. const lol = exceptional({})
  2. console.log(lol.dog) //-> throws "Error: Object does not contain dog"
  3. lol.dog = 'henry'
  4. console.log(lol.dog) //-> henry