@putout/plugin-apply-entries
v2.0.0
Published
🐊Putout plugin adds ability apply 'entries()'
Maintainers
Readme
@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
