sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-map`

Closed

#1,285 opened on May 18, 2021

 (2 comments) (4 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

If an object is created from pairs, and only used to check existence and READ properties. Should prefer use new Map() instead of Object.fromEntries(), kind of similar to prefer-set-has.

Fail

const object = Object.fromEntries(/*...*/);

if (object.foo) {
	console.log(object.foo)
}

Pass

const object = new Map(/*...*/);

if (object.has('foo')) {
	console.log(object.get('foo'))
}
// Object literal should be ignored
const object = {
// ...
};

if (object.foo) {
	console.log(object.foo)
}

Contributor guide