1
0
Fork 0
forked from Mirrors/gomuks

web/polyfill: make toArray work on map iterators

This commit is contained in:
Tulir Asokan 2024-12-23 11:35:23 +02:00
parent 277732efd9
commit 5fae264802

View file

@ -15,7 +15,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
if (!window.Iterator?.prototype.map) { if (!window.Iterator?.prototype.map) {
;(new Map([])).keys().__proto__.map = function(callbackFn) { const iterProto = (new Map([])).keys().__proto__
iterProto.map = function(callbackFn) {
const output = [] const output = []
let i = 0 let i = 0
for (const item of this) { for (const item of this) {
@ -24,7 +25,7 @@ if (!window.Iterator?.prototype.map) {
} }
return output return output
} }
;(new Map([])).keys().__proto__.filter = function(callbackFn) { iterProto.filter = function(callbackFn) {
const output = [] const output = []
let i = 0 let i = 0
for (const item of this) { for (const item of this) {
@ -35,6 +36,10 @@ if (!window.Iterator?.prototype.map) {
} }
return output return output
} }
const identity = x => x
iterProto.toArray = function() {
return this.map(identity)
}
Array.prototype.toArray = function() { Array.prototype.toArray = function() {
return this return this
} }