# safe-access-check

> Minimal library for safe property access

Latest version **0.0.15** (published 2017-04-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install safe-access-check
pnpm add safe-access-check
yarn add safe-access-check
bun add safe-access-check
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2017-04-14 |
| First published | 2017-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Amila Welihinda |
| Maintainers | amilajack |

## Links

- npm: https://www.npmjs.com/package/safe-access-check
- Repository: https://github.com/amilajack/safe-access-check
- Homepage: https://github.com/amilajack/safe-access-check#readme
- Issues: https://github.com/amilajack/safe-access-check/issues
- npm.io page: https://npm.io/package/safe-access-check

## Recent versions

- 0.0.15 (latest) — 2017-04-14
- 0.0.14-0 (next) — 2017-04-01
- 0.0.14 — 2017-04-10
- 0.0.13 — 2017-03-30
- 0.0.12 — 2017-03-28
- 0.0.11 — 2017-03-28
- 0.0.11-0 — 2017-03-28
- 0.0.10 — 2017-03-27
- 0.0.9 — 2017-03-22
- 0.0.8 — 2017-03-22
- 0.0.8-0 — 2017-03-22
- 0.0.7 — 2017-03-20
- 0.0.6 — 2017-03-20
- 0.0.5 — 2017-03-19
- 0.0.4 — 2017-03-19
- … 4 more at https://npm.io/package/safe-access-check/versions

## README

safe-access-check
=========
[![Build Status](https://travis-ci.org/amilajack/safe-access-check.svg?branch=master&maxAge=2592)](https://travis-ci.org/amilajack/safe-access-check)
[![NPM version](https://badge.fury.io/js/safe-access-check.svg?maxAge=2592)](http://badge.fury.io/js/safe-access-check)
[![Dependency Status](https://img.shields.io/david/amilajack/safe-access-check.svg?maxAge=2592)](https://david-dm.org/amilajack/safe-access-check)
[![npm](https://img.shields.io/npm/dm/safe-access-check.svg?maxAge=2592)](https://npm-stat.com/charts.html?package=safe-access-check)

**⚠️ 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
```bash
npm install --save-dev safe-access-check
```

## Usage
```js
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'
```

---
_Source: https://npm.io/package/safe-access-check · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
