1
- 'use strict' ;
2
-
3
1
/**
4
2
* The custom `sort` method for
5
3
* for the [`css-mqpacker`](https://www.npmjs.com/package/css-mqpacker) or
8
6
*
9
7
* @module sort-css-media-queries
10
8
* @author Oleg Dutchenko <[email protected] >
11
- * @version 1.0.0
12
- * @sourcecode |+16
9
+ * @version 1.1.1
13
10
*/
14
11
12
+ 'use strict' ;
13
+
14
+ // ----------------------------------------
15
+ // Private
16
+ // ----------------------------------------
17
+
15
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 / ;
16
19
const minWidth = / \( \s * m i n ( - d e v i c e ) ? - w i d t h / ;
17
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 / ;
@@ -28,77 +31,14 @@ const maxHeight = /\(\s*max(-device)?-height/;
28
31
const isMinHeight = testQuery ( minMaxHeight , maxMinHeight , minHeight ) ;
29
32
const isMaxHeight = testQuery ( maxMinHeight , minMaxHeight , maxHeight ) ;
30
33
31
- /**
32
- * Sorting an array with media queries
33
- * @param {string } a
34
- * @param {string } b
35
- * @return {number } 1 / 0 / -1
36
- * @sourcecode
37
- */
38
- module . exports = function ( a , b ) {
39
- let minA = isMinWidth ( a ) || isMinHeight ( a ) ;
40
- let maxA = isMaxWidth ( a ) || isMaxHeight ( a ) ;
41
-
42
- let minB = isMinWidth ( b ) || isMinHeight ( b ) ;
43
- let maxB = isMaxWidth ( b ) || isMaxHeight ( b ) ;
44
-
45
- if ( minA && maxB ) {
46
- return - 1 ;
47
- }
48
- if ( maxA && minB ) {
49
- return 1 ;
50
- }
51
-
52
- let lengthA = getQueryLength ( a ) ;
53
- let lengthB = getQueryLength ( b ) ;
54
-
55
- if ( lengthA > lengthB ) {
56
- if ( maxA ) {
57
- return - 1 ;
58
- }
59
- return 1 ;
60
- }
61
- if ( lengthA < lengthB ) {
62
- if ( maxA ) {
63
- return 1 ;
64
- }
65
- return - 1 ;
66
- }
67
- return a . localeCompare ( b ) ;
68
- } ;
69
-
70
- /**
71
- * Wrapper for creating test functions
72
- * @private
73
- * @param {RegExp } doubleTestTrue
74
- * @param {RegExp } doubleTestFalse
75
- * @param {RegExp } singleTest
76
- * @return {Function }
77
- * @sourcecode
78
- */
79
- function testQuery ( doubleTestTrue , doubleTestFalse , singleTest ) {
80
- /**
81
- * @param {string } query
82
- * @return {boolean }
83
- */
84
- return function ( query ) {
85
- if ( doubleTestTrue . test ( query ) ) {
86
- return true ;
87
- } else if ( doubleTestFalse . test ( query ) ) {
88
- return false ;
89
- }
90
- return singleTest . test ( query ) ;
91
- }
92
- }
93
-
94
34
/**
95
35
* Obtain the length of the media request in pixels.
96
36
* Copy from original source `function inspectLength (length)`
97
37
* {@link https://github.com/hail2u/node-css-mqpacker/blob/master/index.js#L58}
98
38
* @private
99
39
* @param {string } length
100
40
* @return {number }
101
- * @sourcecode
41
+ * @sourceCode
102
42
*/
103
43
function getQueryLength ( length ) {
104
44
length = / ( - ? \d * \. ? \d + ) ( c h | e m | e x | p x | r e m ) / . exec ( length ) ;
@@ -130,4 +70,73 @@ function getQueryLength (length) {
130
70
}
131
71
132
72
return num ;
133
- }
73
+ }
74
+
75
+ /**
76
+ * Wrapper for creating test functions
77
+ * @private
78
+ * @param {RegExp } doubleTestTrue
79
+ * @param {RegExp } doubleTestFalse
80
+ * @param {RegExp } singleTest
81
+ * @return {Function }
82
+ * @sourceCode
83
+ */
84
+ function testQuery ( doubleTestTrue , doubleTestFalse , singleTest ) {
85
+ /**
86
+ * @param {string } query
87
+ * @return {boolean }
88
+ */
89
+ return function ( query ) {
90
+ if ( doubleTestTrue . test ( query ) ) {
91
+ return true ;
92
+ } else if ( doubleTestFalse . test ( query ) ) {
93
+ return false ;
94
+ }
95
+ return singleTest . test ( query ) ;
96
+ }
97
+ }
98
+
99
+
100
+
101
+ // ----------------------------------------
102
+ // Exports
103
+ // ----------------------------------------
104
+
105
+ /**
106
+ * Sorting an array with media queries
107
+ * @param {string } a
108
+ * @param {string } b
109
+ * @return {number } 1 / 0 / -1
110
+ * @sourceCode
111
+ */
112
+ module . exports = function ( a , b ) {
113
+ let minA = isMinWidth ( a ) || isMinHeight ( a ) ;
114
+ let maxA = isMaxWidth ( a ) || isMaxHeight ( a ) ;
115
+
116
+ let minB = isMinWidth ( b ) || isMinHeight ( b ) ;
117
+ let maxB = isMaxWidth ( b ) || isMaxHeight ( b ) ;
118
+
119
+ if ( minA && maxB ) {
120
+ return - 1 ;
121
+ }
122
+ if ( maxA && minB ) {
123
+ return 1 ;
124
+ }
125
+
126
+ let lengthA = getQueryLength ( a ) ;
127
+ let lengthB = getQueryLength ( b ) ;
128
+
129
+ if ( lengthA > lengthB ) {
130
+ if ( maxA ) {
131
+ return - 1 ;
132
+ }
133
+ return 1 ;
134
+ }
135
+ if ( lengthA < lengthB ) {
136
+ if ( maxA ) {
137
+ return 1 ;
138
+ }
139
+ return - 1 ;
140
+ }
141
+ return a . localeCompare ( b ) ;
142
+ } ;
0 commit comments