stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

`assert.isIteratorLike` is too restrictive

Open lightmare opened this issue 3 years ago • 1 comments

Description

Currently the function looks like this:

function isIteratorLike( value ) {
	return (
		value !== null &&
		typeof value === 'object' && // too narrow
		isFunction( value.next ) &&
		value.next.length === 0 // too narrow
	);
}

ES spec requires that Type(iterator) is Object. Which includes functions.

// Type(x) is Object
typeof x === "object" ? x !== null : typeof x === "function"

Then ES spec puts no restriction on next.length. Iterators created by calling generator functions have next.length === 1 because they accept an argument that becomes the result of evaluating yield.

Related Issues

Related issues # , # , and # .

Questions

No.

Demo

No response

Reproduction

var isIteratorLike = require('@stdlib/assert/is-iterator-like')

function next(arg) { return { value: ++this.v, done: this.v > 10 }}
function itor() { return Object.assign(_=>{}, { v: 0, next }) }

console.log(isIteratorLike(itor()))

Expected Results

true

That this should print true can be confirmed by checking that built-ins treat the function returned by itor() as valid iterator:

Array.from({[Symbol.iterator]: itor})

Actual Results

false

Version

@stdlib/assert 0.0.12

Environments

Node.js

Browser Version

No response

Node.js / npm Version

17.2.0

Platform

No response

Checklist

  • [X] Read and understood the Code of Conduct.
  • [X] Searched for existing issues and pull requests.

lightmare avatar Dec 18 '21 15:12 lightmare

@lightmare Thanks for filing this issue.

I pushed https://github.com/stdlib-js/stdlib/commit/f7ee9af36ded6f62728bbf032e6adc33a89031d2 which relaxes the requirements to (1) allow value to be a function and (2) to no longer check that next is a nullary function.

I believe that should resolve this issue.

kgryte avatar Dec 18 '21 20:12 kgryte

With the most recent @stdlib/assert release, this should be resolved.

kgryte avatar Nov 11 '23 10:11 kgryte