npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@putout/plugin-for-of

v6.1.1

Published

🐊Putout plugin adds ability to apply for...of

Downloads

25,522

Readme

@putout/plugin-for-of NPM version

The for...of statement creates a loop which invokes a custom iteration hook with statements to be executed for the value of each element of an array.

(c) MDN

🐊Putout plugin adds support of transformation for...of statements.

Install

npm i @putout/plugin-for-of

Rules

Configuration

{
    "rules": {
        "for-of/add-missing-declaration": "on",
        "for-of/map": "on",
        "for-of/for-in": "on",
        "for-of/for-each": "on",
        "for-of/reduce": "on",
        "for-of/remove-unused-variables": "on",
        "for-of/remove-useless": "on",
        "for-of/remove-useless-array-from": "on",
        "for-of/remove-useless-variables": ["on", {
            "maxProperties": 4
        }],
        "for-of/for": "on"
    }
}

add-missing-declaration

The const declaration declares block-scoped local variables.

(c) MDN

Chechout in 🐊Putout Editor.

❌ Example of incorrect code

for (name of names) {
    alert(`hello ${name}`);
}

✅ Example of correct code

for (const name of names) {
    alert(`hello ${name}`);
}

map

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

(c) MDN

❌ Example of incorrect code

names.map((name) => {
    alert(`hello ${name}`);
});

✅ Example of correct code

for (const name of names) {
    alert(`hello ${name}`);
}

for-in

The for...in statement iterates over all enumerable properties of an object that are keyed by strings.

(c) MDN

❌ Example of incorrect code

for (const item in object) {
    if (object.hasOwnProperty(item))
        log(item);
}

for (const item in object) {
    if (!object.hasOwnProperty(item))
        continue;
    
    log(item);
}

✅ Example of correct code

for (const item of Object.keys(object)) {
    log(item);
}

for-each

The forEach() method executes a provided function once for each array element.

(c) MDN

❌ Example of incorrect code

Object
    .keys(json)
    .forEach((name) => {
        manage(name, json[name]);
    });

[].forEach.call(arguments, (item) => {
    console.log(item);
});

✅ Example of correct code

for (const name of Object.keys(json)) {
    manage(name, json[name]);
}

for (const item of arguments) {
    console.log(item);
}

reduce

  • The reduce() method executes a user-supplied reducer callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

(c) MDN

You should always look at second argument of a reducer since it changes logic drastically and should read back and forth a couple times to understand what is going on.

Recursive functions like .reduce() can be powerful but sometimes difficult to understand, especially for less experienced JavaScript developers. If code becomes clearer when using other array methods, developers must weigh the readability tradeoff against the other benefits of using .reduce(). In cases where .reduce() is the best choice, documentation and semantic variable naming can help mitigate readability drawbacks.

(c) MDN

Check it out in 🐊Putout Editor.

❌ Example of incorrect code

const result = list.reduce((a, b) => a + b, 1);

✅ Example of correct code

let sum = 1;

for (const a of list) {
    sum += a;
}

remove-unused-variables

❌ Example of incorrect code

for (const {a, b} of c) {
    console.log(a);
}

✅ Example of correct code

for (const {a} of c) {
    console.log(a);
}

remove-useless

The Array enables storing a collection of multiple items under a single variable name.

(c) MDN

❌ Example of incorrect code

for (const a of ['hello']) {
    console.log(a);
}

✅ Example of correct code

console.log('hello');

remove-useless-variables

❌ Example of incorrect code

for (const a of b) {
    const {c} = a;
}

✅ Example of correct code

for (const {c} of b) {}

remove-useless-array-from

The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object.

(c) MDN

❌ Example of incorrect code

for (const x of Array.from(y)) {}

✅ Example of correct code

for (const x of y) {}

for

The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement to be executed in the loop.

(c) MDN

for-n

❌ Example of incorrect code

const n = items.length;

for (let i = 0; i < n; i++) {
    const item = items[i];
    log(item);
}

✅ Example of correct code

for (const item of items) {
    log(item);
}

for-length

❌ Example of incorrect code

for (let i = 0; i < array.length; i++) {
    const item = array[i];
    console.log(item);
}

✅ Example of correct code

for (const item of items) {
    log(item);
}

for-entries

❌ Example of incorrect code

for (let i = 0; i < array.length; i++) {
    const item = array[i];
    console.log(i, item);
}

✅ Example of correct code

for (const [i, item] of array.entries()) {
    console.log(i, item);
}

for-entries-n

❌ Example of incorrect code

const n = array.length;

for (let i = 0; i < n; i++) {
    const item = array[i];
    console.log(i, item);
}

✅ Example of correct code

for (const [i, item] of array.entries()) {
    console.log(i, item);
}

License

MIT