Skip to content

Commit 57a09d5

Browse files
author
dutchenkoOleg
committed
bump 1.4.0
1 parent 53fb380 commit 57a09d5

File tree

3 files changed

+72
-35
lines changed

3 files changed

+72
-35
lines changed

index.js

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,31 @@
88
*
99
* @module sort-css-media-queries
1010
* @author Oleg Dutchenko <dutchenko.o.wezom@gmail.com>
11-
* @version 1.1.1
11+
* @version 1.4.0
1212
*/
1313

1414
// ----------------------------------------
1515
// Private
1616
// ----------------------------------------
1717

18-
const minMaxWidth = /(!?\(\s*min(-device-)?-width).+\(\s*max(-device)?-width/
19-
const minWidth = /\(\s*min(-device)?-width/
20-
const maxMinWidth = /(!?\(\s*max(-device)?-width).+\(\s*min(-device)?-width/
21-
const maxWidth = /\(\s*max(-device)?-width/
18+
const minMaxWidth = /(!?\(\s*min(-device-)?-width).+\(\s*max(-device)?-width/i
19+
const minWidth = /\(\s*min(-device)?-width/i
20+
const maxMinWidth = /(!?\(\s*max(-device)?-width).+\(\s*min(-device)?-width/i
21+
const maxWidth = /\(\s*max(-device)?-width/i
2222

23-
const isMinWidth = testQuery(minMaxWidth, maxMinWidth, minWidth)
24-
const isMaxWidth = testQuery(maxMinWidth, minMaxWidth, maxWidth)
23+
const isMinWidth = _testQuery(minMaxWidth, maxMinWidth, minWidth)
24+
const isMaxWidth = _testQuery(maxMinWidth, minMaxWidth, maxWidth)
2525

26-
const minMaxHeight = /(!?\(\s*min(-device)?-height).+\(\s*max(-device)?-height/
27-
const minHeight = /\(\s*min(-device)?-height/
28-
const maxMinHeight = /(!?\(\s*max(-device)?-height).+\(\s*min(-device)?-height/
29-
const maxHeight = /\(\s*max(-device)?-height/
26+
const minMaxHeight = /(!?\(\s*min(-device)?-height).+\(\s*max(-device)?-height/i
27+
const minHeight = /\(\s*min(-device)?-height/i
28+
const maxMinHeight = /(!?\(\s*max(-device)?-height).+\(\s*min(-device)?-height/i
29+
const maxHeight = /\(\s*max(-device)?-height/i
3030

31-
const isMinHeight = testQuery(minMaxHeight, maxMinHeight, minHeight)
32-
const isMaxHeight = testQuery(maxMinHeight, minMaxHeight, maxHeight)
31+
const isMinHeight = _testQuery(minMaxHeight, maxMinHeight, minHeight)
32+
const isMaxHeight = _testQuery(maxMinHeight, minMaxHeight, maxHeight)
33+
34+
const isPrint = /print/i
35+
const isPrintOnly = /^print$/i
3336

3437
const maxValue = Number.MAX_VALUE
3538

@@ -41,15 +44,15 @@ const maxValue = Number.MAX_VALUE
4144
* @param {string} length
4245
* @return {number}
4346
*/
44-
function getQueryLength (length) {
47+
function _getQueryLength (length) {
4548
length = /(-?\d*\.?\d+)(ch|em|ex|px|rem)/.exec(length)
4649

4750
if (length === null) {
4851
return maxValue
4952
}
5053

5154
let number = length[1]
52-
let unit = length[2]
55+
const unit = length[2]
5356

5457
switch (unit) {
5558
case 'ch':
@@ -81,7 +84,7 @@ function getQueryLength (length) {
8184
* @param {RegExp} singleTest
8285
* @return {Function}
8386
*/
84-
function testQuery (doubleTestTrue, doubleTestFalse, singleTest) {
87+
function _testQuery (doubleTestTrue, doubleTestFalse, singleTest) {
8588
/**
8689
* @param {string} query
8790
* @return {boolean}
@@ -96,6 +99,38 @@ function testQuery (doubleTestTrue, doubleTestFalse, singleTest) {
9699
}
97100
}
98101

102+
/**
103+
* @private
104+
* @param {string} a
105+
* @param {string} b
106+
* @return {number|null}
107+
*/
108+
function _testIsPrint (a, b) {
109+
const isPrintA = isPrint.test(a)
110+
const isPrintOnlyA = isPrintOnly.test(a)
111+
112+
const isPrintB = isPrint.test(b)
113+
const isPrintOnlyB = isPrintOnly.test(b)
114+
115+
if (isPrintA && isPrintB) {
116+
if (!isPrintOnlyA && isPrintOnlyB) {
117+
return 1
118+
}
119+
if (isPrintOnlyA && !isPrintOnlyB) {
120+
return -1
121+
}
122+
return a.localeCompare(b)
123+
}
124+
if (isPrintA) {
125+
return 1
126+
}
127+
if (isPrintB) {
128+
return -1
129+
}
130+
131+
return null
132+
}
133+
99134
// ----------------------------------------
100135
// Public
101136
// ----------------------------------------
@@ -108,17 +143,16 @@ function testQuery (doubleTestTrue, doubleTestFalse, singleTest) {
108143
* @return {number} 1 / 0 / -1
109144
*/
110145
function sortCSSmq (a, b) {
111-
if (/print/.test(b)) {
112-
return -1
113-
} else if (/print/.test(a)) {
114-
return 1
146+
const testIsPrint = _testIsPrint(a, b)
147+
if (testIsPrint !== null) {
148+
return testIsPrint
115149
}
116150

117-
let minA = isMinWidth(a) || isMinHeight(a)
118-
let maxA = isMaxWidth(a) || isMaxHeight(a)
151+
const minA = isMinWidth(a) || isMinHeight(a)
152+
const maxA = isMaxWidth(a) || isMaxHeight(a)
119153

120-
let minB = isMinWidth(b) || isMinHeight(b)
121-
let maxB = isMaxWidth(b) || isMaxHeight(b)
154+
const minB = isMinWidth(b) || isMinHeight(b)
155+
const maxB = isMaxWidth(b) || isMaxHeight(b)
122156

123157
if (minA && maxB) {
124158
return -1
@@ -127,8 +161,8 @@ function sortCSSmq (a, b) {
127161
return 1
128162
}
129163

130-
let lengthA = getQueryLength(a)
131-
let lengthB = getQueryLength(b)
164+
let lengthA = _getQueryLength(a)
165+
let lengthB = _getQueryLength(b)
132166

133167
if (lengthA === maxValue && lengthB === maxValue) {
134168
return a.localeCompare(b)
@@ -163,11 +197,16 @@ function sortCSSmq (a, b) {
163197
* @return {number} 1 / 0 / -1
164198
*/
165199
sortCSSmq.desktopFirst = function (a, b) {
166-
let minA = isMinWidth(a) || isMinHeight(a)
167-
let maxA = isMaxWidth(a) || isMaxHeight(a)
200+
const testIsPrint = _testIsPrint(a, b)
201+
if (testIsPrint !== null) {
202+
return testIsPrint
203+
}
204+
205+
const minA = isMinWidth(a) || isMinHeight(a)
206+
const maxA = isMaxWidth(a) || isMaxHeight(a)
168207

169-
let minB = isMinWidth(b) || isMinHeight(b)
170-
let maxB = isMaxWidth(b) || isMaxHeight(b)
208+
const minB = isMinWidth(b) || isMinHeight(b)
209+
const maxB = isMaxWidth(b) || isMaxHeight(b)
171210

172211
if (minA && maxB) {
173212
return 1
@@ -176,8 +215,8 @@ sortCSSmq.desktopFirst = function (a, b) {
176215
return -1
177216
}
178217

179-
let lengthA = getQueryLength(a)
180-
let lengthB = getQueryLength(b)
218+
const lengthA = _getQueryLength(a)
219+
const lengthB = _getQueryLength(b)
181220

182221
if (lengthA === maxValue && lengthB === maxValue) {
183222
return a.localeCompare(b)

package-lock.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sort-css-media-queries",
3-
"version": "1.3.4",
3+
"version": "1.4.0",
44
"description": "The custom `sort` method (mobile-first / desktop-first) of CSS media queries for `css-mqpacker` or `pleeease` (which uses css-mqpacker) or, perhaps, something else ))",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)