Skip to content

Commit cfdeef9

Browse files
committed
improve not matching
1 parent 69741bf commit cfdeef9

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
| Statements | Branches | Functions | Lines |
1515
| --------------------------------------------------------------------- | --------------------------------------------------------------------------- | -------------------------------------------------------------------- | ---------------------------------------------------------------- |
16-
| ![Statements](https://img.shields.io/badge/statements-95.68%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/branches-96.48%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/functions-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/lines-95.68%25-brightgreen.svg) |
16+
| ![Statements](https://img.shields.io/badge/statements-95.14%25-brightgreen.svg) | ![Branches](https://img.shields.io/badge/branches-96.03%25-brightgreen.svg) | ![Functions](https://img.shields.io/badge/functions-100%25-brightgreen.svg) | ![Lines](https://img.shields.io/badge/lines-95.07%25-brightgreen.svg) |
1717

1818
---
1919

lib/create-sort.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,9 @@ function _testQuery(doubleTestTrue, doubleTestFalse, singleTest) {
8989
* @return {boolean}
9090
*/
9191
return function (query) {
92-
let result;
93-
94-
if (doubleTestTrue.test(query)) result = true;
95-
else if (doubleTestFalse.test(query)) result = false;
96-
else result = singleTest.test(query);
97-
98-
/** Not keyword inverts the whole query */
99-
return query.includes('not') ? !result : result;
92+
if (doubleTestTrue.test(query)) return true;
93+
else if (doubleTestFalse.test(query)) return false;
94+
else return singleTest.test(query);
10095
};
10196
}
10297

@@ -144,6 +139,9 @@ function _testIsPrint(a, b) {
144139
module.exports = function createSort(configuration) {
145140
const config = configuration || {};
146141
const UNITLESS_MQ_ALWAYS_FIRST = config.unitlessMqAlwaysFirst;
142+
const min = (query) => isMinWidth(query) || isMinHeight(query);
143+
const max = (query) => isMaxWidth(query) || isMaxHeight(query);
144+
const not = (query) => query.includes('not');
147145

148146
/**
149147
* Sorting an array with media queries
@@ -158,11 +156,11 @@ module.exports = function createSort(configuration) {
158156
return testIsPrint;
159157
}
160158

161-
const minA = isMinWidth(a) || isMinHeight(a);
162-
const maxA = isMaxWidth(a) || isMaxHeight(a);
159+
const minA = not(a) ? max(a) : min(a);
160+
const maxA = not(a) ? min(a) : max(a);
163161

164-
const minB = isMinWidth(b) || isMinHeight(b);
165-
const maxB = isMaxWidth(b) || isMaxHeight(b);
162+
const minB = not(b) ? max(b) : min(b);
163+
const maxB = not(b) ? min(b) : max(b);
166164

167165
if (UNITLESS_MQ_ALWAYS_FIRST && ((!minA && !maxA) || (!minB && !maxB))) {
168166
if (!minA && !maxA && !minB && !maxB) {
@@ -219,11 +217,11 @@ module.exports = function createSort(configuration) {
219217
return testIsPrint;
220218
}
221219

222-
const minA = isMinWidth(a) || isMinHeight(a);
223-
const maxA = isMaxWidth(a) || isMaxHeight(a);
220+
const minA = not(a) ? max(a) : min(a);
221+
const maxA = not(a) ? min(a) : max(a);
224222

225-
const minB = isMinWidth(b) || isMinHeight(b);
226-
const maxB = isMaxWidth(b) || isMaxHeight(b);
223+
const minB = not(b) ? max(b) : min(b);
224+
const maxB = not(b) ? min(b) : max(b);
227225

228226
if (UNITLESS_MQ_ALWAYS_FIRST && ((!minA && !maxA) || (!minB && !maxB))) {
229227
if (!minA && !maxA && !minB && !maxB) {

0 commit comments

Comments
 (0)