diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 12:00:03 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 13:01:01 +0100 |
commit | 6ea72cae2c761848b7db2c9457fd41c62d0336d6 (patch) | |
tree | 4d62e3f49a8e4922ed520739e4ab9b42b67e9e97 /src/types | |
parent | c108c060152add00cac72a4911f6e998ffb4eb83 (diff) | |
download | svg.js-6ea72cae2c761848b7db2c9457fd41c62d0336d6.tar.gz svg.js-6ea72cae2c761848b7db2c9457fd41c62d0336d6.zip |
make List return new lists on method calls, add map to array polyfill so that this works, fix runner
Diffstat (limited to 'src/types')
-rw-r--r-- | src/types/ArrayPolyfill.js | 6 | ||||
-rw-r--r-- | src/types/List.js | 11 | ||||
-rw-r--r-- | src/types/SVGArray.js | 2 |
3 files changed, 15 insertions, 4 deletions
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 |