This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No more guarantees. Ten times faster.
- Loading branch information
Showing
4 changed files
with
58 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
module.exports = iterate; | ||
function iterate(iterable) { | ||
if (!iterable) { | ||
return new ArrayIterator([]); | ||
} else if (Array.isArray(iterable)) { | ||
return new ArrayIterator(iterable); | ||
} else if (typeof iterable.next === "function") { | ||
return iterable; | ||
} else if (typeof iterable.iterate === "function") { | ||
return iterable.iterate(); | ||
} else { | ||
throw new TypeError("Can't iterate " + iterable); | ||
} | ||
} | ||
|
||
function ArrayIterator(array) { | ||
this.array = array; | ||
this.index = 0; | ||
} | ||
|
||
ArrayIterator.prototype.next = function () { | ||
var iteration; | ||
if (this.index < this.array.length) { | ||
iteration = new Iteration(this.array[this.index++], false); | ||
} else { | ||
iteration = new Iteration(undefined, true); | ||
} | ||
return iteration; | ||
}; | ||
|
||
function Iteration(value, done) { | ||
this.value = value; | ||
this.done = done; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters