question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Most Lodash functions accept null & undefined

See original GitHub issue

Lodash 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:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
villesaucommented, Jan 3, 2018

yep 👍

0reactions
villesaucommented, Jan 5, 2018

@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. see test_does-not-fail-on-null-v4.x.x.js:398. But then again if we look at invert, 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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found