JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 26
  • Score
    100M100P100Q52875F
  • License MIT

Minimal library for safe property access

Package Exports

  • safe-access-check

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (safe-access-check) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

safe-access-check

Build Status NPM version Dependency Status npm

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

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([], '+', {})
// Throws TypeError: 'Unexpected coercion of type "object" and
// type "array" using "+" operator'

NaN + undefined // NaN

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



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

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

safePropertyAccess(['foo', 'bar', '_MOO_', 'baz'], obj);
// Throws TypeError: '"foo.bar._MOO_" is not defined'



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

obj.woo[1] // undefined

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