The type of values yielded by this iterator.
Determines whether all the members of this iterator satisfy the specified test.
A function that accepts up to two arguments. The every method calls the predicate function for each element in this iterator until the predicate returns false, or until the end of this iterator.
Returns the value of the first element in this iterator where predicate is true, and undefined otherwise.
Performs the specified action for each element in the iterator.
A function that accepts up to two arguments. forEach calls the callbackfn function one time for each element in the iterator.
Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator.
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a value from the iterator.
OptionalreturnOptionalvalue: anyDetermines whether the specified callback function returns true for any element of this iterator.
A function that accepts up to two arguments. The some method calls the predicate function for each element in this iterator until the predicate returns a value true, or until the end of the iterator.
OptionalthrowOptionale: anyCreates a new array from the values yielded by this iterator.
Consumes the iterator and returns the value at the provided index, or undefined if the index is out of bounds.
The index of the value to return. If negative, it counts from the end of the iterator, starting with
-1 for the last element, -2 for the second to last, and so on.
The value at the provided index, or undefined if the index is out of bounds (i.e., if there are
not enough elements in the iterator).
Consumes the iterator and returns a value determined by calling the provided function using the iterator as argument.
The type of the value returned by the provided function.
The value returned by calling the provided callback function with this iterator as argument.
Returns the first element of the iterator, or undefined if the iterator is empty.
The first element of the iterator, or undefined if the iterator is empty.
Groups the elements in this iterator into separate arrays and returns an object containing all the groups.
The returned object is composed of keys generated by calling the provided callback function on each element of this iterator, and the value for each key is an array containing all the elements that were assigned to that key.
This method is similar to toMap, but the returned object is a plain, null-prototype, object, instead of a
Map.
The union type that contains all the key values of returned group object.
An object that contains all the groups of elements from this iterator, based on the keys returned by the callback function.
Consumes the iteratror and returns the last yielded value; or undefined if the iterator is empty.
The last element of the iterator, or undefined if the iterator is empty.
Consumes the iterator and returns a boolean indicating whether all elements in the iterator were unique.
true if the iterator contains no pair of elements that are equal, including when the iterator is
empty; or false if there was at least one pair of equal elements.
Groups elements into separate arrays and returns a Map containing each group.
The returned Map is composed of keys generated by calling the provided callback function on each element of this iterator, and the value for each key is an array containing all the elements to which the callback function returned that key.
This method is similar to groupBy, but the returned object is a Map instead of a plain object.
The union type that contains all the key values of returned Map.
A Map that contains all the groups of elements from this iterator, based on the keys returned by the callback function.
Creates a set containing all the values yielded by this iterator.
A set containing all the values yielded by this iterator.
Optionalvalue: anyLazily executes a function over each element of this iterator as the values are iterated.
A function that takes a value from this iterator and its index, and performs some side effect.
An iterator that yields the same values as this iterator, but executes the provided callback function for each yielded element.
Unlike forEach, this method does not execute the provided callback function immediately. Instead, it returns a new iterator that executes the callback function lazily as the values are iterated.
The returned iterator yields the same values as this iterator, without any modification.
StaticconcatCreates a new iterator by concatenating multiple iterables or array-like objects.
The type of values yielded by the provided iterables.
Several iterables or array-like objects to concatenate.
An iterator that is the concatenation of the provided iterables, yielding the values from each of them in sequence.
The resulting iterator iterates over each argument iterable in the order they are provided. For each one, it yields all of its values before moving on to the next iterable.
If any of the provided iterables is infinite, the resulting iterator will also be infinite and will never yield values from the subsequent iterables.
StaticcountCreates an iterator that yields incrementing numbers.
An optional object to configure the behavior of the returned iterator.
Optionalincrement?: numberThe difference between each pair of consecutive numbers yielded by the returned
iterator. If you set a negative increment, the iterator will count downwards. Default is 1.
Optionalstart?: numberThe first number yielded by the iterator. Default is 0.
An iterator that yields incrementing numbers starting from start and incrementing by increment.
⚠ This iterator is infinite. Use take method if you want a specific number of values.
StaticemptyCreates an iterator that yields no value.
The type parameter of the returned iterator. If omitted, the returned iterator will be of any type.
An iterator that yields no value.
StaticfromCreates a new ExtraIterator from an iterable, iterator, or array-like object.
The type of the elements of the source parameter.
The source to create the iterator from. This can be any object that is either an Iterator, an
Iterable, or an array-like object (i.e., has a length property and numerically indexed elements).
A new ExtraIterator instance.
StaticrandomGenerates an infinite sequence of random numbers between 0 and 1 (inclusive–exclusive) using Math.random or
another specified random number generator.
An optional random number generator function that returns a number between 0 and 1
(inclusive–exclusive). If not provided, Math.random will be used as the default random number generator.
Returns a pseudorandom number between 0 and 1.
An iterator that generates an infinite sequence of random numbers between 0 and 1.
⚠ This iterator is infinite. Use take method if you want a specific number of values.
StaticrandomGenerates an infinite sequence of cryptographically strong random bytes using crypto.getRandomValues, in
chunks of bufferSize bytes.
An optional object to configure the behavior of the returned iterator.
OptionalbufferSize?: numberThe number of random bytes to generate in each chunk. Default is 1024.
OptionalsharedBuffer?: booleanIf false (which is the default), each yielded Uint8Array instance owns its own
buffer containing the random bytes. This is safer since you won't accidentally modify the values of a previously
yielded chunk by modifying a later one, but the iterator has to allocate a new buffer for each chunk. If this
option is set to true, the resulting iterator will yield new Uint8Array views of the same underlying buffer,
refilling the buffer with new random bytes between each iteration. This is more performant, but you should be
careful to not keep references to the old views since the values will change.
An iterator that generates an infinite sequence of cryptographically strong random bytes in chunks of
bufferSize bytes.
If you want a flat sequence of individual byte values instead of chunks, you can chain the returned iterator with the flat method. The resulting iterator will contain interger values from 0 to 255 (inclusive).
⚠ This iterator is infinite. Use take method if you want a specific number of values.
// Produces an array with 3 chunks of random bytes, each containing 16 bytes, e.g.
// // [ // Uint8Array(16) [ 0x12, 0x34, ..., 0x56 ], // Uint8Array(16) [ 0x78, 0x9a, ..., 0xbc ], // Uint8Array(16) [ 0xde, 0xf0, ..., 0x12 ], // ] //
const chunks = ExtraIterator.randomBytes({ bufferSize: 16 })
.take(3)
.toArray();
StaticrangeCreates an iterator that yields numbers in a "from-to" range. (exclusive)
The number at which the returned iterator starts yielding values.
The number at which the returned iterator stops yielding values. This value is not included in the
range unless inclusive is set to true.
An optional object to configure the behavior of the returned iterator.
Optionalinclusive?: booleanA boolean that indicates whether the end value should be included in the range.
Default is false.
Optionalstep?: numberThe increment (or decrement) between each yielded number. Default is 1.
An iterator that yields numbers starting from start and incrementing (or decrementing) by step, up
to end.
The returned iterator yields all numbers in the specified range. By default, the end value is not included in
the range. If you want to include the end value as well, you can set the inclusive option to true.
The third argument is an optional "step" that defines the increment (or decrement) between each yielded number.
Note that setting inclusive option to true does not guarantee that the end value will be yielded, since it
also depends on the step value. For example, if start is 0, end is 5, and step is 2, the returned
iterator will not yield 5 regardless of the inclusive option, because it is not included in the range.
ExtraIterator.range(5, 10).toArray() // returns [5, 6, 7, 8, 9]
ExtraIterator.range(5, 10, { inclusive: true }).toArray() // returns [5, 6, 7, 8, 9, 10]
StaticrepeatCreates an iterator that repeatedly yields the provided value.
The type of the value yielded by the returned iterator.
The value to be repeatedly yielded by the returned iterator.
An iterator that repeatedly yields the provided value.
⚠ This iterator is infinite. Use take method if you want a specific number of values.
StaticsingleCreates an iterator that yields a single value.
The type of the value yielded by the returned iterator.
The value the returned iterator will yield.
An iterator that yields the provided value once, then ends.
StaticzipCreates a new iterator that iterates over all the provided iterators simultaneously. The returned iterator yields arrays containing the values yielded by each of the provided iterators.
The type of values yielded by the provided iterables.
Several iterables or array-like objects to iterate over.
An iterator that iterates over all the provided iterables simultaneously. For each iteration, it yields an array with the elements of each iterable at the corresponding position.
Appends a new value to the end of the iterator.
The value to append to the end of the iterator.
A new iterator that yields the values of this iterator, followed by the provided value.
Groups the elements in this iterator into arrays of fixed size.
The size of the chunks. Must be a positive integer.
A new iterator that yields arrays of elements from this iterator, grouped by chunks of the specified size.
This method produces a new iterator that contains all elements of this iterator, grouped by chunks of fixed size.
The returned iterator yields arrays, each one containing a subset of the elements of this iterator.
Each array has the length specified by the size parameter, except possibly the last one, which may contain
fewer elements if the total number of elements in this iterator is not divisible by size.
Groups the elements in this iterator into arrays based on a key provided by a callback function.
A function that takes a value from this iterator and returns a key. Adjacent values for which this function returns the same key will be grouped together in the output.
A new iterator that yields arrays of elements from this iterator, grouped based on the keys returned by the callback function.
The provided callback function will be called for each element in this iterator. It should return a key value. Adjacent elements for which the callback function returns the same key will be grouped together.
The returned iterator yields arrays, each one containing a subset of the elements of this iterator that share the same key according to the provided callback function.
The order of the elements is preserved, and the grouping is stable. This means that if two adjacent elements have the same key, they will appear in the same order in the output as they do in the input.
Groups the elements in this iterator into groups of variable size.
An iterator that yields the elements from this iterator, grouped into arrays based on the provided predicate.
Creates a new iterator that yields the values of this iterator, but won't yield any null or undefined values.
A new iterator that yields the non-null and non-undefined values of this iterator.
Concatenates multiple values to the end of this iterator.
An iterable of values to concatenate to the end of this iterator.
A new iterator that yields the values of this iterator, followed by the values of the provided items
iterable.
If this iterator is empty, returns an iterator with the provided element as its only element; otherwise, it returns a copy of this iterator.
A value to yield if this iterator is empty.
A new iterator that yields the values of this iterator, or the provided default value if this iterator is empty.
If this iterator is empty, calls the provided callback function and yields the returned value as the iterator's only element; otherwise, returns a copy of this iterator.
A function that provides the default value to yield if this iterator is empty.
A new iterator that yields the values of this iterator, or the provided default value if this iterator is empty.
Creates a new iterator that skips the first count values of this iterator and yields the remaining values.
The number of values to skip. If negative, skips values from the end.
A new iterator that yields the remaining values.
Creates a new iterator that invokes the provided predicate function for each element of this iterator and skips
the elements for which the function returns true, but only for as long as it returns true.
A function that takes a value and its index, and returns a boolean indicating whether the value
should be skipped (if true) or yielded (if false). Iteration stops once this function returns false.
A new iterator that yields the values of this iterator starting from the first value for which the
provided predicate function returns false.
Creates an iterator whose values are those from this iterator for which the provided predicate returns true.
A function that accepts up to two arguments to be used to test values from the underlying iterator.
Flattens the iterator by one level. If the iterator yields iterables, it will yield their values. If the iterator yields non-iterables, it will yield the values as is.
An optional object to configure the behavior of the flattening process.
Optionalarraylike?: booleanIf true, the flattening process will also flatten "array-like" objects (i.e., objects
that have a length property and numerically indexed elements) as iterables. By default, only objects that
implement the iterable protocol (i.e., have a [Symbol.iterator] method) are flattened.
A new iterator that yields the flattened values of this iterator.
Creates a new iterator that yields the values of this iterator and the values of the provided iterator interleaved (alternating).
The type of the elements of the other iterator.
An iterable to interleave with this iterator.
A new iterator that yields the values of this iterator and the values of the provided other iterator interleaved.
The resulting iterator yields the first value of this iterator, then the first value of the other iterator, then the second value of this iterator, then the second value of the other iterator, and so on, alternating between the two iterators until both of them are exhausted. The elements of this iterator always come before the elements of the other iterator in the corresponding index.
Once one of the iterators is exhausted, the resulting iterator will yield the remaining values of the other iterator until it is also exhausted.
Creates a new iterator that yields the values of this iterator interposed by a separator value. i.e., The separator is inserted between each pair of subsequent elements of this iterator.
The type of the separator value.
The value to interpose between each pair of subsequent elements of this iterator.
A new iterator that yields the values of this iterator interposed by the provided separator.
Creates a new iterator that yields the values of this iterator interposed by separator values produced by calling the callback function provided as argument.
The type of the separator value.
A new iterator that yields the values of this iterator interposed by the provided separator values.
Creates a new iterator that loops over this iterator, yielding the same values repeatedly.
Optionaloptions: { pingpong?: boolean }A new iterator that loops over this iterator the specified number of times.
Creates a new iterator that yields the elements of this iterator a number of times specified by the {@param times} parameter. The resulting iterator first iterates over this iterator, yielding all its values in order, and then reiterates over the same values and yields each of them again, again and again, until it has looped the specified number of times.
If the times parameter is Infinity (the default), the resulting iterator will loop indefinitely.
Creates a new iterator that loops over this iterator, yielding the same values repeatedly.
Optionaltimes: numberThe number of times to loop over this iterator. Must be an integer number or Infinity (which is
the default). If negative, the resulting iterator will be inverted. If zero, the resulting iterator will be
empty.
Optionaloptions: { pingpong?: boolean }A new iterator that loops over this iterator the specified number of times.
Creates a new iterator that yields the elements of this iterator a number of times specified by the {@param times} parameter. The resulting iterator first iterates over this iterator, yielding all its values in order, and then reiterates over the same values and yields each of them again, again and again, until it has looped the specified number of times.
If the times parameter is Infinity (the default), the resulting iterator will loop indefinitely.
Prepends a new value to the beginning of the iterator. The new value will be yielded first, then the rest of this iterator will be yielded.
The value to prepend to the beginning of the iterator.
A new iterator that yields the provided value, followed by the values of this iterator.
Concatenates multiple values to the start of this iterator.
An iterable of values to concatenate to the start of this iterator.
A new iterator that yields the values of the provided items iterable, followed by the values of this
iterator.
Replaces some elements of this iterator with new values.
The index of the first element to be replaced; and the index where the new elements will be inserted. If negative, it will begin that many elements from the end of the iterator.
The number of elements to be removed.
The new elements to be inserted.
// Replaces 'Bob' and 'Charlie' with 'Eve' and 'Frank'
ExtraIterator.from(['Alice', 'Bob', 'Charlie', 'David'])
.splice(1, 2, 'Eve', 'Frank')
.toArray()
// returns ['Alice', 'Eve', 'Frank', 'David']
Creates a new iterator that yields only the first limit values of this iterator. If limit is negative, it
yields the last -limit values instead.
The number of values to take. If negative, takes values from the end.
A new iterator that yields the specified number of values.
If limit is greater than the number of elements in this iterator, then the returned iterator will yield all the
values of this iterator.
If limit is negative, the returned iterator will consume the iterator and return the last -limit values,
preserving the order of the original iterator. (i.e., the last element yielded by this iterator will be yielded
last by the resulting iterator)
Be careful when using a negative limit with infinite iterators, as it will cause an infinite loop.
Creates a new iterator that invokes the provided callback function for each element of this iterator and yields
the elements for which the callback returns true, only for as long as the callback returns true.
A function that takes a value and its index, and returns a boolean indicating whether the value
should be kept (if true) or discarded (if false). Iteration stops once this function returns false.
A new iterator that yields the values of this iterator for which the provided predicate function
returns true.
This is similar to filter, except iteration stops once the callback returns false for the first time.
Creates a new iterator that yields the values of this iterator, but won't yield any duplicates.
An optional function that returns a key for each value. The keys are used to determine whether two values are equal or not. If two values have the same key, only the first one will be yielded. The other is ignored.
A new iterator that yields the unique values of this iterator.
If the elements of this iterator are also iterable, then this method creates a new iterator that iterates on all those iterable elements simultaneously.
A new iterator that iterates on all the iterable elements of this iterator simultaneously.
Creates a new iterator that iterates on this iterator and the proviuded other iterator, yielding arrays of pairs of elements from this iterator and the other.
An iterable to zip with this iterator.
A new iterator that iterates on this iterator and the provided other iterator simultaneously.
If the elements of this iterator are also iterable, then this method creates a new iterator that iterates on all those iterable elements simultaneously.
A new iterator that iterates on all the iterable elements of this iterator simultaneously.
If the elements of this iterator are also iterable, then this method creates a new iterator that iterates on all those iterable elements simultaneously.
A new iterator that iterates on all the iterable elements of this iterator simultaneously.
If the elements of this iterator are also iterable, then this method creates a new iterator that iterates on all those iterable elements simultaneously.
A new iterator that iterates on all the iterable elements of this iterator simultaneously.
Creates a new iterator that iterates on this iterator and the proviuded other iterators simultaneously, yielding arrays with the elements these iterators at the corresponding position.
Several iterables to zip with this iterator.
A new iterator that iterates on this iterator and the provided other iterators simultaneously.
An extended iterator class that provides additional chainable utility methods for working with iterables.
Example
Example