safe-chain

3.0.5 • Public • Published

safe-chain

Travis CI Codecov

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

Think of it as Ruby safe operator or CoffeeScript existential operator, implemented as a simple function in JavaScript.

Installation

npm install safe-chain

Usage

Nested value

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

Nested function

// 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


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

Readme

Keywords

none

Package Sidebar

Install

npm i safe-chain

Weekly Downloads

37

Version

3.0.5

License

MIT

Unpacked Size

43.6 kB

Total Files

7

Last publish

Collaborators

  • caiogondim