sindresorhus/eslint-plugin-unicorn

Add auto-fix for `no-array-reduce`

Chiusa

#1291 aperta il 18 mag 2021

 (2 commenti) (2 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
enhancementhelp wanted

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

 const foo = array.reduce(callback, initValue);

->

let foo = initValue;
for (const [index, element] of array.entries()) {
  foo  = callback(foo, element, index, array);

  // Or safer
  // foo  = callback.call(array, foo, element, index, array); 
}

 const foo = array.reduce(callback); // No inital value

->

let foo;
for (const [index, element] of array.entries()) {
  if (index === 0) {
   foo = element;
   continue;
  }
  foo  = callback(foo, element, index, array);
}

 const foo = array.reduce((accumulator, element, index, array) => {
	return doThingsWith(accumulator, element, index, array)
}, initValue);

->

let foo = initValue;
for (const [index, element] of array.entries()) {
  foo  = doThingsWith(foo, element, index, array);
}

Guida contributor