Skip to content

Commit e3f3896

Browse files
author
dutchenkoOleg
committed
1.0.0
1 parent 2d2e712 commit e3f3896

File tree

4 files changed

+218
-20
lines changed

4 files changed

+218
-20
lines changed

.gitignore

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
# Build and Release Folders
2-
bin/
3-
bin-debug/
4-
bin-release/
5-
[Oo]bj/ # FlashDevelop obj
6-
[Bb]in/ # FlashDevelop bin
7-
8-
# Other files and folders
9-
.settings/
10-
11-
# Executables
12-
*.swf
13-
*.air
14-
*.ipa
15-
*.apk
16-
17-
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
18-
# should NOT be excluded as they contain compiler settings and other important
19-
# information for Eclipse / Flash Builder.
1+
.idea
2+
tmp
3+
node_modules
4+
npm-debug.log
5+
.DS_Store
6+
thumbs.db

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
11
# sort-css-media-queries
2-
The custom `sort` method for the plugin `css-mqpacker` and possibly something else
2+
3+
![npm](https://img.shields.io/badge/node-6.10.2-yellow.svg)
4+
![es2015](https://img.shields.io/badge/ECMAScript-2015_(ES6)-blue.svg)
5+
6+
The custom `sort` method for [`css-mqpacker`](https://www.npmjs.com/package/css-mqpacker) or [`pleeease`](https://www.npmjs.com/package/pleeease) (which uses css-mqpacker) or, perhaps, something else ))
7+
8+
## Installing
9+
10+
```shell
11+
npm install --save sort-css-media-queries
12+
# or using yarn cli
13+
yarn add sort-css-media-queries
14+
```
15+
16+
## Usage
17+
18+
See the original docs at first https://www.npmjs.com/package/css-mqpacker#sort;
19+
20+
```js
21+
22+
const sortCSSmq = require('sort-css-media-queries');
23+
24+
// your cool code
25+
// ...
26+
27+
postcss([
28+
mqpacker({
29+
sort: sortCSSmq
30+
})
31+
]).process(css);
32+
33+
```
34+
35+
### Warn
36+
37+
it's use es6 syntax
38+
39+
- ECMAScript 2015 (ES6) and beyond - https://nodejs.org/en/docs/es6/
40+
- Node.js ES2015 Support - http://node.green/
41+
42+
43+
## Tests
44+
45+
Sorry but here no tests yet.
46+
47+
48+
## Contributing
49+
50+
you know what to do - [issues](https://github.com/dutchenkoOleg/sort-css-media-queries/issues) and [pulls](https://github.com/dutchenkoOleg/sort-css-media-queries/pulls)

index.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
'use strict';
2+
3+
/**
4+
* The custom `sort` method for
5+
* for the [`css-mqpacker`](https://www.npmjs.com/package/css-mqpacker) or
6+
* [`pleeease`](https://www.npmjs.com/package/pleeease) which using `css-mqpacker`
7+
* or, perhaps, something else ))
8+
*
9+
* @module sort-css-media-queries
10+
* @author Oleg Dutchenko <[email protected]>
11+
* @version 1.0.0
12+
* @sourcecode |+16
13+
*/
14+
15+
const minMaxWidth = /(!?\(\s*min(-device-)?-width).+\(\s*max(-device)?-width/;
16+
const minWidth = /\(\s*min(-device)?-width/;
17+
const maxMinWidth = /(!?\(\s*max(-device)?-width).+\(\s*min(-device)?-width/;
18+
const maxWidth = /\(\s*max(-device)?-width/;
19+
20+
const isMinWidth = testQuery(minMaxWidth, maxMinWidth, minWidth);
21+
const isMaxWidth = testQuery(maxMinWidth, minMaxWidth, maxWidth);
22+
23+
const minMaxHeight = /(!?\(\s*min(-device)?-height).+\(\s*max(-device)?-height/;
24+
const minHeight = /\(\s*min(-device)?-height/;
25+
const maxMinHeight = /(!?\(\s*max(-device)?-height).+\(\s*min(-device)?-height/;
26+
const maxHeight = /\(\s*max(-device)?-height/;
27+
28+
const isMinHeight = testQuery(minMaxHeight, maxMinHeight, minHeight);
29+
const isMaxHeight = testQuery(maxMinHeight, minMaxHeight, maxHeight);
30+
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+
export default 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+
/**
95+
* Obtain the length of the media request in pixels.
96+
* Copy from original source `function inspectLength (length)`
97+
* {@link https://github.com/hail2u/node-css-mqpacker/blob/master/index.js#L58}
98+
* @private
99+
* @param {string} length
100+
* @return {number}
101+
* @sourcecode
102+
*/
103+
function getQueryLength (length) {
104+
length = /(-?\d*\.?\d+)(ch|em|ex|px|rem)/.exec(length);
105+
106+
if (!length) {
107+
return Number.MAX_VALUE;
108+
}
109+
110+
let num = length[1];
111+
let unit = length[2];
112+
113+
switch (unit) {
114+
case "ch":
115+
num = parseFloat(num) * 8.8984375;
116+
break;
117+
118+
case "em":
119+
case "rem":
120+
num = parseFloat(num) * 16;
121+
break;
122+
123+
case "ex":
124+
num = parseFloat(num) * 8.296875;
125+
break;
126+
127+
case "px":
128+
num = parseFloat(num);
129+
break;
130+
}
131+
132+
return num;
133+
}

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "sort-css-media-queries",
3+
"version": "1.0.0",
4+
"description": "The custom `sort` method for `css-mqpacker` or `pleeease` (which uses css-mqpacker) or, perhaps, something else ))",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/dutchenkoOleg/sort-css-media-queries.git"
12+
},
13+
"keywords": [
14+
"css-mqpacker",
15+
"node-css-mqpacker",
16+
"pleeease",
17+
"mq",
18+
"media",
19+
"queries"
20+
],
21+
"author": "Oleg Dutchenko <[email protected]>",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/dutchenkoOleg/sort-css-media-queries/issues"
25+
},
26+
"engines": {
27+
"node": ">= 6.10.2"
28+
},
29+
"homepage": "https://github.com/dutchenkoOleg/sort-css-media-queries#readme"
30+
}

0 commit comments

Comments
 (0)