Array and iterable helpers provide utilities for array conversion, iteration, and destructuring operations that handle edge cases and ensure consistent behavior across JavaScript environments.
Converts array-like objects to real arrays with optional length limit.
/**
* Converts array-like objects to real arrays with optional length limit
* @param arr - Array-like object to convert
* @param len - Optional length limit
* @returns Real array with copied elements
*/
function arrayLikeToArray(arr: ArrayLike<any>, len?: number): any[];Usage Example:
import _arrayLikeToArray from '@babel/runtime/helpers/esm/arrayLikeToArray';
const nodeList = document.querySelectorAll('div');
const array = _arrayLikeToArray(nodeList);Checks if value is array, preserving holes for destructuring operations.
/**
* Checks if value is array, preserving holes for destructuring
* @param arr - Value to check
* @returns Array or undefined to preserve holes
*/
function arrayWithHoles(arr: any): any[] | undefined;Converts array-like objects to arrays without holes, suitable for spread operations.
/**
* Converts array-like to array without holes
* @param arr - Array-like object
* @returns Array without holes
*/
function arrayWithoutHoles(arr: ArrayLike<any>): any[];Converts iterables (like Set, Map) to arrays.
/**
* Converts iterables to arrays
* @param iter - Iterable object
* @returns Array with iterable contents
*/
function iterableToArray(iter: Iterable<any>): any[];Converts iterables to arrays with a length limit.
/**
* Converts iterables to arrays with length limit
* @param iter - Iterable object
* @param i - Maximum number of elements
* @returns Array with limited elements
*/
function iterableToArrayLimit(iter: Iterable<any>, i: number): any[];Fallback conversion for non-standard iterables and typed arrays.
/**
* Fallback conversion for non-standard iterables
* @param o - Object to convert
* @param minLen - Minimum length
* @returns Array or undefined
*/
function unsupportedIterableToArray(o: any, minLen?: number): any[] | undefined;Checks if a value is array-like (has length property).
/**
* Checks if value is array-like
* @param next - Value to check
* @returns Array-like object or undefined
*/
function maybeArrayLike(next: any): ArrayLike<any> | undefined;Generic array conversion with support for destructuring patterns.
/**
* Generic array conversion for destructuring
* @param arr - Value to convert to array
* @returns Array representation
*/
function toArray(arr: any): any[];Usage Example:
import _toArray from '@babel/runtime/helpers/esm/toArray';
// Used by Babel for: const [first, ...rest] = someValue;
const result = _toArray(someValue);
const first = result[0];
const rest = result.slice(1);Converts values to arrays suitable for spread operations.
/**
* Converts to array for spread operations
* @param arr - Value to convert for spreading
* @returns Array ready for spread syntax
*/
function toConsumableArray(arr: any): any[];Usage Example:
import _toConsumableArray from '@babel/runtime/helpers/esm/toConsumableArray';
// Used by Babel for: [...someIterable]
const spread = _toConsumableArray(someIterable);Array slicing for destructuring assignment with proper length handling.
/**
* Array slicing for destructuring assignment
* @param arr - Array to slice
* @param i - Number of elements needed
* @returns Array with exactly i elements (undefined-padded if needed)
*/
function slicedToArray(arr: any, i: number): any[];Usage Example:
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
// Used by Babel for: const [a, b] = someArray;
const _someArray = _slicedToArray(someArray, 2);
const a = _someArray[0];
const b = _someArray[1];Creates optimized for-of loop iterator helpers with proper cleanup.
/**
* Creates for-of loop iterator helpers
* @param o - Object to iterate
* @param allowArrayLike - Allow array-like objects
* @returns Iterator helper object
*/
function createForOfIteratorHelper(o: any, allowArrayLike?: boolean): {
s(): void;
n(): { done: boolean; value: any };
e(error: any): void;
f(): void;
};Loose mode for-of iterator helpers with smaller bundle size.
/**
* Loose mode for-of iterator helpers (smaller bundle)
* @param o - Object to iterate
* @param allowArrayLike - Allow array-like objects
* @returns Simplified iterator helper
*/
function createForOfIteratorHelperLoose(o: any, allowArrayLike?: boolean): {
s(): void;
n(): { done: boolean; value: any };
e(error: any): void;
f(): void;
};Throws appropriate error for non-iterable rest patterns.
/**
* Throws error for non-iterable rest
* @throws TypeError when rest pattern applied to non-iterable
*/
function nonIterableRest(): never;Throws appropriate error for non-iterable spread operations.
/**
* Throws error for non-iterable spread
* @throws TypeError when spread applied to non-iterable
*/
function nonIterableSpread(): never;// Array-like interface
interface ArrayLike<T> {
readonly length: number;
readonly [n: number]: T;
}
// Iterable interface
interface Iterable<T> {
[Symbol.iterator](): Iterator<T>;
}
// Iterator interface
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
interface IteratorResult<T> {
done: boolean;
value: T;
}
// For-of helper interface
interface ForOfIteratorHelper {
s(): void; // start iteration
n(): IteratorResult<any>; // get next value
e(error: any): void; // handle error
f(): void; // finalize/cleanup
}