Skip to content

Commit f98865f

Browse files
committed
Fix bugs with toPx() function
1 parent fd53767 commit f98865f

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

index.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function match(mediaQuery, values) {
1919
var RE_MEDIA_QUERY = /(?:(only|not)?\s*([^\s\(\)]+)\s*and\s*)?(.+)?/i,
2020
RE_MQ_EXPRESSION = /\(\s*([^\s\:\)]+)\s*(?:\:\s*([^\s\)]+))?\s*\)/,
2121
RE_MQ_FEATURE = /^(?:(min|max)-)?(.+)/,
22-
RE_LENGTH_VALUE = /(\d+)(em|rem|px|cm|mm|in|pt|pc)?/;
22+
RE_LENGTH_UNIT = /(em|rem|px|cm|mm|in|pt|pc)?$/;
2323

2424
function parseQuery(mediaQuery) {
2525
return mediaQuery.split(',').map(function (query) {
@@ -53,25 +53,17 @@ function parseQuery(mediaQuery) {
5353
}
5454

5555
function toPx(length) {
56-
var captures = String(length).match(RE_LENGTH_VALUE),
57-
value = Number(captures[1]),
58-
units = captures[2];
56+
var value = parseFloat(length),
57+
units = String(length).match(RE_LENGTH_UNIT)[1];
5958

6059
switch (units) {
61-
case 'em':
62-
case 'rem':
63-
return value * 16;
64-
case 'cm':
65-
return value * 96 / 2.54;
66-
case 'mm':
67-
return value * 96 / 2.54 / 10;
68-
case 'in':
69-
return value * 96;
70-
case 'pt':
71-
return value * 72;
72-
case 'pc':
73-
return value * 72 / 12;
74-
default:
75-
return value;
60+
case 'em' : return value * 16;
61+
case 'rem': return value * 16;
62+
case 'cm' : return value * 96 / 2.54;
63+
case 'mm' : return value * 96 / 2.54 / 10;
64+
case 'in' : return value * 96;
65+
case 'pt' : return value * 72;
66+
case 'pc' : return value * 72 / 12;
67+
default : return value;
7668
}
7769
}

0 commit comments

Comments
 (0)