UNPKG

1.33 kBJavaScriptView Raw
1/**
2 * source by `json2mq`
3 * https://github.com/akiran/json2mq.git
4 */
5var camel2hyphen = function camel2hyphen(str) {
6 return str.replace(/[A-Z]/g, function (match) {
7 return '-' + match.toLowerCase();
8 }).toLowerCase();
9};
10
11var isDimension = function isDimension(feature) {
12 var re = /[height|width]$/;
13 return re.test(feature);
14};
15
16var obj2mq = function obj2mq(obj) {
17 var mq = '';
18 var features = Object.keys(obj);
19 features.forEach(function (feature, index) {
20 var value = obj[feature];
21 feature = camel2hyphen(feature); // Add px to dimension features
22
23 if (isDimension(feature) && typeof value === 'number') {
24 value = value + 'px';
25 }
26
27 if (value === true) {
28 mq += feature;
29 } else if (value === false) {
30 mq += 'not ' + feature;
31 } else {
32 mq += '(' + feature + ': ' + value + ')';
33 }
34
35 if (index < features.length - 1) {
36 mq += ' and ';
37 }
38 });
39 return mq;
40};
41
42export default function (query) {
43 var mq = '';
44
45 if (typeof query === 'string') {
46 return query;
47 } // Handling array of media queries
48
49
50 if (query instanceof Array) {
51 query.forEach(function (q, index) {
52 mq += obj2mq(q);
53
54 if (index < query.length - 1) {
55 mq += ', ';
56 }
57 });
58 return mq;
59 } // Handling single media query
60
61
62 return obj2mq(query);
63}
\No newline at end of file