Skip to content

Commit cabfd1f

Browse files
committed
Updated logic for parsing mq with >=, <=, >, <
1 parent afc3c91 commit cabfd1f

File tree

1 file changed

+95
-12
lines changed

1 file changed

+95
-12
lines changed

index.js

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
const pkg = require("./package.json");
4-
const MIN_MAX_FEATURES = ["width", "height"];
4+
const SIZE_FEATURES = ["width", "height"];
55

66
const inspectLength = function (length) {
77
let num;
@@ -58,13 +58,21 @@ const parseQueryList = function (queryList, list) {
5858
return;
5959
}
6060

61-
expression = list.split(
62-
expression.replace(/^\(|\)$/g, ""),
63-
[":"]
64-
);
61+
// Remove surrounding parentheses
62+
const cleanedExpression = expression.replace(/^\(|\)$/g, "");
6563

66-
feature = expression[0];
67-
value = expression[1];
64+
// Check for range syntax first (>=, <=, >, <)
65+
const rangeMatch = cleanedExpression.match(/^(\w+)\s*(>=|<=|>|<)\s*(.+)$/);
66+
if (rangeMatch) {
67+
const [, property, operator, val] = rangeMatch;
68+
69+
feature = `${property} ${operator}`;
70+
value = val.trim();
71+
} else {
72+
expression = list.split(cleanedExpression, [":"]);
73+
feature = expression[0];
74+
value = expression[1];
75+
}
6876

6977
if (!expressions[feature]) {
7078
expressions[feature] = [];
@@ -84,10 +92,15 @@ const optimizeAtRuleParams = function (params, list) {
8492

8593
return mapAtRuleParams
8694
.map(function (mqExpressions) {
87-
MIN_MAX_FEATURES.forEach(function (prop) {
95+
SIZE_FEATURES.forEach(function (prop) {
8896
const minProp = "min-" + prop;
8997
const maxProp = "max-" + prop;
98+
const rangeMinProp = prop + " >=";
99+
const rangeMaxProp = prop + " <=";
100+
const rangeMinStrictProp = prop + " >";
101+
const rangeMaxStrictProp = prop + " <";
90102

103+
// Handle range syntax min-* properties
91104
if (mqExpressions.hasOwnProperty(minProp)) {
92105
mqExpressions[minProp] = mqExpressions[minProp].reduce(
93106
function (a, b) {
@@ -96,20 +109,85 @@ const optimizeAtRuleParams = function (params, list) {
96109
);
97110
}
98111

112+
// Handle range syntax max-* properties
99113
if (mqExpressions.hasOwnProperty(maxProp)) {
100114
mqExpressions[maxProp] = mqExpressions[maxProp].reduce(
101115
function (a, b) {
102116
return inspectLength(a) < inspectLength(b) ? a : b;
103117
}
104118
);
105119
}
120+
121+
// Handle range syntax >= properties
122+
if (mqExpressions.hasOwnProperty(rangeMinProp)) {
123+
mqExpressions[rangeMinProp] = mqExpressions[rangeMinProp].reduce(
124+
function (a, b) {
125+
return inspectLength(a) > inspectLength(b) ? a : b;
126+
}
127+
);
128+
}
129+
130+
// Handle range syntax <= properties
131+
if (mqExpressions.hasOwnProperty(rangeMaxProp)) {
132+
mqExpressions[rangeMaxProp] = mqExpressions[rangeMaxProp].reduce(
133+
function (a, b) {
134+
return inspectLength(a) < inspectLength(b) ? a : b;
135+
}
136+
);
137+
}
138+
139+
// Handle range syntax > properties
140+
if (mqExpressions.hasOwnProperty(rangeMinStrictProp)) {
141+
mqExpressions[rangeMinStrictProp] = mqExpressions[rangeMinStrictProp].reduce(
142+
function (a, b) {
143+
return inspectLength(a) > inspectLength(b) ? a : b;
144+
}
145+
);
146+
}
147+
148+
// Handle range syntax < properties
149+
if (mqExpressions.hasOwnProperty(rangeMaxStrictProp)) {
150+
mqExpressions[rangeMaxStrictProp] = mqExpressions[rangeMaxStrictProp].reduce(
151+
function (a, b) {
152+
return inspectLength(a) < inspectLength(b) ? a : b;
153+
}
154+
);
155+
}
106156
});
107157
return mqExpressions;
108158
})
109159
.filter(function (e) {
110-
return !!e["min-width"] && !!e["max-width"]
111-
? inspectLength(e["min-width"]) <= inspectLength(e["max-width"])
112-
: true;
160+
if (!!e["min-width"] && !!e["max-width"]) {
161+
if (inspectLength(e["min-width"]) > inspectLength(e["max-width"])) {
162+
return false;
163+
}
164+
}
165+
166+
if (!!e["width >="] && !!e["width <="]) {
167+
if (inspectLength(e["width >="]) > inspectLength(e["width <="])) {
168+
return false;
169+
}
170+
}
171+
172+
if (!!e["width >"] && !!e["width <"]) {
173+
if (inspectLength(e["width >"]) >= inspectLength(e["width <"])) {
174+
return false;
175+
}
176+
}
177+
178+
if (!!e["height >="] && !!e["height <="]) {
179+
if (inspectLength(e["height >="]) > inspectLength(e["height <="])) {
180+
return false;
181+
}
182+
}
183+
184+
if (!!e["height >"] && !!e["height <"]) {
185+
if (inspectLength(e["height >"]) >= inspectLength(e["height <"])) {
186+
return false;
187+
}
188+
}
189+
190+
return true;
113191
})
114192
.map(function (e) {
115193
const array = [];
@@ -122,7 +200,12 @@ const optimizeAtRuleParams = function (params, list) {
122200
} else {
123201
switch (typeof e[prop]) {
124202
case "string":
125-
array.push("(" + prop + ": " + e[prop] + ")");
203+
if (prop.includes(" >=") || prop.includes(" <=") ||
204+
prop.includes(" >") || prop.includes(" <")) {
205+
array.push("(" + prop + " " + e[prop] + ")");
206+
} else {
207+
array.push("(" + prop + ": " + e[prop] + ")");
208+
}
126209
break;
127210
case "object":
128211
// Handle unrecognized properties.

0 commit comments

Comments
 (0)