2.0.0 β’ Published 2 years ago
@putout/plugin-apply-entries v2.0.0
@putout/plugin-apply-entries 
The
Object.entries()static method returns an array of a given object's own enumerable string-keyed property key-value pairs..(c) Object.entries()
The entries() method of Array instances returns a new array iterator object that contains the key/value pairs for each index in the array.
πPutout plugin adds ability to apply entries().
While entries() does the same for Object and Array, if you for some reason confuse this to methods, and use Object.entries() on array, you will have string-indexes.
- βοΈ Not bundled since it most likely will be bad for coverage
- βοΈ Requires additional configuration to work in ESLint and πPutout as it is ESM module
for (const [index, value] of Object.entries(['a', 'b', 'c'])) {
console.log(index === 1); // never true
}So (thanks to @putout/plugin-declare), you can use:
const {isArray} = Array;
const entries = (a) => isArray(a) ? a.entries() : Object.entries(a);
// entries called only once during loop initialization
for (const [index, value] of entries(['a', 'b', 'c'])) {
console.log(index === 1); // never true
}So instead of memorizing:
- β
Array.entries(); - β
Object.prototype.entries(); - β
Array.prototype.entries(); - β
Object.entries();
Just use:
- π
entries();
Checkout in πPutout Editor.
Install
npm i @putout/plugin-apply-entriesRule
{
"rules": {
"apply-entries": "on"
}
}β Example of incorrect code
for (const a of b.entries()) {}β Example of correct code
const {isArray} = Array;
const entries = (a) => isArray(a) ? a.entries() : Object.entries(a);
for (const a of entries(b)) {}License
MIT