diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 13:29:15 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 13:29:15 +0100 |
commit | b9f5c216c6eb75f3a00c6b121da5a72885286fa0 (patch) | |
tree | f7af03f1119dc0bb852a7ac98702d709ba87e403 | |
parent | 7f67bc4ffa044607925246d846f544c87d5e00fa (diff) | |
download | svg.js-b9f5c216c6eb75f3a00c6b121da5a72885286fa0.tar.gz svg.js-b9f5c216c6eb75f3a00c6b121da5a72885286fa0.zip |
ticking off the last checkbox of (#645). return List whenever possible
-rw-r--r-- | dist/svg.js | 164 | ||||
-rw-r--r-- | src/elements/Dom.js | 7 | ||||
-rw-r--r-- | src/modules/core/selector.js | 7 | ||||
-rw-r--r-- | src/types/List.js | 3 |
4 files changed, 89 insertions, 92 deletions
diff --git a/dist/svg.js b/dist/svg.js index d7dfcd1..8670803 100644 --- a/dist/svg.js +++ b/dist/svg.js @@ -6,7 +6,7 @@ * @copyright Wout Fierens <wout@mick-wout.com> * @license MIT * -* BUILT: Mon Nov 12 2018 13:00:40 GMT+0100 (GMT+01:00) +* BUILT: Mon Nov 12 2018 13:26:51 GMT+0100 (GMT+01:00) */; var SVG = (function () { 'use strict'; @@ -1172,6 +1172,82 @@ var SVG = (function () { return EventTarget; }(Base); + /* eslint no-new-func: "off" */ + var subClassArray = function () { + try { + // try es6 subclassing + return Function('name', 'baseClass', '_constructor', ['baseClass = baseClass || Array', 'return {', ' [name]: class extends baseClass {', ' constructor (...args) {', ' super(...args)', ' _constructor && _constructor.apply(this, args)', ' }', ' }', '}[name]'].join('\n')); + } catch (e) { + // Use es5 approach + return function (name) { + var baseClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Array; + + var _constructor = arguments.length > 2 ? arguments[2] : undefined; + + var Arr = function Arr() { + baseClass.apply(this, arguments); + _constructor && _constructor.apply(this, arguments); + }; + + Arr.prototype = Object.create(baseClass.prototype); + Arr.prototype.constructor = Arr; + + Arr.prototype.map = function (fn) { + var arr = new Arr(); + arr.push.apply(arr, Array.prototype.map.call(this, fn)); + return arr; + }; + + return Arr; + }; + } + }(); + + var List = subClassArray('List', Array, function () { + var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; + // 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.apply(this, _toConsumableArray(arr)); + }); + extend(List, { + each: function each(fnOrMethodName) { + for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + args[_key - 1] = arguments[_key]; + } + + if (typeof fnOrMethodName === 'function') { + this.forEach(function (el) { + fnOrMethodName.call(el, el); + }); + } else { + return this.map(function (el) { + return el[fnOrMethodName].apply(el, args); + }); + } + + return this; + }, + toArray: function toArray() { + return Array.prototype.concat.apply([], this); + } + }); + + List.extend = function (methods) { + methods = methods.reduce(function (obj, name) { + obj[name] = function () { + for (var _len2 = arguments.length, attrs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + attrs[_key2] = arguments[_key2]; + } + + return this.each.apply(this, [name].concat(attrs)); + }; + + return obj; + }, {}); + extend(List, methods); + }; + function noop() {} // Default animation values var timeline = { @@ -1218,37 +1294,6 @@ var SVG = (function () { attrs: attrs }); - /* eslint no-new-func: "off" */ - var subClassArray = function () { - try { - // try es6 subclassing - return Function('name', 'baseClass', '_constructor', ['baseClass = baseClass || Array', 'return {', ' [name]: class extends baseClass {', ' constructor (...args) {', ' super(...args)', ' _constructor && _constructor.apply(this, args)', ' }', ' }', '}[name]'].join('\n')); - } catch (e) { - // Use es5 approach - return function (name) { - var baseClass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Array; - - var _constructor = arguments.length > 2 ? arguments[2] : undefined; - - var Arr = function Arr() { - baseClass.apply(this, arguments); - _constructor && _constructor.apply(this, arguments); - }; - - Arr.prototype = Object.create(baseClass.prototype); - Arr.prototype.constructor = Arr; - - Arr.prototype.map = function (fn) { - var arr = new Arr(); - arr.push.apply(arr, Array.prototype.map.call(this, fn)); - return arr; - }; - - return Arr; - }; - } - }(); - var SVGArray = subClassArray('SVGArray', Array, function (arr) { this.init(arr); }); @@ -1524,9 +1569,9 @@ var SVG = (function () { }, { key: "children", value: function children() { - return map(this.node.children, function (node) { + return new List(map(this.node.children, function (node) { return adopt(node); - }); + })); } // Remove all elements in this container }, { @@ -3303,9 +3348,9 @@ var SVG = (function () { register(Stop); function baseFind(query, parent) { - return map((parent || globals.document).querySelectorAll(query), function (node) { + return new List(map((parent || globals.document).querySelectorAll(query), function (node) { return adopt(node); - }); + })); } // Scoped find method function find(query) { @@ -3780,53 +3825,6 @@ var SVG = (function () { }); register(Line); - var List = subClassArray('List', Array, function () { - var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; - // 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.apply(this, _toConsumableArray(arr)); - }); - extend(List, { - each: function each(fnOrMethodName) { - for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { - args[_key - 1] = arguments[_key]; - } - - if (typeof fnOrMethodName === 'function') { - this.forEach(function (el) { - fnOrMethodName.call(el, el); - }); - } else { - return this.map(function (el) { - return el[fnOrMethodName].apply(el, args); - }); // this.forEach((el) => { - // el[fnOrMethodName](...args) - // }) - } - - return this; - }, - toArray: function toArray() { - return Array.prototype.concat.apply([], this); - } - }); - - List.extend = function (methods) { - methods = methods.reduce(function (obj, name) { - obj[name] = function () { - for (var _len2 = arguments.length, attrs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { - attrs[_key2] = arguments[_key2]; - } - - return this.each.apply(this, [name].concat(attrs)); - }; - - return obj; - }, {}); - extend(List, methods); - }; - var Marker = /*#__PURE__*/ function (_Container) { diff --git a/src/elements/Dom.js b/src/elements/Dom.js index 192b9bd..6d35f1e 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -6,10 +6,11 @@ import { makeInstance, register } from '../utils/adopter.js' +import { globals } from '../utils/window.js' import { map } from '../utils/utils.js' import { ns } from '../modules/core/namespaces.js' -import { globals } from '../utils/window.js' import EventTarget from '../types/EventTarget.js' +import List from '../types/List.js' import attr from '../modules/core/attr.js' export default class Dom extends EventTarget { @@ -43,9 +44,9 @@ export default class Dom extends EventTarget { // Returns all child elements children () { - return map(this.node.children, function (node) { + return new List(map(this.node.children, function (node) { return adopt(node) - }) + })) } // Remove all elements in this container diff --git a/src/modules/core/selector.js b/src/modules/core/selector.js index f2a7c58..83a919f 100644 --- a/src/modules/core/selector.js +++ b/src/modules/core/selector.js @@ -1,12 +1,13 @@ import { adopt } from '../../utils/adopter.js' +import { globals } from '../../utils/window.js' import { map } from '../../utils/utils.js' import { registerMethods } from '../../utils/methods.js' -import { globals } from '../../utils/window.js' +import List from '../../types/List.js' export default function baseFind (query, parent) { - return map((parent || globals.document).querySelectorAll(query), function (node) { + return new List(map((parent || globals.document).querySelectorAll(query), function (node) { return adopt(node) - }) + })) } // Scoped find method diff --git a/src/types/List.js b/src/types/List.js index b50a18e..8bd3985 100644 --- a/src/types/List.js +++ b/src/types/List.js @@ -16,9 +16,6 @@ extend(List, { this.forEach((el) => { fnOrMethodName.call(el, el) }) } else { return this.map(el => { return el[fnOrMethodName](...args) }) - // this.forEach((el) => { - // el[fnOrMethodName](...args) - // }) } return this |