divyagnan/hnt

Default null check

sujaltv opened this issue · 1 comments

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

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