Prevent getting nonexistent property by throwing a ReferenceError, using ES6 proxies.
In other words, throw an error if reading an object property that doesn't exist.
Avoid typo or incomplete renaming/refactor.
Usefull to combine with Object.freeze
to declare constants.
Usage
const zealit = const ref = foo: true bar: undefined reffoo // truerefbar // undefinedrefbaz // undefined // default behaviorconst zealed = zealedfoo // truezealedbar // undefined, no Error thrownzealedbaz // throws a ReferenceError // "freeze" optionconst myConstants = myConstantsPI // 3.14159265myConstantsPi // throws a ReferenceErrormyConstantsnbMsInOneDay = 39 // throws a TypeError // "ignore" optionconst foo = foobar // undefined, no Error thrownfoobaz // throws a ReferenceError // "clone" optionconst bar = baz: yo: 1 const baz = barbazYO // undefined as bar was deeply cloned by zealitbazbazYO // throws a ReferenceError // "strict" optionconst foo = foox // undefined, no Error thrownfooy // throws a ReferenceErrorconst bar = barx // throws a ReferenceErrorbary // throws a ReferenceError
Methods and options
zealit(obj[, option])
Updates obj
recursively and returns a zealed version of the object.
obj
<any> Any JavaScript primitive or Objectoption
<Object>freeze
<boolean> Iftrue
, the object is freezed as the same time viaObject.freeze
. If provided, this local option will take precedence over the global option.ignore
<String|Array> Properties to ignore, no exception will be thrown for these properties as they keep behaving like vanilla JavaScript properties. The current zealed object will not throw exception for properties listed locally nor properties of the global listzealit.option.ignore
catch
<boolean|Function> to override the default behavior (throwing a ReferenceError). If provided, this local option will take precedence over the global option.fn(err)
: callsfn
function with the ReferenceError as argument in place of throw ReferenceErrorfalse
: throw ReferenceError (default behavior)true
: doesn't throw ReferenceError
disable
<boolean> Iftrue
,zealit
does nothing. If provided, this local option will take precedence over the global option.strict
<boolean> Iftrue
,zealit
throws a ReferenceError if property exists with theundefined
value.
zealit.option
Object to expose global options, applies to all zealed objects.
-
freeze
<boolean> Iftrue
,zealit
will freeze objects as the same time viaObject.freeze
. Defaults tofalse
. If provided, the local option will take precedence over this global option.zealitoptionfreeze = trueconst foo =foobar = false // throws a TypeError -
ignore
<Array> Properties to ignore, no exception will be thrown for these properties as they keep behaving like vanilla JavaScript properties. Applies to all zealed objects, even those was instancied before an update ofzealit.option.ignore
const foo =foobaz // throws a ReferenceErrorzealitoptionignorefoobaz // undefined -
catch
<boolean|Function> to override the default behavior (throwing a ReferenceError). If provided, the local option will take precedence over this global option.fn(err)
: callsfn
function with the ReferenceError as argument in place of throw ReferenceErrorfalse
: throw ReferenceError (default behavior)true
: doesn't throw ReferenceError
const foo =zealitoption { console }foobaz // return undefined and console.log('gotcha', ReferenceError) -
disable
<boolean> Iftrue
,zealit
does nothing, simply returnobj
itself.zealitoptiondisable = trueconst foo = bar: true// same thingconst baz =const baz = foo -
strict
<boolean> Iftrue
,zealit
throws a ReferenceError if property has theundefined
value, even if property exists. By default,zealit
throws a ReferenceError only if property doesn't exist.zealitoptionstrict = trueconst foo =foobar // throws a ReferenceError
Installation
Using npm:
$ npm install zealit --save
In Node.js:
const zealit =
Todo
- explain limitation (Promise, .length, lodash, hidden properties)
- more documentation about "clone" option (lose hidden properties vs update and keep those)
- option to rezeal a property
- test with more node version (v6.7.0 at the moment)
- option to disable recursion?