safe-chain
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
// Beforeconst nestedVal = obj && objlorem && objloremipsum && objloremipsumdolor // Afterconst nestedVal =
Nested function
// Beforeconst nestedFuncVal = obj && objlorem && objloremipsum && objloremipsumdolor && typeof objloremipsumdolor === 'function' ? objloremipsum : undefined // Afterconst nestedFuncVal =
Credits
- Ruby safe operator
- CoffeeScript existential operator
- Icon by Martin Chapman Fromm from the Noun Project
caiogondim.com · GitHub @caiogondim · Twitter @caio_gondim