# safe-chain

> No more crazy checks to safely get a nested value inside an object

Latest version **3.0.5** (published 2018-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install safe-chain
pnpm add safe-chain
yarn add safe-chain
bun add safe-chain
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.5 |
| Published | 2018-03-13 |
| First published | 2017-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 42.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Caio Gondim |
| Maintainers | caiogondim |

## Links

- npm: https://www.npmjs.com/package/safe-chain
- npm.io page: https://npm.io/package/safe-chain

## Recent versions

- 3.0.5 (latest) — 2018-03-13
- 3.0.4 — 2018-01-21
- 3.0.3 — 2018-01-21
- 3.0.2 — 2018-01-21
- 3.0.1 — 2017-03-22
- 3.0.0 — 2017-03-22

## README

<img src="http://rawgit.com/caiogondim/safe-chain.js/master/img/icon.svg">

# safe-chain

<div>
<img src="http://travis-ci.org/caiogondim/safe-chain.js.svg?branch=master" alt="Travis CI"> <img src="http://img.badgesize.io/caiogondim/safe-chain.js/master/src/index.js?compression=gzip"> <img src="https://codecov.io/gh/caiogondim/obstructed.js/branch/master/graph/badge.svg" alt="Codecov"> <a href="https://www.npmjs.com/package/safe-chain"><img src="https://img.shields.io/npm/v/safe-chain.svg" /></a>
</div>

<br>

No more crazy checks to safely get a nested value inside an object.

Think of it as [Ruby safe operator](https://irb.rocks/ruby-safe-operator/) or
[CoffeeScript existential
operator](http://valve.github.io/blog/2013/07/13/existential-operator-in-coffeescript/),
implemented as a simple function in JavaScript.

## Installation

```bash
npm install safe-chain
```

## Usage

### Nested value

```js
// Before
const nestedVal = (
  obj &&
  obj.lorem &&
  obj.lorem.ipsum &&
  obj.lorem.ipsum.dolor
)

// After
const nestedVal = safeChain(obj, `lorem.ipsum.dolor`)
```

### Nested function

```js
// Before
const nestedFuncVal = (
  obj &&
  obj.lorem &&
  obj.lorem.ipsum &&
  obj.lorem.ipsum.dolor &&
  typeof obj.lorem.ipsum.dolor === 'function'
    ? obj.lorem.ipsum.dolor()
    : undefined
)

// After
const nestedFuncVal = safeChain(obj, `lorem.ipsum.dolor`)()
```

## Credits

- [Ruby safe operator](https://irb.rocks/ruby-safe-operator/)
- [CoffeeScript existential operator](http://valve.github.io/blog/2013/07/13/existential-operator-in-coffeescript/)
- Icon by Martin Chapman Fromm from the Noun Project

---

[caiogondim.com](https://caiogondim.com) &nbsp;&middot;&nbsp;
GitHub [@caiogondim](https://github.com/caiogondim) &nbsp;&middot;&nbsp;
Twitter [@caio_gondim](https://twitter.com/caio_gondim)

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