@jhel/iterup
Version:
A TypeScript iterator utility library that provides lazy evaluation for efficient data processing
105 lines • 3.02 kB
JavaScript
/**
* Utility functions for type checking and working with iterators.
*
* This module provides helper functions to determine the type of iterators
* and check for specific iterator interfaces at runtime.
*/
import { IterupID, None, } from "./core";
/**
* Type guard to check if a value is a synchronous iterator.
* Checks for the presence of the Symbol.iterator method.
*
* @template Value - The type of values the iterator should yield
* @param value - The value to check
* @returns True if the value is a synchronous iterator
*
* @example
* ```ts
* if (isIterator(someValue)) {
* // someValue is now typed as BaseSyncIterator<Value>
* for (const item of someValue) {
* console.log(item);
* }
* }
* ```
*/
export function isIterator(value) {
if (typeof value !== "object")
return false;
if (value === null)
return false;
return (Symbol.iterator in value && typeof value[Symbol.iterator] === "function");
}
/**
* Type guard to check if a value is iterable (has Symbol.iterator).
* This includes iterables like arrays, sets, maps, and strings.
*
* @template Value - The type of values the iterable should yield
* @param value - The value to check
* @returns True if the value is iterable
*
* @example
* ```ts
* if (isIterable(someValue)) {
* for (const item of someValue) {
* console.log(item);
* }
* }
* ```
*/
export function isIterable(value) {
if (typeof value !== "object")
return false;
if (value === null)
return false;
return (Symbol.iterator in value && typeof value[Symbol.iterator] === "function");
}
/**
* Type guard to check if a value is an asynchronous iterator.
* Checks for the presence of the Symbol.asyncIterator method.
*
* @template Value - The type of values the iterator should yield
* @param value - The value to check
* @returns True if the value is an asynchronous iterator
*
* @example
* ```ts
* if (isAsyncIterator(someValue)) {
* for await (const item of someValue) {
* console.log(item);
* }
* }
* ```
*/
export function isAsyncIterator(value) {
if (typeof value !== "object")
return false;
if (value === null)
return false;
return (Symbol.asyncIterator in value &&
typeof value[Symbol.asyncIterator] === "function");
}
/**
* Type guard to check if an async iterator is an Iterup instance.
* Checks for the presence of the IterupID symbol that marks enhanced iterators.
*
* @template Value - The type of values the iterator yields
* @param value - The async iterator to check
* @returns True if the iterator is an Iterup instance with extension methods
*
* @example
* ```ts
* if (isIterup(someAsyncIterator)) {
* const result = await someAsyncIterator.map(x => x * 2).collect();
* }
* ```
*/
export function isIterup(value) {
return IterupID in value;
}
export function unwrapResult(result) {
if (result.done)
return None;
return result.value;
}
//# sourceMappingURL=utils.js.map