{"id":13801554,"url":"https://github.com/freaktechnik/eslint-plugin-array-func","last_synced_at":"2025-05-15T13:05:37.587Z","repository":{"id":25702435,"uuid":"106007920","full_name":"freaktechnik/eslint-plugin-array-func","owner":"freaktechnik","description":"Rules for Array functions and methods.","archived":false,"fork":false,"pushed_at":"2025-05-13T11:00:58.000Z","size":3320,"stargazers_count":96,"open_issues_count":12,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-13T12:20:08.864Z","etag":null,"topics":["array-methods","eslint-plugin","eslintplugin"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/eslint-plugin-array-func","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/freaktechnik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-10-06T13:30:06.000Z","updated_at":"2025-05-13T11:01:00.000Z","dependencies_parsed_at":"2023-02-12T22:31:22.253Z","dependency_job_id":"2af27def-29d8-4511-8087-dfb5dd6383f1","html_url":"https://github.com/freaktechnik/eslint-plugin-array-func","commit_stats":{"total_commits":547,"total_committers":12,"mean_commits":"45.583333333333336","dds":0.2742230347349177,"last_synced_commit":"163b9479afd0c0975046e4a5c17b3dcb7f0edea8"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freaktechnik%2Feslint-plugin-array-func","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freaktechnik%2Feslint-plugin-array-func/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freaktechnik%2Feslint-plugin-array-func/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freaktechnik%2Feslint-plugin-array-func/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/freaktechnik","download_url":"https://codeload.github.com/freaktechnik/eslint-plugin-array-func/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253939375,"owners_count":21987441,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array-methods","eslint-plugin","eslintplugin"],"created_at":"2024-08-04T00:01:24.269Z","updated_at":"2025-05-15T13:05:37.562Z","avatar_url":"https://github.com/freaktechnik.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Plugins"],"sub_categories":["Practices and Specific ES Features"],"readme":"# eslint-plugin-array-func\n\n[![codecov](https://codecov.io/gh/freaktechnik/eslint-plugin-array-func/graph/badge.svg?token=FhaBIu6Ze0)](https://codecov.io/gh/freaktechnik/eslint-plugin-array-func)\n\nRules for Array functions and methods.\n\n## Contents\n\n- [Installation](#installation)\n- [Rules](#rules)\n  - [`from-map`](#from-map)\n    - [Examples](#examples)\n    - [Using the rule](#using-the-rule)\n  - [`no-unnecessary-this-arg`](#no-unnecessary-this-arg)\n    - [Checked Functions](#checked-functions)\n    - [Checked Methods](#checked-methods)\n    - [Examples](#examples-1)\n    - [Using the rule](#using-the-rule-1)\n  - [`prefer-array-from`](#prefer-array-from)\n    - [Examples](#examples-2)\n    - [Using the rule](#using-the-rule-2)\n  - [`avoid-reverse`](#avoid-reverse)\n    - [Examples](#examples-3)\n    - [Using the rule](#using-the-rule-3)\n  - [`prefer-flat-map`](#prefer-flat-map)\n    - [Examples](#examples-4)\n    - [Using the rule](#using-the-rule-4)\n  - [`prefer-flat`](#prefer-flat)\n    - [Examples](#examples-5)\n    - [Using the rule](#using-the-rule-5)\n- [Configurations](#configurations)\n  - [`array-func/recommended` Configuration](#array-funcrecommended-configuration)\n    - [Using the Configuration](#using-the-configuration)\n  - [`array-func/all` Configuration](#array-funcall-configuration)\n    - [Using the Configuration](#using-the-configuration-1)\n- [License](#license)\n\n## Installation\n\nInstall [ESLint](https://www.github.com/eslint/eslint) either locally or globally.\n\n```sh\n$ npm install -D eslint\n```\n\nIf you installed `ESLint` globally, you have to install the `array-func` plugin globally too. Otherwise, install it locally.\n\n```sh\n$ npm install -D eslint-plugin-array-func\n```\n\n## Rules\n\n### `from-map`\n\nPrefer using the `mapFn` callback of `Array.from` over an immediate `.map()` call on the `Array.from` result.\n\n`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.\n\nThis 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.\n\n#### Examples\n\nCode that triggers this rule:\n\n```js\nArray.from(iterable).map((t) =\u003e t.id);\n\nArray.from(iterable, (t) =\u003e t.id).map((id) =\u003e id[0]);\n```\n\nCode that doesn't trigger this rule:\n\n```js\nArray.from(iterable, (t) =\u003e t.id);\n\nArray.from(iterable, function(t) { this.format(t); }, this);\n\nconst arr = Array.from(iterable);\nconst mappedArray = arr.map((t) =\u003e t.id);\n```\n\n#### Using the rule\n\nTo use this rule, your `eslint.config.js` should at least contain the following:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  {\n    plugins: {\n      \"array-func\": arrayFunc\n    },\n    rules: {\n      \"array-func/from-map\": \"error\"\n    }\n  }\n];\n```\n\nAlternatively you can use a [configuration](#configurations) included with this plugin.\n\n### `no-unnecessary-this-arg`\n\nAvoid the `this` parameter when providing arrow function as callback in array functions.\n\nThe `this` parameter is useless when providing arrow functions, since the `this` of arrow functions can not be rebound, thus the parameter has no effect.\n\nThe 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.\n\n#### Checked Functions\n\n- `from` (fixable)\n\n#### Checked Methods\n\n- `every`\n- `filter`\n- `find`\n- `findIndex`\n- `forEach`\n- `map`\n- `some`\n\n#### Examples\n\nCode that triggers this rule:\n\n```js\nconst array = Array.from(\"example\", (char) =\u003e char.charCodeAt(0), this);\n\nconst e = array.find((char) =\u003e char === 101, this);\n\nconst exampleAsArray = array.map((char) =\u003e String.fromCharCode(char), this);\n\nconst eIndex = array.findIndex((char) =\u003e char === 101, this);\n\nconst containsE = array.some((char) =\u003e char === 101, this);\n\nconst isOnlyE = array.every((char) =\u003e char === 101, this);\n\nconst onlyEs = array.filter((char) =\u003e char === 101, this);\n\narray.forEach((char) =\u003e console.log(char), this);\n```\n\nCode that doesn't trigger this rule:\n\n```js\nconst array = Array.from(\"example\", (char) =\u003e char.charCodeAt(0));\nconst alternateArray = Array.from(\"example\", function(char) {\n    return char.charCodeAt(this)\n}, 0);\n\nconst e = array.find((char) =\u003e char === 101);\n\nconst exampleAsArray = array.map((char) =\u003e String.fromCharCode(char));\n\nconst eIndex = array.findIndex((char) =\u003e char === 101);\n\nconst containsE = array.some((char) =\u003e char === 101);\n\nconst isOnlyE = array.every((char) =\u003e char === 101);\n\nconst onlyEs = array.filter(function(char) {\n    return char === this\n}, 101);\n\narray.forEach(function(char) {\n    this.log(char);\n}, console);\n\narray.filter(this.isGood, this);\n```\n\n#### Using the rule\n\nTo use this rule, your `eslint.config.js` should at least contain the following:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  {\n    plugins: {\n      \"array-func\": arrayFunc\n    },\n    rules: {\n      \"array-func/no-unnecessary-this-arg\": \"error\"\n    }\n  }\n];\n```\n\nAlternatively you can use a [configuration](#configurations) included with this plugin.\n\n### `prefer-array-from`\n\nUse `Array.from` instead of `[...iterable]`.\nSee [`from-map`](#from-map) for additional benefits `Array.from` can provide over the spread syntax.\n\nThis rule is auto fixable.\n\n#### Examples\n\nCode that triggers this rule:\n\n```js\nconst iterable = [...\"string\"];\n\nconst arrayCopy = [...iterable];\n```\n\nCode that doesn't trigger this rule:\n\n```js\nconst array = [1, 2, 3];\n\nconst extendedArray =  [0, ...array];\n\nconst arrayCopy = Array.from(array);\n\nconst characterArray = Array.from(\"string\");\n```\n\n#### Using the rule\n\nTo use this rule, your `eslint.config.js` should at least contain the following:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  {\n    plugins: {\n      \"array-func\": arrayFunc\n    },\n    rules: {\n      \"array-func/prefer-array-from\": \"error\"\n    }\n  }\n];\n```\n\nAlternatively you can use a [configuration](#configurations) included with this plugin.\n\n### `avoid-reverse`\n\nAvoid reversing the array and running a method on it if there is an equivalent\nof the method operating on the array from the other end.\n\nThere are two operations with such equivalents: `reduce` with `reduceRight`.\n\nThis rule is auto fixable.\n\n#### Examples\n\nCode that triggers this rule:\n\n```js\nconst string = array.reverse().reduce((p, c) =\u003e p + c, '');\n\nconst reverseString = array.reverse().reduceRight((p, c) =\u003e p + c, '');\n```\n\nCode that doesn't trigger this rule:\n\n```js\nconst reverseString = array.reduce((p, c) =\u003e p + c, '');\n\nconst string = array.reduceRight((p, c) =\u003e p + c, '');\n\nconst reverseArray = array.reverse();\n\nconst reverseMap = array.reverse().map((r) =\u003e r + 1);\n```\n\n#### Using the rule\n\nTo use this rule, your `eslint.config.js` should at least contain the following:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  {\n    plugins: {\n      \"array-func\": arrayFunc\n    },\n    rules: {\n      \"array-func/avoid-reverse\": \"error\"\n    }\n  }\n];\n```\n\nAlternatively you can use a [configuration](#configurations) included with this plugin.\n\n### `prefer-flat-map`\n\nUse `.flatMap()` to map and then flatten an array instead of using `.map().flat()`.\n\nThis rule is auto fixable.\n\n#### Examples\n\nCode that triggers this rule:\n\n```js\nconst mappedAndFlattened = array.map((p) =\u003e p).flat();\n\nconst flatWithDefaultDepth = array.map((r) =\u003e r).flat(1);\n```\n\nCode that doesn't trigger this rule:\n\n```js\nconst oneAction = array.flatMap((m) =\u003e m);\n\nconst flattened = array.flat();\n\nconst mapped = array.map((r) =\u003e r + 1);\n\nconst flattenedThenMapped = array.flat().map((r) =\u003e r + 1);\n\nconst flatMappedWithExtra = array.map((r) =\u003e r + 1).reverse().flat();\n\nconst flatWithDepth = array.map((p) =\u003e p).flat(99);\n```\n\n#### Using the rule\n\nTo use this rule, your `eslint.config.js` should at least contain the following:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  {\n    plugins: {\n      \"array-func\": arrayFunc\n    },\n    rules: {\n      \"array-func/prefer-flat-map\": \"error\"\n    }\n  }\n];\n```\n\nAlternatively you can use a [configuration](#configurations) included with this plugin.\n\n### `prefer-flat`\n\nUse `.flat()` to flatten an array of arrays. This rule currently recognizes two\npatterns and can replace them with a `.flat()` call:\n\n- `[].concat(...array)`\n- `array.reduce((p, n) =\u003e p.concat(n), [])`\n\nThis rule is auto fixable.\n\n#### Examples\n\nCode that triggers this rule:\n\n```js\nconst concatFlat = [].concat(...array);\n\nconst reduceFlat = array.reduce((p, n) =\u003e p.concat(n), []);\n```\n\nCode that doesn't trigger this rule:\n\n```js\nconst flattened = array.flat();\n\nconst reverseFlat = array.reduce((p, n) =\u003e n.concat(p), []);\n\nconst otherReduce = array.reduce((p, n) =\u003e n + p, 0);\n```\n\n#### Using the rule\n\nTo use this rule, your `eslint.config.js` should at least contain the following:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  {\n    plugins: {\n      \"array-func\": arrayFunc\n    },\n    rules: {\n      \"array-func/prefer-flat\": \"error\"\n    }\n  }\n];\n```\n\nAlternatively you can use a [configuration](#configurations) included with this plugin.\n\n## Configurations\n\n### `recommended` Configuration\n\nRule | Error level | Fixable\n---- | ----------- | -------\n`array-func/from-map` | Error | Yes\n`array-func/no-unnecessary-this-arg` | Error | Sometimes\n`array-func/prefer-array-from` | Error | Yes\n`array-func/avoid-reverse` | Error | Yes\n\n#### Using the Configuration\n\nTo enable this configuration, import the plugin and add the config to your eslint config array:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  arrayFunc.configs.recommended,\n];\n```\n\n### `all` Configuration\n\nThe recommended configuration does not include all rules, since some Array methods\nwere added after ES2015. The all configuration enables all rules the plugin\ncontainsy.\n\nRule | Error level | Fixable\n---- | ----------- | -------\n`array-func/from-map` | Error | Yes\n`array-func/no-unnecessary-this-arg` | Error | Sometimes\n`array-func/prefer-array-from` | Error | Yes\n`array-func/avoid-reverse` | Error | Yes\n`array-func/prefer-flat-map` | Error | Yes\n`array-func/prefer-flat` | Error | Yes\n\n#### Using the Configuration\n\nTo enable this configuration, import the plugin and add the config to your eslint config array:\n\n```js\nimport arrayFunc from \"eslint-plugin-array-func\";\n\nexport default [\n  arrayFunc.configs.all,\n];\n```\n\n## License\n\nThe `array-func` plugin is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreaktechnik%2Feslint-plugin-array-func","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffreaktechnik%2Feslint-plugin-array-func","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreaktechnik%2Feslint-plugin-array-func/lists"}