blob: 5945341bdd8f7c9f75c882e63f1035627ff94119 (
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import { numberAndUnit } from '../modules/core/regex.js'
// Module for unit conversions
export default class SVGNumber {
// Initialize
constructor(...args) {
this.init(...args)
}
convert(unit) {
return new SVGNumber(this.value, unit)
}
// Divide number
divide(number) {
number = new SVGNumber(number)
return new SVGNumber(this / number, this.unit || number.unit)
}
init(value, unit) {
unit = Array.isArray(value) ? value[1] : unit
value = Array.isArray(value) ? value[0] : value
// initialize defaults
this.value = 0
this.unit = unit || ''
// parse value
if (typeof value === 'number') {
// ensure a valid numeric value
this.value = isNaN(value)
? 0
: !isFinite(value)
? value < 0
? -3.4e38
: +3.4e38
: value
} else if (typeof value === 'string') {
unit = value.match(numberAndUnit)
if (unit) {
// make value numeric
this.value = parseFloat(unit[1])
// normalize
if (unit[5] === '%') {
this.value /= 100
} else if (unit[5] === 's') {
this.value *= 1000
}
// store unit
this.unit = unit[5]
}
} else {
if (value instanceof SVGNumber) {
this.value = value.valueOf()
this.unit = value.unit
}
}
return this
}
// Subtract number
minus(number) {
number = new SVGNumber(number)
return new SVGNumber(this - number, this.unit || number.unit)
}
// Add number
plus(number) {
number = new SVGNumber(number)
return new SVGNumber(this + number, this.unit || number.unit)
}
// Multiply number
times(number) {
number = new SVGNumber(number)
return new SVGNumber(this * number, this.unit || number.unit)
}
toArray() {
return [this.value, this.unit]
}
toJSON() {
return this.toString()
}
toString() {
return (
(this.unit === '%'
? ~~(this.value * 1e8) / 1e6
: this.unit === 's'
? this.value / 1e3
: this.value) + this.unit
)
}
valueOf() {
return this.value
}
}
|