You can achieve a similar result with simple functions like:
const O = (val, value = {}) => val && (typeof val === 'object') ? val : value const A = (val, value = []) => val && Array.isArray(val) ? val : value const S = (val, value = '') => val && (typeof val === 'string') ? val : value const N = (val, value = 0) => isNaN(val) ? value : val const NZ = (val, value = 1) => val === 0 || isNaN(val) ? value === 0 ? 1 : value : val const F = (f, f0 = () => null) => f ? f : f0 const a = {} console.log(O(a.b)) // {} console.log(A(O(a.b).c)) // [] console.log(NZ(a)) // 1 (non-zero number) console.log(N(a)) // 0 (number) console.log(S(a, 'hello')) // hello console.log(F(a.b)(123)) // null
It's not much more overhead and it works today.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can achieve a similar result with simple functions like:
It's not much more overhead and it works today.