Giter Club home page Giter Club logo

zealit's Introduction

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 = require('zealit')

const ref = { foo: true, bar: undefined }
ref.foo // true
ref.bar // undefined
ref.baz // undefined

// default behavior
const zealed = zealit(ref)
zealed.foo // true
zealed.bar // undefined, no Error thrown
zealed.baz // throws a ReferenceError

// "freeze" option
const myConstants = zealit({
    PI: 3.14159265,
    nbMsInOneDay: 1000 * 60 * 60 * 24,
}, { freeze: true })
myConstants.PI // 3.14159265
myConstants.Pi // throws a ReferenceError
myConstants.nbMsInOneDay = 39 // throws a TypeError

// "ignore" option
const foo = zealit({}, { ignore: 'bar' })
foo.bar // undefined, no Error thrown
foo.baz // throws a ReferenceError

// "clone" option
const bar = { baz: { yo: 1 } }
const baz = zealit(bar, { clone: true })
bar.baz.YO // undefined as bar was deeply cloned by zealit
baz.baz.YO // throws a ReferenceError

// "strict" option
const foo = zealit({ x: undefined }, { strict: false })
foo.x // undefined, no Error thrown
foo.y // throws a ReferenceError
const bar = zealit({ x: undefined }, { strict: true })
bar.x // throws a ReferenceError
bar.y // 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 Object
  • option <Object>
    • freeze <boolean> If true, the object is freezed as the same time via Object.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 list zealit.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): calls fn function with the ReferenceError as argument in place of throw ReferenceError
      • false: throw ReferenceError (default behavior)
      • true: doesn't throw ReferenceError
    • disable <boolean> If true, zealit does nothing. If provided, this local option will take precedence over the global option.
    • strict <boolean> If true, zealit throws a ReferenceError if property exists with the undefined value.

zealit.option

Object to expose global options, applies to all zealed objects.

  • freeze <boolean> If true, zealit will freeze objects as the same time via Object.freeze. Defaults to false. If provided, the local option will take precedence over this global option.

    zealit.option.freeze = true
    const foo = zealit({ bar: true })
    foo.bar = 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 of zealit.option.ignore

    const foo = zealit({ bar: true })
    foo.baz // throws a ReferenceError
    
    zealit.option.ignore.push('bar')
    foo.baz // 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): calls fn function with the ReferenceError as argument in place of throw ReferenceError
    • false: throw ReferenceError (default behavior)
    • true: doesn't throw ReferenceError
    const foo = zealit({ bar: true })
    zealit.option.catch = (err) => { console.log('gotcha', err) }
    foo.baz // return undefined and console.log('gotcha', ReferenceError)
  • disable <boolean> If true, zealit does nothing, simply return obj itself.

    zealit.option.disable = true
    const foo = { bar: true }
    
    // same thing
    const baz = zealit(foo)
    const baz = foo
  • strict <boolean> If true, zealit throws a ReferenceError if property has the undefined value, even if property exists. By default, zealit throws a ReferenceError only if property doesn't exist.

    zealit.option.strict = true
    const foo = zealit({ bar: undefined })
    foo.bar // throws a ReferenceError

Installation

Using npm:

$ npm install zealit --save

In Node.js:

const zealit = require('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?

zealit's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

zealit's Issues

Could the code be simpler?

Hi! I found your project following your answer on stackoverflow to the problem, we also tried to solve.
The idea of using Proxy is quite cool.
But when reading the source code I have a feeling that it is a bit overcomplicated.
In parcticular, once you've gone past

            if (v !== undefined) {
                return v
            }

we know that v === undefined so I do not really understand why do we have to check if key is "toJSON". I believe that if key==="toJSON" then then v would not be undefined for most of the time, and if it would be undefined, then it would be better to know about it.

Perhaps I'm confused here, so I would appreciate the example in which we need such a complicated thing, and not something super simple like if(key in target) return target[key]; else throw ... ?

Use zealit for all objects automatically

As I understand it, you manually have to wrap each object you want to protect in zealit objects.

Is there any way to use zealit for all my objects in a class, in a file, or similar, without having to manually specify it each time?

I assume this could be done by overloading the Object type?

However, I'm new to javascript, so I might be approaching it the wrong way.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.