summaryrefslogtreecommitdiffstats
path: root/src/SVGArray.js
blob: 9789dfead0ffbe6ca042f38ddbee93927a773829 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* global arrayClone */
import {delimiter} from './regex.js'
import ArrayPolyfill from './ArrayPolyfill.js'

let BaseArray = (function() {
  try {
    let b = class extends Array {}
    return Array
  } catch (e) {
    return ArrayPolyfill
  }
})()

export default class SVGArray extends BaseArray {
  constructor (...args) {
    super()
    this.init(...args)
  }

  init (array, fallback) {
    //this.splice(0, this.length)
    this.length = 0
    this.push(...this.parse(array || fallback))
  }

  toArray () {
    return Array.prototype.slice(this)
  }

  toString () {
    this.join(' ')
  }

  valueOf () {
    return this.toArray()
  }

  // Parse whitespace separated string
  parse (array) {
    array = array.valueOf()

    // if already is an array, no need to parse it
    if (Array.isArray(array)) return array

    return array.trim().split(delimiter).map(parseFloat)
  }

  clone () {
    return new this.constructor(this)
  }

  toSet () {
    return new Set(this)
  }
}