Skip to content

assert.isIteratorLike is too restrictive #471

@lightmare

Description

@lightmare

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions