Skip to content

Commit defd61c

Browse files
committed
Improve toDecimal() to support numbers or strings that are decimals
1 parent b20a6da commit defd61c

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

index.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,16 @@ function parseQuery(mediaQuery) {
5353
});
5454
}
5555

56-
function toPx(length) {
57-
var value = parseFloat(length),
58-
units = String(length).match(RE_LENGTH_UNIT)[1];
56+
function toDecimal(ratio) {
57+
var decimal = Number(ratio),
58+
numbers;
5959

60-
switch (units) {
61-
case 'em' : return value * 16;
62-
case 'rem': return value * 16;
63-
case 'cm' : return value * 96 / 2.54;
64-
case 'mm' : return value * 96 / 2.54 / 10;
65-
case 'in' : return value * 96;
66-
case 'pt' : return value * 72;
67-
case 'pc' : return value * 72 / 12;
68-
default : return value;
60+
if (!decimal) {
61+
numbers = ratio.match(/^(\d+)\s*\/\s*(\d+)$/);
62+
decimal = numbers[1] / numbers[2];
6963
}
64+
65+
return decimal;
7066
}
7167

7268
function toDpi(resolution) {
@@ -80,7 +76,18 @@ function toDpi(resolution) {
8076
}
8177
}
8278

83-
function toDecimal(ratio) {
84-
var numbers = ratio.match(/^(\d+)\s*\/\s*(\d+)$/);
85-
return numbers[1] / numbers[2];
79+
function toPx(length) {
80+
var value = parseFloat(length),
81+
units = String(length).match(RE_LENGTH_UNIT)[1];
82+
83+
switch (units) {
84+
case 'em' : return value * 16;
85+
case 'rem': return value * 16;
86+
case 'cm' : return value * 96 / 2.54;
87+
case 'mm' : return value * 96 / 2.54 / 10;
88+
case 'in' : return value * 96;
89+
case 'pt' : return value * 72;
90+
case 'pc' : return value * 72 / 12;
91+
default : return value;
92+
}
8693
}

0 commit comments

Comments
 (0)