sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-map`

Chiusa

#1285 aperta il 18 mag 2021

 (2 commenti) (4 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

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)
}

Guida contributor