Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-no-array-reduce

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-no-array-reduce

ESLint rule to disallow Array.reduce() method.

  • 1.0.55
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.2K
increased by25.72%
Maintainers
1
Weekly downloads
 
Created
Source

eslint-plugin-no-array-reduce

npm version npm downloads CI semantic-release prettier TypeScript

ESLint rule to disallow Array.reduce() method.

Method reduce() in most cases can be written as map(), filter() or one of the for() loops which benefits in code readability and makes it easier to maintain for future developers.

Subjectively there are still cases where you might want to use reduce() with eslint-disable.
There are many debates, discussions and other resources related to it:

Install

npm install --save-dev eslint-plugin-no-array-reduce

Then extend eslint config:

{
  "extends": [
    // ...
    "plugin:no-array-reduce/recommended"
  ]
}

Fail

const products = [
  { name: 'milk', type: 'dairy' },
  { name: 'cheese', type: 'dairy' },
  { name: 'beef', type: 'meat' },
  { name: 'chicken', type: 'meat' },
];

// Add price to each product
const productsWithPrices = products.reduce((acc, product) => acc.concat({ ...product, price: 2.7 }), []);
// Filter dairy products
const dairies = products.reduce((acc, product) => (product.type === 'dairy' ? acc.concat(product) : acc), []);
// Group products by type
const productsByType = products.reduce(
  (acc, product) => ({
    ...acc,
    [product.type]: [...(acc[product.type] || []), product],
  }),
  [],
);

Pass

// Add price to each product
const productsWithPrices = products.map((product) => ({ ...product, price: 2.7 }));
// Filter dairy products
const dairies = products.filter((product) => product.type === 'dairy');
// Group products by type (ECMA stage 3 - https://github.com/tc39/proposal-array-grouping)
const productsByType = products.groupBy((product) => product.type);

CodeSandbox

Contributing

All contributions are welcome!

Keywords

FAQs

Package last updated on 23 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc