Skip to content

Commit

Permalink
web/polyfill: make toArray work on map iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 23, 2024
1 parent 277732e commit 5fae264
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions web/src/util/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

if (!window.Iterator?.prototype.map) {
;(new Map([])).keys().__proto__.map = function(callbackFn) {
const iterProto = (new Map([])).keys().__proto__
iterProto.map = function(callbackFn) {
const output = []
let i = 0
for (const item of this) {
Expand All @@ -24,7 +25,7 @@ if (!window.Iterator?.prototype.map) {
}
return output
}
;(new Map([])).keys().__proto__.filter = function(callbackFn) {
iterProto.filter = function(callbackFn) {
const output = []
let i = 0
for (const item of this) {
Expand All @@ -35,6 +36,10 @@ if (!window.Iterator?.prototype.map) {
}
return output
}
const identity = x => x
iterProto.toArray = function() {
return this.map(identity)
}
Array.prototype.toArray = function() {
return this
}
Expand Down

0 comments on commit 5fae264

Please sign in to comment.