convertUnit.js
2.54 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
/**
* @type {{[key:string]: {[key:string]: number}}}
*/
const conversions = {
// Absolute length units
px: {
px: 1,
cm: 96 / 2.54,
mm: 96 / 25.4,
q: 96 / 101.6,
in: 96,
pt: 96 / 72,
pc: 16,
},
cm: {
px: 2.54 / 96,
cm: 1,
mm: 0.1,
q: 0.025,
in: 2.54,
pt: 2.54 / 72,
pc: 2.54 / 6,
},
mm: {
px: 25.4 / 96,
cm: 10,
mm: 1,
q: 0.25,
in: 25.4,
pt: 25.4 / 72,
pc: 25.4 / 6,
},
q: {
px: 101.6 / 96,
cm: 40,
mm: 4,
q: 1,
in: 101.6,
pt: 101.6 / 72,
pc: 101.6 / 6,
},
in: {
px: 1 / 96,
cm: 1 / 2.54,
mm: 1 / 25.4,
q: 1 / 101.6,
in: 1,
pt: 1 / 72,
pc: 1 / 6,
},
pt: {
px: 0.75,
cm: 72 / 2.54,
mm: 72 / 25.4,
q: 72 / 101.6,
in: 72,
pt: 1,
pc: 12,
},
pc: {
px: 0.0625,
cm: 6 / 2.54,
mm: 6 / 25.4,
q: 6 / 101.6,
in: 6,
pt: 6 / 72,
pc: 1,
},
// Angle units
deg: {
deg: 1,
grad: 0.9,
rad: 180 / Math.PI,
turn: 360,
},
grad: {
deg: 400 / 360,
grad: 1,
rad: 200 / Math.PI,
turn: 400,
},
rad: {
deg: Math.PI / 180,
grad: Math.PI / 200,
rad: 1,
turn: Math.PI * 2,
},
turn: {
deg: 1 / 360,
grad: 0.0025,
rad: 0.5 / Math.PI,
turn: 1,
},
// Duration units
s: {
s: 1,
ms: 0.001,
},
ms: {
s: 1000,
ms: 1,
},
// Frequency units
hz: {
hz: 1,
khz: 1000,
},
khz: {
hz: 0.001,
khz: 1,
},
// Resolution units
dpi: {
dpi: 1,
dpcm: 1 / 2.54,
dppx: 1 / 96,
},
dpcm: {
dpi: 2.54,
dpcm: 1,
dppx: 2.54 / 96,
},
dppx: {
dpi: 96,
dpcm: 96 / 2.54,
dppx: 1,
},
};
/**
* @param {number} value
* @param {string} sourceUnit
* @param {string} targetUnit
* @param {number|false} precision
*/
function convertUnit(value, sourceUnit, targetUnit, precision) {
const sourceUnitNormalized = sourceUnit.toLowerCase();
const targetUnitNormalized = targetUnit.toLowerCase();
if (!conversions[targetUnitNormalized]) {
throw new Error('Cannot convert to ' + targetUnit);
}
if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
throw new Error('Cannot convert from ' + sourceUnit + ' to ' + targetUnit);
}
const converted =
conversions[targetUnitNormalized][sourceUnitNormalized] * value;
if (precision !== false) {
precision = Math.pow(10, Math.ceil(precision) || 5);
return Math.round(converted * precision) / precision;
}
return converted;
}
module.exports = convertUnit;