Defensive programming helper methods that will change your life
npm install safe-utilsSimple helper methods that lets you access properties without worrying about defensive programming.
This package has been used without issues in production both in Node.js and in browsers (React style apps) for aprox 4 years.
NOTE! There is a performane hit when safeGet catches errors, but this hasn't been a problem in actual applications.
``JavaScript
const {safeGet} = require('safe-utils')
var test = undefined
safeGet(() => test.hallo)
// returns undefined
var test = undefined
safeGet(() => test.hallo, 'not found')
// returns 'not found'
var test = {hallo: 'world'}
safeGet(() => test.hallo)
// returns 'world'
`
`JavaScript
const {safeJoin} = require('safe-utils')
var user = {
firstName: 'John',
lastName: ''
}
safeJoin([user.firstName, user.lastName], ' ')
// returns 'John'
var user = {
firstName: 'John',
lastName: 'Doodle'
}
safeJoin([user.firstName, user.lastName], ' ')
// returns 'John Doodle'
// Using safeGet style method calls
safeJoin([() => user.firstName, () => user.lastName], ' ')
// returns 'John Doodle'
safeJoin([() => not.existing.prop, () => user.lastName], ' ')
// returns 'Doodle'
`
When used with await, this helper gives you more readable code by removing the nested catch.
`JavaScript``
const {err, res} = await safeCatch(myPromisFunc)(params, to, promiseFunc)