Skip to content

Return undefined/default when requesting key nested under null value.#13

Merged
developit merged 5 commits intodevelopit:masterfrom
kalmbach:2-default-for-nested-null-value
Jun 6, 2018
Merged

Return undefined/default when requesting key nested under null value.#13
developit merged 5 commits intodevelopit:masterfrom
kalmbach:2-default-for-nested-null-value

Conversation

@kalmbach
Copy link
Contributor

@kalmbach kalmbach commented Dec 16, 2017

Return undefined/default value when requesting for a key nested under a null value.

My logic for this change is:
If we reach an undefined object or the end of the key path (p >= key.length), we return the object in that position, if that object is undefined we return the default value, otherwise we rerturn the object. (including null values)

When we don't reach the end of the key path (p < key.length), it's because a value in between is falsey, 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]

@kalmbach
Copy link
Contributor Author

I have solved the error reported by travis locally installing the uglify-js package (note the diff in the name => uglifyjs)

@developit developit self-requested a review January 14, 2018 00:14
@developit
Copy link
Owner

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we not just have obj == undefined here and drop the strict equality check? :)

Copy link
Contributor Author

@kalmbach kalmbach Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right

@bboydflo
Copy link

it would be nice to merge this in :)

@jawadst
Copy link

jawadst commented May 8, 2018

@developit Any chance to merge and release this?

@developit
Copy link
Owner

Yup. Need to check size.

@developit
Copy link
Owner

131 bytes means this is only 7 bytes added weight. I'm in.

@developit developit merged commit f6c4465 into developit:master Jun 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants