sindresorhus / arrify

Convert a value to an array
MIT License
131 stars 28 forks source link

Invalid @@iterator method will produce an error #16

Open hwluo12 opened 2 years ago

hwluo12 commented 2 years ago

If an iterable's @@iterator method does not return an iterator object, then it is a non-well-formed iterable. Just like this:

import arrify from "arrify";

let iterable = {  
  0: "a",  
  1: "b",  
  2: "c",  
  length: 3,  
  [Symbol.iterator]: () => 1,  
};  
console.log(arrify(iterable));

You will get an error:

file:///D:/05-personal/node/nodejs-debugging/node_modules/arrify/index.js:15
                return [...value];
                           ^

TypeError: Result of the Symbol.iterator method is not an object
    at arrify (file:///D:/05-personal/node/nodejs-debugging/node_modules/arrify/index.js:15:14)
    at file:///D:/05-personal/node/nodejs-debugging/src/index.js:50:13
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)
error Command failed with exit code 1.
sindresorhus commented 2 years ago

What behavior did you expect in this case?

xiangyecunfu commented 1 year ago

将代码中的typeof value[Symbol.iterator] === 'function'改为typeof value[Symbol.iterator] === 'function' && typeof value.next === "function" 就行了。不符合标准的迭代器是没有next方法的。

hwluo12 commented 1 year ago

您好,您的邮件我已收到,稍后会回复您。