Return undefined/default when requesting key nested under null value.#13
Return undefined/default when requesting key nested under null value.#13developit merged 5 commits intodevelopit:masterfrom kalmbach:2-default-for-nested-null-value
Conversation
|
I have solved the error reported by travis locally installing the |
|
Just a note - I'm pondering this PR and want to merge it, but it won't go into today's release. |
| while (obj && p<key.length) obj = obj[key[p++]]; | ||
| return obj===undefined ? def : obj; | ||
| } No newline at end of file | ||
| return (obj===undefined || p<key.length) ? def : obj; |
There was a problem hiding this comment.
Could we not just have obj == undefined here and drop the strict equality check? :)
There was a problem hiding this comment.
I am not following, if we drop the strict equality check it will fail to return a defined key with a null value, and will return the default instead. For example:
var obj = { a: null }
delve(obj, "a", "foo")
=> "foo"
It should have returned null, it's a defined key. It should only return undefined (or the default) when the key is not defined in the object.
The strict equality check is necessary to distinguish undefined keys from the defined keys with null values.
I hope this makes sense.
|
it would be nice to merge this in :) |
|
@developit Any chance to merge and release this? |
|
Yup. Need to check size. |
|
131 bytes means this is only 7 bytes added weight. I'm in. |
Return undefined/default value when requesting for a key nested under a null value.
My logic for this change is:
If we reach an
undefinedobject or the end of the key path (p >= key.length), we return the object in that position, if that object isundefinedwe return thedefaultvalue, otherwise we rerturn the object. (includingnullvalues)When we don't reach the end of the key path (
p < key.length), it's because a value in between isfalsey, in either case that mean the object at the end of the key path is unreachable (undefined) and we should return the default value.[Closes #9]