source: trip-planner-front/node_modules/postcss-convert-values/dist/lib/convert.js@ 1ad8e64

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

initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7const lengthConv = {
8 in: 96,
9 px: 1,
10 pt: 4 / 3,
11 pc: 16
12};
13const timeConv = {
14 s: 1000,
15 ms: 1
16};
17const angleConv = {
18 turn: 360,
19 deg: 1
20};
21
22function dropLeadingZero(number) {
23 const value = String(number);
24
25 if (number % 1) {
26 if (value[0] === '0') {
27 return value.slice(1);
28 }
29
30 if (value[0] === '-' && value[1] === '0') {
31 return '-' + value.slice(2);
32 }
33 }
34
35 return value;
36}
37
38function transform(number, unit, conversion) {
39 const lowerCasedUnit = unit.toLowerCase();
40 let one, base;
41 let convertionUnits = Object.keys(conversion).filter(u => {
42 if (conversion[u] === 1) {
43 one = u;
44 }
45
46 return lowerCasedUnit !== u;
47 });
48
49 if (lowerCasedUnit === one) {
50 base = number / conversion[lowerCasedUnit];
51 } else {
52 base = number * conversion[lowerCasedUnit];
53 }
54
55 return convertionUnits.map(u => dropLeadingZero(base / conversion[u]) + u).reduce((a, b) => a.length < b.length ? a : b);
56}
57
58function _default(number, unit, {
59 time,
60 length,
61 angle
62}) {
63 let value = dropLeadingZero(number) + (unit ? unit : '');
64 let converted;
65
66 if (length !== false && unit.toLowerCase() in lengthConv) {
67 converted = transform(number, unit, lengthConv);
68 }
69
70 if (time !== false && unit.toLowerCase() in timeConv) {
71 converted = transform(number, unit, timeConv);
72 }
73
74 if (angle !== false && unit.toLowerCase() in angleConv) {
75 converted = transform(number, unit, angleConv);
76 }
77
78 if (converted && converted.length < value.length) {
79 value = converted;
80 }
81
82 return value;
83}
84
85module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.