Created
August 28, 2013 23:19
-
-
Save brettz9/6372591 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
| <!DOCTYPE html> | |
| <html xmlns="http:/www.w3.org/1999/xhtml"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Iterator</title> | |
| <script src="proposal-iterator-methods.js" type="application/javascript;version=1.7"></script> | |
| </head> | |
| <body> | |
| <script> | |
| (function () {'use strict'; | |
| var obj = {'0': '0', a:3, b: 'b'}, | |
| is = '', isr = '', isrt = ''; | |
| document.write( | |
| Iterator(obj).some(function (key, val, i) { | |
| is += i; | |
| return key === val; | |
| }) + '<br >' + is + '<br >' | |
| ); | |
| document.write( | |
| Iterator(obj, true).some(function (key, i) { | |
| isrt += i; | |
| return parseInt(key, 10) === i; | |
| }) + '<br >' + isrt + '<br >' | |
| ); | |
| document.write( | |
| RegExp.iterator(/^(.*?): (.*)$/gm, 'key1: val1\nkey2: key2').some(function (n0, key, val, i) { | |
| isr += i; | |
| return key === val; | |
| }) + '<br >' + isr + '<br >' | |
| ); | |
| }()); | |
| </script> | |
| </body> | |
| </html> |
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
| /*globals Iterator, StopIteration*/ | |
| (function () {'use strict'; | |
| // Todo: Should eventually match new iterator pattern; see http://www.2ality.com/2013/06/iterators-generators.html | |
| // Todo: Add other relevant Array or Array-extra methods | |
| Iterator.prototype.some = function (cb, thisObj) { | |
| thisObj = thisObj || null; | |
| var res, next; | |
| if (!this._i) { | |
| this._i = 0; | |
| } | |
| try { | |
| next = this.next(); | |
| } catch(e) { | |
| if (e instanceof StopIteration) { | |
| return false; | |
| } | |
| throw e; | |
| } | |
| next = Array.isArray(next) ? next : [next]; | |
| res = cb.apply(thisObj, next.concat(this._i)); | |
| this._i++; | |
| if (res) { | |
| return true; | |
| } | |
| return this.some(cb, thisObj); | |
| }; | |
| function _mixinRegex (regex, newFlags, newLastIndex) { | |
| newLastIndex = newLastIndex === undefined ? regex.lastIndex : newLastIndex; | |
| newFlags = newFlags || ''; | |
| regex = new RegExp( | |
| regex.source, | |
| (newFlags.indexOf('g') > -1 ? 'g' : regex.global ? 'g' : '') + | |
| (newFlags.indexOf('i') > -1 ? 'i' : regex.ignoreCase ? 'i' : '') + | |
| (newFlags.indexOf('m') > -1 ? 'm' : regex.multiline ? 'm' : '') + | |
| (newFlags.indexOf('y') > -1 ? 'y' : regex.sticky ? 'y' : '') // Non-standard but harmless if already being used | |
| ); | |
| regex.lastIndex = newLastIndex; | |
| return regex; | |
| } | |
| function RegExpIterator (pattern, string) { | |
| this.pattern = _mixinRegex(pattern, 'g'); | |
| this.string = string; | |
| } | |
| RegExpIterator.prototype.some = Iterator.prototype.some; | |
| RegExpIterator.prototype.next = function () { | |
| var matches = this.pattern.exec(this.string); | |
| if (matches !== null) { | |
| return matches; | |
| } | |
| throw StopIteration; | |
| }; | |
| RegExpIterator.prototype['__iterator__'] = function (keysOnly) { | |
| return this; | |
| }; | |
| RegExp.iterator = function (pattern, string) { | |
| return new RegExpIterator(pattern, string); | |
| }; | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment