iteragain
Advanced tools
Comparing version 3.5.0 to 3.6.0
@@ -5,3 +5,4 @@ import ExtendedIterator from './internal/ExtendedIterator'; | ||
export declare function iter<T>(arg: IteratorOrIterable<T>): ExtendedIterator<T>; | ||
export declare function iter<TFunc extends (...args: any[]) => any>(arg: TFunc, sentinel?: ReturnType<TFunc>): ExtendedIterator<ReturnType<TFunc>>; | ||
export declare function iter(arg: Record<PropertyKey, any>): ExtendedIterator<ObjectEntry>; | ||
export default iter; |
@@ -6,6 +6,6 @@ "use strict"; | ||
const toIterator_1 = require("./toIterator"); | ||
function iter(arg) { | ||
return new ExtendedIterator_1.default((0, toIterator_1.default)(arg)); | ||
function iter(arg, sentinel) { | ||
return new ExtendedIterator_1.default((0, toIterator_1.default)(arg, sentinel)); | ||
} | ||
exports.iter = iter; | ||
exports.default = iter; |
{ | ||
"name": "iteragain", | ||
"version": "3.5.0", | ||
"version": "3.6.0", | ||
"description": "Javascript Iterable/Iterator/Generator-function utilities.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,2 +18,4 @@ | ||
### A performance benefit over user Array's higher-order methods | ||
```js | ||
@@ -30,2 +32,4 @@ // This is an example of using Array higher-order methods. | ||
### Basic use of `iter` and some `standalone` functions | ||
```js | ||
@@ -46,2 +50,4 @@ // Import from the root index | ||
### Using the `range` standalone function. | ||
```js | ||
@@ -65,2 +71,4 @@ import range from 'iteragain/range'; | ||
### Iterating through any object's properties recursively. | ||
```js | ||
@@ -75,2 +83,11 @@ import iter from 'iteragain/iter'; | ||
### Supports passing functions as input and using an optional sentinel value to stop iteration | ||
```js | ||
import iter from 'iteragain/iter'; | ||
const it = iter(() => someFunction(1, 2, 3), null) | ||
// Will call `someFunction` with the arguments [1, 2, 3] for each iteration until it returns `null`: | ||
const results = it.toArray(); | ||
``` | ||
## Inpired by | ||
@@ -77,0 +94,0 @@ |
@@ -1,5 +0,18 @@ | ||
import { IteratorOrIterable, ObjectEntry } from './internal/types'; | ||
/** Converts most objects that can be an `Iterator` into one. */ | ||
export declare function toIterator<T>(arg: IteratorOrIterable<T>): Iterator<T>; | ||
export declare function toIterator(arg: Record<PropertyKey, any>): Iterator<ObjectEntry>; | ||
import ObjectIterator from './internal/ObjectIterator'; | ||
import { IteratorOrIterable } from './internal/types'; | ||
import FunctionIterator from './internal/FunctionIterator'; | ||
/** | ||
* Converts most objects that can be an `Iterator` into one. The supported inputs are: | ||
* - An `Iterator`, will return the input as is/unchanged. | ||
* - An `Iterable`, will return the input with it's `Symbol.iterator` property called. So this includes any object that | ||
* has a `Symbol.iterator` property. Like Arrays, Strings, Maps, Sets, etc. | ||
* - Any `function` with an optional `sentinel` argument, will return a `FunctionIterator`. This iterator will call the | ||
* input `func` once per `next()` call and will stop once the value of `sentinel` (default: undefined) is returned from | ||
* `func`. | ||
* - Any other non-nullable object, will return an ObjectIterator that iterates over the input object's own properties | ||
* deeply. | ||
*/ | ||
export declare function toIterator<T>(it: IteratorOrIterable<T>): Iterator<T>; | ||
export declare function toIterator<TFunc extends (...args: any[]) => any>(func: TFunc, sentinel?: ReturnType<TFunc>): FunctionIterator<TFunc>; | ||
export declare function toIterator(object: Record<PropertyKey, any>): ObjectIterator<any>; | ||
export default toIterator; |
@@ -7,12 +7,15 @@ "use strict"; | ||
const isIterator_1 = require("./isIterator"); | ||
function toIterator(arg) { | ||
if ((0, isIterator_1.default)(arg)) | ||
return arg; | ||
if ((0, isIterable_1.default)(arg)) | ||
return arg[Symbol.iterator](); | ||
if (typeof arg === 'object' && arg !== null) | ||
return new ObjectIterator_1.default(arg); | ||
throw new Error(`typeof arg '${typeof arg}' could not be coerced into an Iterator`); | ||
const FunctionIterator_1 = require("./internal/FunctionIterator"); | ||
function toIterator(...args) { | ||
if ((0, isIterator_1.default)(args[0])) | ||
return args[0]; | ||
if ((0, isIterable_1.default)(args[0])) | ||
return args[0][Symbol.iterator](); | ||
if (typeof args[0] === 'object' && args[0] !== null) | ||
return new ObjectIterator_1.default(args[0]); | ||
if (typeof args[0] === 'function') | ||
return new FunctionIterator_1.default(args[0], args[1]); | ||
throw new TypeError(`Cannot convert ${typeof args[0]} to an iterator.`); | ||
} | ||
exports.toIterator = toIterator; | ||
exports.default = toIterator; |
157601
162
3407
101