0.0.15 • Published 7 years ago

safe-access-check v0.0.15

Weekly downloads
7
License
MIT
Repository
github
Last release
7 years ago

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'
0.0.15

7 years ago

0.0.14

7 years ago

0.0.14-0

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.11-0

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.8-0

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.2-0

7 years ago

0.0.1

7 years ago