- Notifications
You must be signed in to change notification settings - Fork7
Rules for Array functions and methods.
License
freaktechnik/eslint-plugin-array-func
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Rules for Array functions and methods.
InstallESLint either locally or globally.
$ npm install -D eslint
If you installedESLint
globally, you have to install thearray-func
plugin globally too. Otherwise, install it locally.
$ npm install -D eslint-plugin-array-func
Prefer using themapFn
callback ofArray.from
over an immediate.map()
call on theArray.from
result.
Array.from
has amapFn
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 ofArray.from
avoids an iteration. See alsoMDN for an explanation of the potential benefits of using the mapping callback ofArray.from
directly.
This rule is auto fixable. It will produce nested function calls if you use theArray.from
map callback and have a.map()
call following it.
Code that triggers this rule:
Array.from(iterable).map((t)=>t.id);Array.from(iterable,(t)=>t.id).map((id)=>id[0]);
Code that doesn't trigger this rule:
Array.from(iterable,(t)=>t.id);Array.from(iterable,function(t){this.format(t);},this);constarr=Array.from(iterable);constmappedArray=arr.map((t)=>t.id);
To use this rule, youreslint.config.js
should at least contain the following:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/from-map":"error"}}];
Alternatively you can use aconfiguration included with this plugin.
Avoid thethis
parameter when providing arrow function as callback in array functions.
Thethis
parameter is useless when providing arrow functions, since thethis
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:
constarray=Array.from("example",(char)=>char.charCodeAt(0),this);conste=array.find((char)=>char===101,this);constexampleAsArray=array.map((char)=>String.fromCharCode(char),this);consteIndex=array.findIndex((char)=>char===101,this);constcontainsE=array.some((char)=>char===101,this);constisOnlyE=array.every((char)=>char===101,this);constonlyEs=array.filter((char)=>char===101,this);array.forEach((char)=>console.log(char),this);
Code that doesn't trigger this rule:
constarray=Array.from("example",(char)=>char.charCodeAt(0));constalternateArray=Array.from("example",function(char){returnchar.charCodeAt(this)},0);conste=array.find((char)=>char===101);constexampleAsArray=array.map((char)=>String.fromCharCode(char));consteIndex=array.findIndex((char)=>char===101);constcontainsE=array.some((char)=>char===101);constisOnlyE=array.every((char)=>char===101);constonlyEs=array.filter(function(char){returnchar===this},101);array.forEach(function(char){this.log(char);},console);array.filter(this.isGood,this);
To use this rule, youreslint.config.js
should at least contain the following:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/no-unnecessary-this-arg":"error"}}];
Alternatively you can use aconfiguration included with this plugin.
UseArray.from
instead of[...iterable]
.Seefrom-map
for additional benefitsArray.from
can provide over the spread syntax.
This rule is auto fixable.
Code that triggers this rule:
constiterable=[..."string"];constarrayCopy=[...iterable];
Code that doesn't trigger this rule:
constarray=[1,2,3];constextendedArray=[0, ...array];constarrayCopy=Array.from(array);constcharacterArray=Array.from("string");
To use this rule, youreslint.config.js
should at least contain the following:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/prefer-array-from":"error"}}];
Alternatively you can use aconfiguration included with this plugin.
Avoid reversing the array and running a method on it if there is an equivalentof the method operating on the array from the other end.
There are two operations with such equivalents:reduce
withreduceRight
.
This rule is auto fixable.
Code that triggers this rule:
conststring=array.reverse().reduce((p,c)=>p+c,'');constreverseString=array.reverse().reduceRight((p,c)=>p+c,'');
Code that doesn't trigger this rule:
constreverseString=array.reduce((p,c)=>p+c,'');conststring=array.reduceRight((p,c)=>p+c,'');constreverseArray=array.reverse();constreverseMap=array.reverse().map((r)=>r+1);
To use this rule, youreslint.config.js
should at least contain the following:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/avoid-reverse":"error"}}];
Alternatively you can use aconfiguration included with this plugin.
Use.flatMap()
to map and then flatten an array instead of using.map().flat()
.
This rule is auto fixable.
Code that triggers this rule:
constmappedAndFlattened=array.map((p)=>p).flat();constflatWithDefaultDepth=array.map((r)=>r).flat(1);
Code that doesn't trigger this rule:
constoneAction=array.flatMap((m)=>m);constflattened=array.flat();constmapped=array.map((r)=>r+1);constflattenedThenMapped=array.flat().map((r)=>r+1);constflatMappedWithExtra=array.map((r)=>r+1).reverse().flat();constflatWithDepth=array.map((p)=>p).flat(99);
To use this rule, youreslint.config.js
should at least contain the following:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/prefer-flat-map":"error"}}];
Alternatively you can use aconfiguration included with this plugin.
Use.flat()
to flatten an array of arrays. This rule currently recognizes twopatterns 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:
constconcatFlat=[].concat(...array);constreduceFlat=array.reduce((p,n)=>p.concat(n),[]);
Code that doesn't trigger this rule:
constflattened=array.flat();constreverseFlat=array.reduce((p,n)=>n.concat(p),[]);constotherReduce=array.reduce((p,n)=>n+p,0);
To use this rule, youreslint.config.js
should at least contain the following:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/prefer-flat":"error"}}];
Alternatively you can use aconfiguration included with this plugin.
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 |
To enable this configuration, import the plugin and add the config to your eslint config array:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[arrayFunc.configs.recommended,];
The recommended configuration does not include all rules, since some Array methodswere added after ES2015. The all configuration enables all rules the plugincontainsy.
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:
importarrayFuncfrom"eslint-plugin-array-func";exportdefault[arrayFunc.configs.all,];
Thearray-func
plugin is licensed under theMIT License.
About
Rules for Array functions and methods.
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.