-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-object-from-entries
rule (#1308)
- Loading branch information
Showing
10 changed files
with
1,644 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Prefer using `Object.fromEntries(…)` to transform a list of key-value pairs into an object | ||
|
||
When transforming a list of key-value pairs into an object, [`Object.fromEntries(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries) should be preferred. | ||
|
||
This rule is fixable for simple cases. | ||
|
||
## Fail | ||
|
||
```js | ||
const object = pairs.reduce( | ||
(object, [key, value]) => ({...object, [key]: value}), | ||
{} | ||
); | ||
``` | ||
|
||
```js | ||
const object = pairs.reduce( | ||
(object, [key, value]) => ({...object, [key]: value}), | ||
Object.create(null) | ||
); | ||
``` | ||
|
||
```js | ||
const object = pairs.reduce( | ||
(object, [key, value]) => Object.assign(object, {[key]: value}), | ||
{} | ||
); | ||
``` | ||
|
||
```js | ||
const object = pairs.reduce(addPairToObject, {}); | ||
``` | ||
|
||
```js | ||
const object = _.fromPairs(pairs); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const object = Object.fromEntries(pairs); | ||
``` | ||
|
||
```js | ||
const object = new Map(pairs); | ||
``` | ||
|
||
## Options | ||
|
||
Type: `object` | ||
|
||
### functions | ||
|
||
Type: `string[]` | ||
|
||
You can also check custom functions that transforms pairs. | ||
|
||
`lodash.fromPairs()` and `_.fromPairs()` are always checked. | ||
|
||
Example: | ||
|
||
```js | ||
{ | ||
'unicorn/prefer-object-from-entries': [ | ||
'error', | ||
{ | ||
functions: [ | ||
'getObjectFromKeyValue', | ||
'utils.fromPairs' | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
```js | ||
// eslint unicorn/prefer-object-from-entries: ["error", {"functions": ["utils.fromPairs"]}] | ||
const object = utils.fromPairs(pairs); // Fails | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.