GithubHelp home page GithubHelp logo

amilajack / safe-access-check Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 85 KB

A micro library that prevents unsafe coercion and property access in javascript

JavaScript 100.00%
javascript coercion check safe

safe-access-check's Introduction

safe-access-check

Build Status NPM version Dependency Status npm

⚠️ Experimental. Intended to be used by compilers and code checkers ⚠️

Todos

  • Existential check of enumerable properties
  • Optional logging of values recieved
  • Clearer, more consistent error messages

Installation

npm install --save-dev safe-access-check

Usage

import { safeCoerce, safePropertyAccess } from 'safe-access-check';

// ------------------------------------------------
// 1. Usage as an expression
// ------------------------------------------------
let some = moo + '10' // 'moo10'
some = safeCoerce('moo', '+', 10) // 'moo10'

// ------------------------------------------------
// 2. Usage for coercion safeguard
// ------------------------------------------------
[] + {} // "[object Object]"

safeCoerce([], '+', {})
// TypeError: 'Unexpected coercion of type "Object" and
// type "Array" using "+" operator'

NaN + undefined // NaN

safeCoerce(NaN, '+', undefined);
// TypeError: Unexpected coercion of type "NaN" and type
// "undefined" using "+" operator

safeCoerce(new String('12'), '>', 12);
// TypeError: Unexpected comparison of type "String" and type
// "number" using ">" operator

// ------------------------------------------------
// 3. Usage for better undefined propagation errors
// ------------------------------------------------
const obj = {
  foo: {
    bar: {
      baz: false
    }
  }
}

obj.foo.bar._MOO_.baz;
// TypeError: 'Cannot read property 'baz' of undefined'

safePropertyAccess(['foo', 'bar', '_MOO_', 'baz'], obj);
// TypeError: Property "_MOO_" does not exist in "Object.foo._MOO_"

// ------------------------------------------------
// 4. Usage as out of bounds check
// ------------------------------------------------
const obj = {
  woo: ['']
}

obj.woo[1] // undefined

safePropertyAccess(['woo', 1], obj)
// TypeError: '"woo[1]" is out of bounds'

safe-access-check's People

Contributors

amilajack avatar

Stargazers

 avatar

Watchers

 avatar

safe-access-check's Issues

Idea: Provide links to errors

Some of the error messages may be confusing to beginners. It would be nice if short url could be added to the output of each error message in order to explain the reason of the error message.

Idea: Log values validated on failure

safePropertyAccess(
  ['foo', 'bar', 'baz'],
  // obj without 'baz'
  {
    'foo': {
      'bar': {}  
    }
  }
)
TypeError: "Object.foo.bar.baz" is not defined' on object:
{
   'foo': {
     'bar': {}  
   }
}

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.