Skip to content

Commit

Permalink
Changed API. removed type
Browse files Browse the repository at this point in the history
  • Loading branch information
kiran committed Jan 15, 2015
1 parent 22aa655 commit ee17cfd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
26 changes: 14 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ var isDimension = function (feature) {

var obj2mq = function (obj) {
var mq = '';
if (obj.type) {
mq += obj.type + ' and ';
delete obj.type;
}
var features = Object.keys(obj);
features.forEach(function (feature, index) {
var value = obj[feature];
Expand All @@ -20,13 +16,16 @@ var obj2mq = function (obj) {
value = value + 'px';
}
if (value === true) {
mq += '(' + feature + ') and ';
mq += feature;
} else if (value === false) {
mq += 'not ' + feature;
} else {
mq += '(' + feature + ': ' + value + ') and ';
mq += '(' + feature + ': ' + value + ')';
}
if (index < features.length-1) {
mq += ' and '
}
});
// Remove extra 'and' at the end
mq = mq.replace(/ and $/, '');
return mq;
};

Expand All @@ -35,14 +34,17 @@ var json2mq = function (query) {
if (typeof query === 'string') {
return query;
}
// Handling array of media queries
if (query instanceof Array) {
query.forEach(function (q) {
mq += obj2mq(q) + ', ';
query.forEach(function (q, index) {
mq += obj2mq(q);
if (index < query.length-1) {
mq += ', '
}
});
// Remove extra comma at the end
mq = mq.replace(/, $/, '');
return mq;
}
// Handling single media query
return obj2mq(query);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Generate media query string from JSON or javascript object",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha test -w"
"test": "./node_modules/.bin/mocha test"
},
"repository": {
"type": "git",
Expand Down
12 changes: 8 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ var json2mq = require('../');

describe('json2mq', function () {
it('should return query string for media type', function () {
json2mq({type: 'screen'}).should.equal('screen');
json2mq({screen: true}).should.equal('screen');
});

it('should return query string for media type with not', function () {
json2mq({handheld: false}).should.equal('not handheld');
});

it('should return query string for media features', function () {
json2mq({minWidth: 100, maxWidth: 200}).should.equal('(min-width: 100px) and (max-width: 200px)');
});

it('should return query string for media type and media features', function () {
json2mq({type: 'screen', minWidth: 100, maxWidth: 200}).should.equal('screen and (min-width: 100px) and (max-width: 200px)');
json2mq({screen: true, minWidth: 100, maxWidth: 200}).should.equal('screen and (min-width: 100px) and (max-width: 200px)');
});

it('should add px unit to dimension features', function () {
Expand All @@ -25,11 +29,11 @@ describe('json2mq', function () {
it('should return comma seperated query string for multiple media queries', function () {
json2mq([
{minWidth: 100},
{type: 'handheld', orientation: 'landscape'}
{handheld: true, orientation: 'landscape'}
]).should.equal('(min-width: 100px), handheld and (orientation: landscape)');
});

it('should only return feature if its value is true', function () {
json2mq({type: 'all', monochrome: true}).should.equal('all and (monochrome)');
json2mq({all: true, monochrome: true}).should.equal('all and monochrome');
});
});

0 comments on commit ee17cfd

Please sign in to comment.