Skip to content

Commit 774916c

Browse files
author
dutchenkoOleg
committed
1.1.1 - refactoring
1 parent 5f662f3 commit 774916c

File tree

2 files changed

+79
-70
lines changed

2 files changed

+79
-70
lines changed

index.js

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
/**
42
* The custom `sort` method for
53
* for the [`css-mqpacker`](https://www.npmjs.com/package/css-mqpacker) or
@@ -8,10 +6,15 @@
86
*
97
* @module sort-css-media-queries
108
* @author Oleg Dutchenko <[email protected]>
11-
* @version 1.0.0
12-
* @sourcecode |+16
9+
* @version 1.1.1
1310
*/
1411

12+
'use strict';
13+
14+
// ----------------------------------------
15+
// Private
16+
// ----------------------------------------
17+
1518
const minMaxWidth = /(!?\(\s*min(-device-)?-width).+\(\s*max(-device)?-width/;
1619
const minWidth = /\(\s*min(-device)?-width/;
1720
const maxMinWidth = /(!?\(\s*max(-device)?-width).+\(\s*min(-device)?-width/;
@@ -28,77 +31,14 @@ const maxHeight = /\(\s*max(-device)?-height/;
2831
const isMinHeight = testQuery(minMaxHeight, maxMinHeight, minHeight);
2932
const isMaxHeight = testQuery(maxMinHeight, minMaxHeight, maxHeight);
3033

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-
9434
/**
9535
* Obtain the length of the media request in pixels.
9636
* Copy from original source `function inspectLength (length)`
9737
* {@link https://github.com/hail2u/node-css-mqpacker/blob/master/index.js#L58}
9838
* @private
9939
* @param {string} length
10040
* @return {number}
101-
* @sourcecode
41+
* @sourceCode
10242
*/
10343
function getQueryLength (length) {
10444
length = /(-?\d*\.?\d+)(ch|em|ex|px|rem)/.exec(length);
@@ -130,4 +70,73 @@ function getQueryLength (length) {
13070
}
13171

13272
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+
};

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.1.0",
3+
"version": "1.1.1",
44
"description": "The custom `sort` method for `css-mqpacker` or `pleeease` (which uses css-mqpacker) or, perhaps, something else ))",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)