sendwend
v0.0.1
Published
Get the last value of a finite iterable or async iterable with constant extra memory.
Readme
SendWend
Get the last value of a finite iterable or async iterable with constant extra memory. Zero dependencies. Supports ESM, CommonJS, and TypeScript.
npm install sendwendimport { last, lastAsync } from 'sendwend';
last([1, 2, 3]); // 3
last([], 'empty'); // 'empty'
last(new Set(['a', 'b'])); // 'b'
async function* values() {
yield 1;
yield 2;
}
await lastAsync(values()); // 2
await lastAsync([Promise.resolve(1), Promise.resolve(2)]); // 2CommonJS: const { last, lastAsync } = require('sendwend');
This early release provides the iterable helpers below. The workflow orchestration API is not included in this release.
Project website: https://sendwend.com
API
last(iterable, fallback?): returns the final value, or the fallback if the input is empty. The default fallback isundefined.lastAsync(iterable, fallback?): returns a Promise for the final value or fallback. Accepts sync and async iterables and awaits values from sync iterables.
Both functions consume the entire input once, run in O(n) time, and use O(1) extra memory. Use finite inputs only. Iterator errors propagate to the caller. An actual last value of undefined is returned as-is, even when a fallback is provided. The async function follows JavaScript's for await...of semantics.
License
MIT
