eslint-plugin-lodash icon indicating copy to clipboard operation
eslint-plugin-lodash copied to clipboard

False positive for prefer-lodash-chain with _.range?

Open danvk opened this issue 5 years ago • 1 comments

I have this function to produce nPoints of data on a curve:

const generateData = (nPoints: number) =>
  _.range(0, nPoints).map(i => {
    // Inverse tanh on (-0.9, +0.9); just something to get a distribution.
    const x = (1.9 * i) / nPoints - 0.95;
    const atanh = Math.log1p(x) - Math.log1p(-x);
    return {row: i, atanh};
  });

The prefer-lodash-chain rule flags this because of the _.range followed by the map. It's not clear what I'm supposed to replace this with, though. It would have to be something like _(_.range(0, nPoints)).map(...).value(), which doesn't seem like an improvement.

danvk avatar Jul 27 '20 18:07 danvk

@danvk You can you _.times instead. https://lodash.com/docs/4.17.15#times

_.range(0, nPoints).map(i => {

_.times(nPoints, i => {

alldayalone avatar Nov 17 '21 17:11 alldayalone