@harperfast/extended-iterable

中文文档
1.0.5发布于 2026/10/21Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 1758.3万

Extended iterable class, providing lazy array-like methods with automatic async and return/throw forwarding

原始使用文档
文档目录3 节
  1. Catching Errors in iteration
  2. License
  3. Related Projects

license npm version npm downloads

The extended-iterable package provides a class that implements the Iterable/Iterator protocol and provides array-like methods with lazy evaluation, similar to iterators helpers. However, extended-iterable provides additional methods, the ability to handle both sync and async iteration, automatic handling of asynchronous callbacks, and return/throw forwarding which can be critical for proper cleanup when used with database transactions.

The package exports a single ExtendedIterable class, which can be constructed with a basic iterable or can be constructed with no arguments and assigned (or overridden with) an iterate method that returns an iterator.

For example, we can create an ExtendedIterable:

import { ExtendedIterable } from '@harperdb/extended-iterable';

function performQuery() {
    let results = new ExtendedIterable();
    results.iterate = function() {
        let txn = startTxn(); // we can start a transaction in some database, and be notified of when the iterable is completed below
        let cursor = txn.doQuery();
        return {
            // implement an iterator
            next() {
                // iterate through a query or table
                let entry = cursor.getNext();
                if (entry)
                    return { value: entry };
                else
                    return { done: true };
            },
            return () { // called when the iterator is done
                txn.commit();
            },
            throw () {
                txn.abort();
            }
        }
    };
    return results;
}

The returned ExtendedIterable is fully iterable:

let extendedIterable = performQuery();
for (let value of extendedIterable) {
    // for each value
}

Or we can use the provided iterative methods on the returned results:

extendedIterable
    .filter((value) => test(value))
    .forEach((value) => {
        // for each value that matched the filter
    })

Note that map and filter are also lazy, they will only be executed once their returned iterable is iterated or forEach is called on it. The map and filter functions also support async/promise-based functions, and you can create an async iterable if the callback functions execute asynchronously (return a promise).

We can also query with offset to skip a certain number of entries, and limit the number of entries to iterate through:

let sliced = extendedIterable.slice(10, 20); // skip first 10 and get next 10

If you want to get a true array from the results, the asArray property will return the results as an array.

Catching Errors in iteration

With an array, map and filter callbacks are immediately executed, but wit iterators, they are executed during iteration, so if an error occurs during iteration, the error will be thrown when the iteration is attempted. It is also critical that when an iteration is finished, the cursor is closed, so by default, if an error occurs during iteration, the cursor will immediately be closed. However, if you want to catch errors that occur in map (and flatMap) callbacks during iteration, you can use the mapError method to catch errors that occur during iteration, and allow iteration to continue (without closing the cursor). For example:

let mapped = extendedIterable.map(({ key, value }) => {
    return thisMightThrowError(value);
}).mapError((error) => {
    // rather than letting the error terminate the iteration, we can catch it here and return a value to continue iterating:
    return 'error occurred';
})
for (let entry of mapped) {
...
}

A mapError callback can return a value to continue iterating, or throw an error to terminate the iteration.

License

This library is licensed under the terms of the MIT license.

版本与发布记录

@harperfast/extended-iterable 共发布 5 个版本,首次发布于 2025/12/18;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.0.5latest2026/10/2127 天前
1.0.42026/10/2028 天前
1.0.32026/01/249 个月前
1.0.22026/01/249 个月前
1.0.12025/12/1810 个月前

关于 @harperfast/extended-iterable 的常见问题

@harperfast/extended-iterable 是什么?

Extended iterable class, providing lazy array-like methods with automatic async and return/throw forwarding

如何安装 @harperfast/extended-iterable?

在项目目录运行 npm install @harperfast/extended-iterable,或使用 pnpm add / yarn add / bun add。

@harperfast/extended-iterable 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@harperfast/extended-iterable 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@harperfast/extended-iterable 可以商用吗?

@harperfast/extended-iterable 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@harperfast/extended-iterable 的下载量有多少?

根据 npm 官方下载统计,@harperfast/extended-iterable 在 2026/10/18 – 2026/11/16 这 30 天内被下载 17,582,750 次,日均约 586,092 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@harperfast/extended-iterable 还在维护吗?

最新版本 1.0.5 发布于 2026/10/21(27 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2025/12/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。