Most Lodash functions accept null & undefined
See original GitHub issueLodash libdefs does not always accept null or undefined when Lodash most of the time is totally fine with them. I made a small script to skim trough Lodash and check out which ones does not accept null or undefined and there were only handful of them. here is the script i used on lodash documentation page:
_.map(_, (value, key) => {
if (typeof value !== 'function') {
return;
}
try {
value(..._.times(value.length, () => null));
} catch (e) {
return key;
}
}).filter(v => v);
and here are the actual functions which failed when passing nulls:
[
"after",
"ary",
"before",
"bind",
"curry",
"curryRight",
"debounce",
"defer",
"delay",
"flip",
"memoize",
"negate",
"once",
"partial",
"partialRight",
"rearg",
"rest",
"spread",
"tap",
"throttle",
"thru",
"unary"
]
Are Lodash libdefs strict on nulls by design, or is it just inaccuracy? All the other functions returned something more or less meaningful when passing in nulls: null, undefined, empty array or object. Some returned Window or -1 etc. Return values vary a bit depending if passing in null or undefined
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
How to use Lodash when data is undefined or null
Lodash provides quite a few methods to check and get the value you want from an object. _.get would actually return the value...
Read more >Lodash Documentation
Creates an array with all falsey values removed. The values false , null , 0 , "" , undefined , and NaN are...
Read more >Fun JavaScript: Safely dealing with undefined/null values ...
1- map: It takes a function, and if the value exists, applies that function on the value and returns a new value inside...
Read more >You Might Not Need Lodash
Creates an array with all falsey values removed. The values false , null , 0 , "" , undefined , and NaN are...
Read more >How to get the first non-null/undefined argument in JavaScript
Approach 1: We can implement coalescing in pre-ES6 JavaScript by looping through the arguments and checking which of the arguments is equal to ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yep 👍
@AndrewSouthpaw I think good rule of thumb could be to try distinguish wether null or undefined is somewhat special value or not for the function. If undefined and null are similar values than any other value, no need to have special handling for them. For example with function
eq
, I don’t see that undefined or null differs from any other values or types like number or string: For that function all of them are just values. In this case I took it into account and thought returning boolean is just enough. seetest_does-not-fail-on-null-v4.x.x.js:398
. But then again if we look atinvert
, it’s not clear that the function returns (an empty) object in case of null or undefined and that’s imo somewhat special behaviour around undefined or null cases which can be taken into account on the libdef level.Then there is ofc maintainability aspect. In case of Lodash, I think it’s such a stabilised library that things are not likely to change that often. This makes it acceptable to have more specific definitions than in some younger libs where some corner cases might not be taken into account.