Created
November 11, 2013 18:25
-
-
Save fitzgen/7417845 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function wu(thing) { | |
| return new wu.Iterable(thing); | |
| } | |
| // This is known as @@iterator in the ES6 spec. Until it is bound to some | |
| // well-known name or importable, find the @@iterator object by expecting it | |
| // as the first property accessed on a for-of iterable. | |
| // | |
| // This shim is thanks to @andywingo via @gozala. | |
| const iteratorSymbol = (function() { | |
| try { | |
| for (let _ of Proxy.create({get: function(_, name) { throw name; } })) | |
| break; | |
| } catch (name) { | |
| return name; | |
| } | |
| throw new Error("Cannot find iterator symbol."); | |
| }()); | |
| function makeIterator(thing) { | |
| if (thing[iteratorSymbol]) { | |
| return thing[iteratorSymbol]; | |
| } | |
| if (typeof thing == "number") { | |
| return wu.range(0, thing)[iteratorSymbol]; | |
| } | |
| if (typeof thing == "object" && thing !== null) { | |
| return TODO | |
| } | |
| throw new Error("Not iterable: " + thing); | |
| } | |
| wu.Iterable = function (thing) { | |
| this[iteratorSymbol] = Function.isGenerator(thing) | |
| ? thing | |
| : makeIterator(thing); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment