sindresorhus/eslint-plugin-unicorn

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

已關閉

#2,767 建立於 2025年9月25日

 (5 則留言) (1 個反應) (0 位負責人)JavaScript (468 個分叉)user submission
help wantednew rule

倉庫指標

星標
 (5,022 顆星)
PR 合併指標
 (平均合併 4小時 30分鐘) (30 天內合併 26 個 PR)

描述

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

貢獻者指南