source: trip-planner-front/node_modules/postcss-calc/dist/lib/convertUnit.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7const conversions = {
8 // Absolute length units
9 'px': {
10 'px': 1,
11 'cm': 96 / 2.54,
12 'mm': 96 / 25.4,
13 'q': 96 / 101.6,
14 'in': 96,
15 'pt': 96 / 72,
16 'pc': 16
17 },
18 'cm': {
19 'px': 2.54 / 96,
20 'cm': 1,
21 'mm': 0.1,
22 'q': 0.025,
23 'in': 2.54,
24 'pt': 2.54 / 72,
25 'pc': 2.54 / 6
26 },
27 'mm': {
28 'px': 25.4 / 96,
29 'cm': 10,
30 'mm': 1,
31 'q': 0.25,
32 'in': 25.4,
33 'pt': 25.4 / 72,
34 'pc': 25.4 / 6
35 },
36 'q': {
37 'px': 101.6 / 96,
38 'cm': 40,
39 'mm': 4,
40 'q': 1,
41 'in': 101.6,
42 'pt': 101.6 / 72,
43 'pc': 101.6 / 6
44 },
45 'in': {
46 'px': 1 / 96,
47 'cm': 1 / 2.54,
48 'mm': 1 / 25.4,
49 'q': 1 / 101.6,
50 'in': 1,
51 'pt': 1 / 72,
52 'pc': 1 / 6
53 },
54 'pt': {
55 'px': 0.75,
56 'cm': 72 / 2.54,
57 'mm': 72 / 25.4,
58 'q': 72 / 101.6,
59 'in': 72,
60 'pt': 1,
61 'pc': 12
62 },
63 'pc': {
64 'px': 0.0625,
65 'cm': 6 / 2.54,
66 'mm': 6 / 25.4,
67 'q': 6 / 101.6,
68 'in': 6,
69 'pt': 6 / 72,
70 'pc': 1
71 },
72 // Angle units
73 'deg': {
74 'deg': 1,
75 'grad': 0.9,
76 'rad': 180 / Math.PI,
77 'turn': 360
78 },
79 'grad': {
80 'deg': 400 / 360,
81 'grad': 1,
82 'rad': 200 / Math.PI,
83 'turn': 400
84 },
85 'rad': {
86 'deg': Math.PI / 180,
87 'grad': Math.PI / 200,
88 'rad': 1,
89 'turn': Math.PI * 2
90 },
91 'turn': {
92 'deg': 1 / 360,
93 'grad': 0.0025,
94 'rad': 0.5 / Math.PI,
95 'turn': 1
96 },
97 // Duration units
98 's': {
99 's': 1,
100 'ms': 0.001
101 },
102 'ms': {
103 's': 1000,
104 'ms': 1
105 },
106 // Frequency units
107 'hz': {
108 'hz': 1,
109 'khz': 1000
110 },
111 'khz': {
112 'hz': 0.001,
113 'khz': 1
114 },
115 // Resolution units
116 'dpi': {
117 'dpi': 1,
118 'dpcm': 1 / 2.54,
119 'dppx': 1 / 96
120 },
121 'dpcm': {
122 'dpi': 2.54,
123 'dpcm': 1,
124 'dppx': 2.54 / 96
125 },
126 'dppx': {
127 'dpi': 96,
128 'dpcm': 96 / 2.54,
129 'dppx': 1
130 }
131};
132
133function convertUnit(value, sourceUnit, targetUnit, precision) {
134 const sourceUnitNormalized = sourceUnit.toLowerCase();
135 const targetUnitNormalized = targetUnit.toLowerCase();
136
137 if (!conversions[targetUnitNormalized]) {
138 throw new Error("Cannot convert to " + targetUnit);
139 }
140
141 if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
142 throw new Error("Cannot convert from " + sourceUnit + " to " + targetUnit);
143 }
144
145 const converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
146
147 if (precision !== false) {
148 precision = Math.pow(10, parseInt(precision) || 5);
149 return Math.round(converted * precision) / precision;
150 }
151
152 return converted;
153}
154
155var _default = convertUnit;
156exports.default = _default;
157module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.