Skip to content

Files

Latest commit

d3e6231 · Mar 23, 2025

History

History

plugin-apply-entries

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Aug 23, 2023
Dec 28, 2024
Aug 14, 2023
Mar 5, 2025
Apr 10, 2024
Apr 13, 2024
Dec 28, 2024
Aug 14, 2023
Aug 23, 2023
Feb 21, 2025
Mar 23, 2025

@putout/plugin-apply-entries NPM version

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.

(c) Array.prototype.entries()

🐊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-entries

Rule

{
    "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