From 6ea72cae2c761848b7db2c9457fd41c62d0336d6 Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Mon, 12 Nov 2018 12:00:03 +0100 Subject: make List return new lists on method calls, add map to array polyfill so that this works, fix runner --- src/types/ArrayPolyfill.js | 6 ++++++ src/types/List.js | 11 +++++++---- src/types/SVGArray.js | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) (limited to 'src/types') diff --git a/src/types/ArrayPolyfill.js b/src/types/ArrayPolyfill.js index 839a970..4d2309f 100644 --- a/src/types/ArrayPolyfill.js +++ b/src/types/ArrayPolyfill.js @@ -24,6 +24,12 @@ export const subClassArray = (function () { Arr.prototype = Object.create(baseClass.prototype) Arr.prototype.constructor = Arr + Arr.prototype.map = function (fn) { + const arr = new Arr() + arr.push.apply(arr, Array.prototype.map.call(this, fn)) + return arr + } + return Arr } } diff --git a/src/types/List.js b/src/types/List.js index 193ed05..b50a18e 100644 --- a/src/types/List.js +++ b/src/types/List.js @@ -1,7 +1,9 @@ import { extend } from '../utils/adopter.js' import { subClassArray } from './ArrayPolyfill.js' -const List = subClassArray('List', Array, function (arr) { +const List = subClassArray('List', Array, function (arr = []) { + // This catches the case, that native map tries to create an array with new Array(1) + if (typeof arr === 'number') return this this.length = 0 this.push(...arr) }) @@ -13,9 +15,10 @@ extend(List, { if (typeof fnOrMethodName === 'function') { this.forEach((el) => { fnOrMethodName.call(el, el) }) } else { - this.forEach((el) => { - el[fnOrMethodName](...args) - }) + return this.map(el => { return el[fnOrMethodName](...args) }) + // this.forEach((el) => { + // el[fnOrMethodName](...args) + // }) } return this diff --git a/src/types/SVGArray.js b/src/types/SVGArray.js index 4fcb500..7f27ec4 100644 --- a/src/types/SVGArray.js +++ b/src/types/SVGArray.js @@ -10,6 +10,8 @@ export default SVGArray extend(SVGArray, { init (arr) { + // This catches the case, that native map tries to create an array with new Array(1) + if (typeof arr === 'number') return this this.length = 0 this.push(...this.parse(arr)) return this -- cgit v1.2.3