aboutsummaryrefslogtreecommitdiffstats
path: root/src/types/SVGArray.js
blob: dafa2d4060a51a53f40c432cb0de394f13b14916 (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
import { delimiter } from '../modules/core/regex.js'

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

  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
  }

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

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

  // Flattens the array if needed
  valueOf () {
    const ret = []
    ret.push(...this)
    return ret
  }

  // Parse whitespace separated string
  parse (array = []) {
    // If already is an array, no need to parse it
    if (array instanceof Array) return array

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

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

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