convertUnit.js 2.7 KB

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