divyagnan / hnt

🦁 Small library to safely access (hunt for 😉) deeply nested array values
MIT License
4 stars 1 forks source link

Default null check #4

Closed sujaltv closed 4 years ago

sujaltv commented 4 years ago

Consider this:

import hnt from 'hnt'

function getResult(): any[] | null {
  ...
}

const result = getResult()
console.log(htn(result, 'data[0]', {})

With the signature as it stands, the console would fail. Rather than having to explicitly check if array's null before passing it on to htn, it would be desirable to do so implicitly:

export function hnt(array: any[] | null, path: string, fallback: any) {
  if (array === null) return fallback
  ...
divyagnan commented 4 years ago

Yeah I've been meaning to fix this for a really long time - thanks for finally pushing me to do it!

I released the new version with the fix (#5) as npm versions 1.0.5 (I messed up when pushing versions 1.0.3 and 1.0.4 😅) and 2.0.0 (since I messed up the 1s I figured a clean start would be good).

https://www.npmjs.com/package/hnt