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