/demos/animate/

vg.js.git
The lightweight library for manipulating and animating SVG: https://github.com/svgdotjs/svg.jswww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/List.js
blob: 193ed05b1cd09794d9d2b158def09ae5d28181f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { extend } from '../utils/adopter.js'
import { subClassArray } from './ArrayPolyfill.js'

const List = subClassArray('List', Array, function (arr) {
  this.length = 0
  this.push(...arr)
})

export default List

extend(List, {
  each (fnOrMethodName, ...args) {
    if (typeof fnOrMethodName === 'function') {
      this.forEach((el) => { fnOrMethodName.call(el, el) })
    } else {
      this.forEach((el) => {
        el[fnOrMethodName](...args)
      })
    }

    return this
  },

  toArray () {
    return Array.prototype.concat.apply([], this)
  }
})

List.extend = function (methods) {
  methods = methods.reduce((obj, name) => {
    obj[name] = function (...attrs) {
      return this.each(name, ...attrs)
    }
    return obj
  }, {})

  extend(List, methods)
}