sindresorhus/eslint-plugin-unicorn

Rule proposal: Enforce `.toArray()` at the end of `Iterator` methods chain

Open

#2,767 创建于 2025年9月25日

在 GitHub 查看
 (5 评论) (1 反应) (0 负责人)JavaScript (5,022 star) (468 fork)user submission
help wantednew rule

描述

Description

Note that it's a good idea to call toArray() as a last step of your processing. For example, fibonacci().take(10).toArray().filter(...) is less efficient, because iterator helpers are lazy and avoids creating a temporary array.

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/toArray

Examples

// ❌
foo.toArray().filter(...)
//            ^^^^^^ Array#filter()

// ✅
foo.filter(...).toArray()
//  ^^^^^^ Iterator#filter()

Proposed rule name

not sure

Additional Info

No response

贡献者指南

Rule proposal: Enforce `.toArray()` at the end of `Iterator` methods chain · sindresorhus/eslint-plugin-unicorn#2767 | Good First Issue