55Rules for Array functions and methods.
66
77## Contents
8+
89- [ Installation] ( #installation )
910- [ Rules] ( #rules )
1011 - [ ` from-map ` ] ( #from-map )
1112 - [ Examples] ( #examples )
13+ - [ Using the rule] ( #using-the-rule )
1214 - [ ` no-unnecessary-this-arg ` ] ( #no-unnecessary-this-arg )
1315 - [ Checked Functions] ( #checked-functions )
1416 - [ Checked Methods] ( #checked-methods )
1517 - [ Examples] ( #examples-1 )
18+ - [ Using the rule] ( #using-the-rule-1 )
1619 - [ ` prefer-array-from ` ] ( #prefer-array-from )
1720 - [ Examples] ( #examples-2 )
21+ - [ Using the rule] ( #using-the-rule-2 )
1822 - [ ` avoid-reverse ` ] ( #avoid-reverse )
1923 - [ Examples] ( #examples-3 )
24+ - [ Using the rule] ( #using-the-rule-3 )
2025 - [ ` prefer-flat-map ` ] ( #prefer-flat-map )
2126 - [ Examples] ( #examples-4 )
27+ - [ Using the rule] ( #using-the-rule-4 )
2228 - [ ` prefer-flat ` ] ( #prefer-flat )
2329 - [ Examples] ( #examples-5 )
30+ - [ Using the rule] ( #using-the-rule-5 )
2431- [ Configurations] ( #configurations )
2532 - [ ` array-func/recommended ` Configuration] ( #array-funcrecommended-configuration )
2633 - [ Using the Configuration] ( #using-the-configuration )
2734 - [ ` array-func/all ` Configuration] ( #array-funcall-configuration )
35+ - [ Using the Configuration] ( #using-the-configuration-1 )
2836- [ License] ( #license )
2937
3038## Installation
@@ -44,21 +52,25 @@ $ npm install -D eslint-plugin-array-func
4452## Rules
4553
4654### ` from-map `
55+
4756Prefer using the ` mapFn ` callback of ` Array.from ` over an immediate ` .map() ` call on the ` Array.from ` result.
4857
4958` 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] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Description ) for an explanation of the potential benefits of using the mapping callback of ` Array.from ` directly.
5059
5160This 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.
5261
5362#### Examples
63+
5464Code that triggers this rule:
65+
5566``` js
5667Array .from (iterable).map ((t ) => t .id );
5768
5869Array .from (iterable, (t ) => t .id ).map ((id ) => id[0 ]);
5970```
6071
6172Code that doesn't trigger this rule:
73+
6274``` js
6375Array .from (iterable, (t ) => t .id );
6476
@@ -68,17 +80,37 @@ const arr = Array.from(iterable);
6880const mappedArray = arr .map ((t ) => t .id );
6981```
7082
83+ #### Using the rule
84+
85+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
86+
87+ ``` json
88+ {
89+ "plugin" : [
90+ " array-func"
91+ ],
92+ "rules" : {
93+ "array-func/array-from" : " error"
94+ }
95+ }
96+ ```
97+
98+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
99+
71100### ` no-unnecessary-this-arg `
101+
72102Avoid the ` this ` parameter when providing arrow function as callback in array functions.
73103
74104The ` this ` parameter is useless when providing arrow functions, since the ` this ` of arrow functions can not be rebound, thus the parameter has no effect.
75105
76106The 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.
77107
78108#### Checked Functions
109+
79110- ` from ` (fixable)
80111
81112#### Checked Methods
113+
82114- ` every `
83115- ` filter `
84116- ` find `
@@ -88,7 +120,9 @@ The fix is usually to omit the parameter. The Array methods can't be auto-fixed,
88120- ` some `
89121
90122#### Examples
123+
91124Code that triggers this rule:
125+
92126``` js
93127const array = Array .from (" example" , (char ) => char .charCodeAt (0 ), this );
94128
@@ -108,6 +142,7 @@ array.forEach((char) => console.log(char), this);
108142```
109143
110144Code that doesn't trigger this rule:
145+
111146``` js
112147const array = Array .from (" example" , (char ) => char .charCodeAt (0 ));
113148const alternateArray = Array .from (" example" , function (char ) {
@@ -135,21 +170,42 @@ array.forEach(function(char) {
135170array .filter (this .isGood , this );
136171```
137172
173+ #### Using the rule
174+
175+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
176+
177+ ``` json
178+ {
179+ "plugin" : [
180+ " array-func"
181+ ],
182+ "rules" : {
183+ "array-func/no-unnecessary-this-arg" : " error"
184+ }
185+ }
186+ ```
187+
188+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
189+
138190### ` prefer-array-from `
191+
139192Use ` Array.from ` instead of ` [...iterable] ` .
140193See [ ` from-map ` ] ( #from-map ) for additional benefits ` Array.from ` can provide over the spread syntax.
141194
142195This rule is auto fixable.
143196
144197#### Examples
198+
145199Code that triggers this rule:
200+
146201``` js
147202const iterable = [... " string" ];
148203
149204const arrayCopy = [... iterable];
150205```
151206
152207Code that doesn't trigger this rule:
208+
153209``` js
154210const array = [1 , 2 , 3 ];
155211
@@ -160,7 +216,25 @@ const arrayCopy = Array.from(array);
160216const characterArray = Array .from (" string" );
161217```
162218
219+ #### Using the rule
220+
221+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
222+
223+ ``` json
224+ {
225+ "plugin" : [
226+ " array-func"
227+ ],
228+ "rules" : {
229+ "array-func/prefer-array-from" : " error"
230+ }
231+ }
232+ ```
233+
234+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
235+
163236### ` avoid-reverse `
237+
164238Avoid reversing the array and running a method on it if there is an equivalent
165239of the method operating on the array from the other end.
166240
@@ -169,14 +243,17 @@ There are two operations with such equivalents: `reduce` with `reduceRight`.
169243This rule is auto fixable.
170244
171245#### Examples
246+
172247Code that triggers this rule:
248+
173249``` js
174250const string = array .reverse ().reduce ((p , c ) => p + c, ' ' );
175251
176252const reverseString = array .reverse ().reduceRight ((p , c ) => p + c, ' ' );
177253```
178254
179255Code that doesn't trigger this rule:
256+
180257``` js
181258const reverseString = array .reduce ((p , c ) => p + c, ' ' );
182259
@@ -187,21 +264,42 @@ const reverseArray = array.reverse();
187264const reverseMap = array .reverse ().map ((r ) => r + 1 );
188265```
189266
267+ #### Using the rule
268+
269+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
270+
271+ ``` json
272+ {
273+ "plugin" : [
274+ " array-func"
275+ ],
276+ "rules" : {
277+ "array-func/avoid-reverse" : " error"
278+ }
279+ }
280+ ```
281+
282+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
283+
190284### ` prefer-flat-map `
285+
191286Use ` .flatMap() ` to flatten an array and map the values instead of using
192287` .flat().map() ` .
193288
194289This rule is auto fixable.
195290
196291#### Examples
292+
197293Code that triggers this rule:
294+
198295``` js
199296const flattenedAndMapped = array .map ((p ) => p).flat ();
200297
201298const flatWithDefaultDepth = array .map ((r ) => r).flat (1 );
202299```
203300
204301Code that doesn't trigger this rule:
302+
205303``` js
206304const oneAction = array .flatMap ((m ) => m);
207305
@@ -216,7 +314,25 @@ const flatMappedWithExtra = array.map((r) => r + 1).reverse().flat();
216314const flatWithDepth = array .map ((p ) => p).flat (99 );
217315```
218316
317+ #### Using the rule
318+
319+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
320+
321+ ``` json
322+ {
323+ "plugin" : [
324+ " array-func"
325+ ],
326+ "rules" : {
327+ "array-func/prefer-flat-map" : " error"
328+ }
329+ }
330+ ```
331+
332+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
333+
219334### ` prefer-flat `
335+
220336Use ` .flat() ` to flatten an array of arrays. This rule currently recognizes two
221337patterns and can replace them with a ` .flat() ` call:
222338
@@ -226,14 +342,17 @@ patterns and can replace them with a `.flat()` call:
226342This rule is auto fixable.
227343
228344#### Examples
345+
229346Code that triggers this rule:
347+
230348``` js
231349const concatFlat = [].concat (... array);
232350
233351const reduceFlat = array .reduce ((p , n ) => p .concat (n), []);
234352```
235353
236354Code that doesn't trigger this rule:
355+
237356``` js
238357const flattened = array .flat ();
239358
@@ -242,9 +361,27 @@ const reverseFlat = array.reduce((p, n) => n.concat(p), []);
242361const otherReduce = array .reduce ((p , n ) => n + p, 0 );
243362```
244363
364+ #### Using the rule
365+
366+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
367+
368+ ``` json
369+ {
370+ "plugin" : [
371+ " array-func"
372+ ],
373+ "rules" : {
374+ "array-func/prefer-flat" : " error"
375+ }
376+ }
377+ ```
378+
379+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
380+
245381## Configurations
246382
247383### ` array-func/recommended ` Configuration
384+
248385The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added.
249386
250387Rule | Error level | Fixable
@@ -255,7 +392,9 @@ Rule | Error level | Fixable
255392` array-func/avoid-reverse ` | Error | Yes
256393
257394#### Using the Configuration
395+
258396To enable this configuration use the ` extends ` property in your ` .eslintrc.json ` config file (may look different for other config file styles):
397+
259398``` json
260399{
261400 "extends" : [
@@ -265,6 +404,7 @@ To enable this configuration use the `extends` property in your `.eslintrc.json`
265404```
266405
267406### ` array-func/all ` Configuration
407+
268408The recommended configuration does not include all rules, since some Array methods
269409were added after ES2015. The all configuration enables all rules the plugin
270410contains and sets the ECMA version appropriately.
@@ -278,5 +418,18 @@ Rule | Error level | Fixable
278418` array-func/prefer-flat-map ` | Error | Yes
279419` array-func/prefer-flat ` | Error | Yes
280420
421+ #### Using the Configuration
422+
423+ To enable this configuration use the ` extends ` property in your ` .eslintrc.json ` config file (may look different for other config file styles):
424+
425+ ``` json
426+ {
427+ "extends" : [
428+ " plugin:array-func/all"
429+ ]
430+ }
431+ ```
432+
281433## License
434+
282435The ` array-func ` plugin is licensed under the [ MIT License] ( LICENSE ) .
0 commit comments