1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = _default;
|
---|
7 | const lengthConv = {
|
---|
8 | in: 96,
|
---|
9 | px: 1,
|
---|
10 | pt: 4 / 3,
|
---|
11 | pc: 16
|
---|
12 | };
|
---|
13 | const timeConv = {
|
---|
14 | s: 1000,
|
---|
15 | ms: 1
|
---|
16 | };
|
---|
17 | const angleConv = {
|
---|
18 | turn: 360,
|
---|
19 | deg: 1
|
---|
20 | };
|
---|
21 |
|
---|
22 | function 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 |
|
---|
38 | function 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 |
|
---|
58 | function _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 |
|
---|
85 | module.exports = exports.default; |
---|