-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
34 lines (30 loc) · 851 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module.exports = function (sort) {
if (Array.isArray(sort)) {
var arr = sort
sort = function (a, b) {
return arr.indexOf(a) - arr.indexOf(b)
}
} else if (sort && typeof sort !== 'function') {
throw TypeError('Sort argument must either be an array or a function.')
}
return function (style) {
var rules = style.rules
var media = {}
for (var i = 0; i < rules.length; i++) {
var rule = rules[i]
var query = rule.media
if (!query) continue;
rules.splice(i--, 1)
;[].push.apply(media[query] || (media[query] = []), rule.rules)
}
var queries = Object.keys(media)
if (sort) queries = queries.sort(sort);
;[].push.apply(rules, queries.map(function (query) {
return {
type: 'media',
media: query,
rules: media[query]
}
}))
}
}