@putout/plugin-apply-entries

2.0.0 • Public • Published

@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

Package Sidebar

Install

npm i @putout/plugin-apply-entries

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

5.08 kB

Total Files

4

Last publish

Collaborators

  • coderaiser