Gathering detailed insights and metrics for eslint-plugin-array-func
Gathering detailed insights and metrics for eslint-plugin-array-func
Gathering detailed insights and metrics for eslint-plugin-array-func
Gathering detailed insights and metrics for eslint-plugin-array-func
Rules for Array functions and methods.
npm install eslint-plugin-array-func
85.4
Supply Chain
93.4
Quality
77.1
Maintenance
100
Vulnerability
98.9
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
94 Stars
566 Commits
7 Forks
4 Watching
1 Branches
10 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-15.4%
15,520
Compared to previous day
Last week
-10%
79,336
Compared to previous week
Last month
18.5%
336,350
Compared to previous month
Last year
25.2%
3,470,837
Compared to previous year
Rules for Array functions and methods.
Install ESLint either locally or globally.
1$ npm install -D eslint
If you installed ESLint
globally, you have to install the array-func
plugin globally too. Otherwise, install it locally.
1$ npm install -D eslint-plugin-array-func
from-map
Prefer using the mapFn
callback of Array.from
over an immediate .map()
call on the Array.from
result.
Array.from
has a mapFn
callback that lets you map the items of the iterable to an array like you would with .map()
except that values have not yet been truncated to fit types allowed in an array. Some iterables can't be directly converted to an array and thus have to be iterated either way. In that case using the mapping callback of Array.from
avoids an iteration. See also MDN for an explanation of the potential benefits of using the mapping callback of Array.from
directly.
This rule is auto fixable. It will produce nested function calls if you use the Array.from
map callback and have a .map()
call following it.
Code that triggers this rule:
1Array.from(iterable).map((t) => t.id); 2 3Array.from(iterable, (t) => t.id).map((id) => id[0]);
Code that doesn't trigger this rule:
1Array.from(iterable, (t) => t.id); 2 3Array.from(iterable, function(t) { this.format(t); }, this); 4 5const arr = Array.from(iterable); 6const mappedArray = arr.map((t) => t.id);
To use this rule, your eslint.config.js
should at least contain the following:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 { 5 plugins: { 6 "array-func": arrayFunc 7 }, 8 rules: { 9 "array-func/from-map": "error" 10 } 11 } 12];
Alternatively you can use a configuration included with this plugin.
no-unnecessary-this-arg
Avoid the this
parameter when providing arrow function as callback in array functions.
The this
parameter is useless when providing arrow functions, since the this
of arrow functions can not be rebound, thus the parameter has no effect.
The fix is usually to omit the parameter. The Array methods can't be auto-fixed, since the detection of array methods is not confident enough to know that the method is being called on an array.
from
(fixable)every
filter
find
findIndex
forEach
map
some
Code that triggers this rule:
1const array = Array.from("example", (char) => char.charCodeAt(0), this); 2 3const e = array.find((char) => char === 101, this); 4 5const exampleAsArray = array.map((char) => String.fromCharCode(char), this); 6 7const eIndex = array.findIndex((char) => char === 101, this); 8 9const containsE = array.some((char) => char === 101, this); 10 11const isOnlyE = array.every((char) => char === 101, this); 12 13const onlyEs = array.filter((char) => char === 101, this); 14 15array.forEach((char) => console.log(char), this);
Code that doesn't trigger this rule:
1const array = Array.from("example", (char) => char.charCodeAt(0)); 2const alternateArray = Array.from("example", function(char) { 3 return char.charCodeAt(this) 4}, 0); 5 6const e = array.find((char) => char === 101); 7 8const exampleAsArray = array.map((char) => String.fromCharCode(char)); 9 10const eIndex = array.findIndex((char) => char === 101); 11 12const containsE = array.some((char) => char === 101); 13 14const isOnlyE = array.every((char) => char === 101); 15 16const onlyEs = array.filter(function(char) { 17 return char === this 18}, 101); 19 20array.forEach(function(char) { 21 this.log(char); 22}, console); 23 24array.filter(this.isGood, this);
To use this rule, your eslint.config.js
should at least contain the following:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 { 5 plugins: { 6 "array-func": arrayFunc 7 }, 8 rules: { 9 "array-func/no-unnecessary-this-arg": "error" 10 } 11 } 12];
Alternatively you can use a configuration included with this plugin.
prefer-array-from
Use Array.from
instead of [...iterable]
.
See from-map
for additional benefits Array.from
can provide over the spread syntax.
This rule is auto fixable.
Code that triggers this rule:
1const iterable = [..."string"]; 2 3const arrayCopy = [...iterable];
Code that doesn't trigger this rule:
1const array = [1, 2, 3]; 2 3const extendedArray = [0, ...array]; 4 5const arrayCopy = Array.from(array); 6 7const characterArray = Array.from("string");
To use this rule, your eslint.config.js
should at least contain the following:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 { 5 plugins: { 6 "array-func": arrayFunc 7 }, 8 rules: { 9 "array-func/prefer-array-from": "error" 10 } 11 } 12];
Alternatively you can use a configuration included with this plugin.
avoid-reverse
Avoid reversing the array and running a method on it if there is an equivalent of the method operating on the array from the other end.
There are two operations with such equivalents: reduce
with reduceRight
.
This rule is auto fixable.
Code that triggers this rule:
1const string = array.reverse().reduce((p, c) => p + c, ''); 2 3const reverseString = array.reverse().reduceRight((p, c) => p + c, '');
Code that doesn't trigger this rule:
1const reverseString = array.reduce((p, c) => p + c, ''); 2 3const string = array.reduceRight((p, c) => p + c, ''); 4 5const reverseArray = array.reverse(); 6 7const reverseMap = array.reverse().map((r) => r + 1);
To use this rule, your eslint.config.js
should at least contain the following:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 { 5 plugins: { 6 "array-func": arrayFunc 7 }, 8 rules: { 9 "array-func/avoid-reverse": "error" 10 } 11 } 12];
Alternatively you can use a configuration included with this plugin.
prefer-flat-map
Use .flatMap()
to map and then flatten an array instead of using .map().flat()
.
This rule is auto fixable.
Code that triggers this rule:
1const mappedAndFlattened = array.map((p) => p).flat(); 2 3const flatWithDefaultDepth = array.map((r) => r).flat(1);
Code that doesn't trigger this rule:
1const oneAction = array.flatMap((m) => m); 2 3const flattened = array.flat(); 4 5const mapped = array.map((r) => r + 1); 6 7const flattenedThenMapped = array.flat().map((r) => r + 1); 8 9const flatMappedWithExtra = array.map((r) => r + 1).reverse().flat(); 10 11const flatWithDepth = array.map((p) => p).flat(99);
To use this rule, your eslint.config.js
should at least contain the following:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 { 5 plugins: { 6 "array-func": arrayFunc 7 }, 8 rules: { 9 "array-func/prefer-flat-map": "error" 10 } 11 } 12];
Alternatively you can use a configuration included with this plugin.
prefer-flat
Use .flat()
to flatten an array of arrays. This rule currently recognizes two
patterns and can replace them with a .flat()
call:
[].concat(...array)
array.reduce((p, n) => p.concat(n), [])
This rule is auto fixable.
Code that triggers this rule:
1const concatFlat = [].concat(...array); 2 3const reduceFlat = array.reduce((p, n) => p.concat(n), []);
Code that doesn't trigger this rule:
1const flattened = array.flat(); 2 3const reverseFlat = array.reduce((p, n) => n.concat(p), []); 4 5const otherReduce = array.reduce((p, n) => n + p, 0);
To use this rule, your eslint.config.js
should at least contain the following:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 { 5 plugins: { 6 "array-func": arrayFunc 7 }, 8 rules: { 9 "array-func/prefer-flat": "error" 10 } 11 } 12];
Alternatively you can use a configuration included with this plugin.
recommended
ConfigurationRule | Error level | Fixable |
---|---|---|
array-func/from-map | Error | Yes |
array-func/no-unnecessary-this-arg | Error | Sometimes |
array-func/prefer-array-from | Error | Yes |
array-func/avoid-reverse | Error | Yes |
To enable this configuration, import the plugin and add the config to your eslint config array:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 arrayFunc.configs.recommended, 5];
all
ConfigurationThe recommended configuration does not include all rules, since some Array methods were added after ES2015. The all configuration enables all rules the plugin containsy.
Rule | Error level | Fixable |
---|---|---|
array-func/from-map | Error | Yes |
array-func/no-unnecessary-this-arg | Error | Sometimes |
array-func/prefer-array-from | Error | Yes |
array-func/avoid-reverse | Error | Yes |
array-func/prefer-flat-map | Error | Yes |
array-func/prefer-flat | Error | Yes |
To enable this configuration, import the plugin and add the config to your eslint config array:
1import arrayFunc from "eslint-plugin-array-func"; 2 3export default [ 4 arrayFunc.configs.all, 5];
The array-func
plugin is licensed under the MIT License.
No vulnerabilities found.
Reason
18 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
Found 1/2 approved changesets -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More