8
8
*
9
9
* @module sort-css-media-queries
10
10
* @author Oleg Dutchenko <dutchenko.o.wezom@gmail.com>
11
- * @version 1.1.1
11
+ * @version 1.4.0
12
12
*/
13
13
14
14
// ----------------------------------------
15
15
// Private
16
16
// ----------------------------------------
17
17
18
- const minMaxWidth = / ( ! ? \( \s * m i n ( - d e v i c e - ) ? - w i d t h ) .+ \( \s * m a x ( - d e v i c e ) ? - w i d t h /
19
- const minWidth = / \( \s * m i n ( - d e v i c e ) ? - w i d t h /
20
- const maxMinWidth = / ( ! ? \( \s * m a x ( - d e v i c e ) ? - w i d t h ) .+ \( \s * m i n ( - d e v i c e ) ? - w i d t h /
21
- const maxWidth = / \( \s * m a x ( - d e v i c e ) ? - w i d t h /
18
+ const minMaxWidth = / ( ! ? \( \s * m i n ( - d e v i c e - ) ? - w i d t h ) .+ \( \s * m a x ( - d e v i c e ) ? - w i d t h / i
19
+ const minWidth = / \( \s * m i n ( - d e v i c e ) ? - w i d t h / i
20
+ const maxMinWidth = / ( ! ? \( \s * m a x ( - d e v i c e ) ? - w i d t h ) .+ \( \s * m i n ( - d e v i c e ) ? - w i d t h / i
21
+ const maxWidth = / \( \s * m a x ( - d e v i c e ) ? - w i d t h / i
22
22
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 )
25
25
26
- const minMaxHeight = / ( ! ? \( \s * m i n ( - d e v i c e ) ? - h e i g h t ) .+ \( \s * m a x ( - d e v i c e ) ? - h e i g h t /
27
- const minHeight = / \( \s * m i n ( - d e v i c e ) ? - h e i g h t /
28
- const maxMinHeight = / ( ! ? \( \s * m a x ( - d e v i c e ) ? - h e i g h t ) .+ \( \s * m i n ( - d e v i c e ) ? - h e i g h t /
29
- const maxHeight = / \( \s * m a x ( - d e v i c e ) ? - h e i g h t /
26
+ const minMaxHeight = / ( ! ? \( \s * m i n ( - d e v i c e ) ? - h e i g h t ) .+ \( \s * m a x ( - d e v i c e ) ? - h e i g h t / i
27
+ const minHeight = / \( \s * m i n ( - d e v i c e ) ? - h e i g h t / i
28
+ const maxMinHeight = / ( ! ? \( \s * m a x ( - d e v i c e ) ? - h e i g h t ) .+ \( \s * m i n ( - d e v i c e ) ? - h e i g h t / i
29
+ const maxHeight = / \( \s * m a x ( - d e v i c e ) ? - h e i g h t / i
30
30
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 = / p r i n t / i
35
+ const isPrintOnly = / ^ p r i n t $ / i
33
36
34
37
const maxValue = Number . MAX_VALUE
35
38
@@ -41,15 +44,15 @@ const maxValue = Number.MAX_VALUE
41
44
* @param {string } length
42
45
* @return {number }
43
46
*/
44
- function getQueryLength ( length ) {
47
+ function _getQueryLength ( length ) {
45
48
length = / ( - ? \d * \. ? \d + ) ( c h | e m | e x | p x | r e m ) / . exec ( length )
46
49
47
50
if ( length === null ) {
48
51
return maxValue
49
52
}
50
53
51
54
let number = length [ 1 ]
52
- let unit = length [ 2 ]
55
+ const unit = length [ 2 ]
53
56
54
57
switch ( unit ) {
55
58
case 'ch' :
@@ -81,7 +84,7 @@ function getQueryLength (length) {
81
84
* @param {RegExp } singleTest
82
85
* @return {Function }
83
86
*/
84
- function testQuery ( doubleTestTrue , doubleTestFalse , singleTest ) {
87
+ function _testQuery ( doubleTestTrue , doubleTestFalse , singleTest ) {
85
88
/**
86
89
* @param {string } query
87
90
* @return {boolean }
@@ -96,6 +99,38 @@ function testQuery (doubleTestTrue, doubleTestFalse, singleTest) {
96
99
}
97
100
}
98
101
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
+
99
134
// ----------------------------------------
100
135
// Public
101
136
// ----------------------------------------
@@ -108,17 +143,16 @@ function testQuery (doubleTestTrue, doubleTestFalse, singleTest) {
108
143
* @return {number } 1 / 0 / -1
109
144
*/
110
145
function sortCSSmq ( a , b ) {
111
- if ( / p r i n t / . test ( b ) ) {
112
- return - 1
113
- } else if ( / p r i n t / . test ( a ) ) {
114
- return 1
146
+ const testIsPrint = _testIsPrint ( a , b )
147
+ if ( testIsPrint !== null ) {
148
+ return testIsPrint
115
149
}
116
150
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 )
119
153
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 )
122
156
123
157
if ( minA && maxB ) {
124
158
return - 1
@@ -127,8 +161,8 @@ function sortCSSmq (a, b) {
127
161
return 1
128
162
}
129
163
130
- let lengthA = getQueryLength ( a )
131
- let lengthB = getQueryLength ( b )
164
+ let lengthA = _getQueryLength ( a )
165
+ let lengthB = _getQueryLength ( b )
132
166
133
167
if ( lengthA === maxValue && lengthB === maxValue ) {
134
168
return a . localeCompare ( b )
@@ -163,11 +197,16 @@ function sortCSSmq (a, b) {
163
197
* @return {number } 1 / 0 / -1
164
198
*/
165
199
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 )
168
207
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 )
171
210
172
211
if ( minA && maxB ) {
173
212
return 1
@@ -176,8 +215,8 @@ sortCSSmq.desktopFirst = function (a, b) {
176
215
return - 1
177
216
}
178
217
179
- let lengthA = getQueryLength ( a )
180
- let lengthB = getQueryLength ( b )
218
+ const lengthA = _getQueryLength ( a )
219
+ const lengthB = _getQueryLength ( b )
181
220
182
221
if ( lengthA === maxValue && lengthB === maxValue ) {
183
222
return a . localeCompare ( b )
0 commit comments