Skip to content

added matchFromAST #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 79 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ See the accompanying LICENSE file for terms.

'use strict';

exports.match = matchQuery;
exports.parse = parseQuery;
exports.match = matchQuery;
exports.parse = parseQuery;
exports.matchFromAST = matchFromAST;

// -----------------------------------------------------------------------------

Expand All @@ -17,71 +18,16 @@ var RE_MEDIA_QUERY = /^(?:(only|not)?\s*([_a-z][_a-z0-9-]*)|(\([^\)]+\)))(?:
RE_LENGTH_UNIT = /(em|rem|px|cm|mm|in|pt|pc)?\s*$/,
RE_RESOLUTION_UNIT = /(dpi|dpcm|dppx)?\s*$/;

function matchFromAST(ast, values){
return ast.some(function(expression){
return matchSingleExpression(expression, values);
});
}

function matchQuery(mediaQuery, values) {
return parseQuery(mediaQuery).some(function (query) {
var inverse = query.inverse;

// Either the parsed or specified `type` is "all", or the types must be
// equal for a match.
var typeMatch = query.type === 'all' || values.type === query.type;

// Quit early when `type` doesn't match, but take "not" into account.
if ((typeMatch && inverse) || !(typeMatch || inverse)) {
return false;
}

var expressionsMatch = query.expressions.every(function (expression) {
var feature = expression.feature,
modifier = expression.modifier,
expValue = expression.value,
value = values[feature];

// Missing or falsy values don't match.
if (!value) { return false; }

switch (feature) {
case 'orientation':
case 'scan':
return value.toLowerCase() === expValue.toLowerCase();

case 'width':
case 'height':
case 'device-width':
case 'device-height':
expValue = toPx(expValue);
value = toPx(value);
break;

case 'resolution':
expValue = toDpi(expValue);
value = toDpi(value);
break;

case 'aspect-ratio':
case 'device-aspect-ratio':
case /* Deprecated */ 'device-pixel-ratio':
expValue = toDecimal(expValue);
value = toDecimal(value);
break;

case 'grid':
case 'color':
case 'color-index':
case 'monochrome':
expValue = parseInt(expValue, 10) || 1;
value = parseInt(value, 10) || 0;
break;
}

switch (modifier) {
case 'min': return value >= expValue;
case 'max': return value <= expValue;
default : return value === expValue;
}
});

return (expressionsMatch && !inverse) || (!expressionsMatch && inverse);
});
return parseQuery(mediaQuery).some(function(expression){
return matchSingleExpression(expression, values);
});
}

function parseQuery(mediaQuery) {
Expand Down Expand Up @@ -138,8 +84,75 @@ function parseQuery(mediaQuery) {
});
}



// -- Utilities ----------------------------------------------------------------

function matchSingleExpression(query, values) {
var inverse = query.inverse;

// Either the parsed or specified `type` is "all", or the types must be
// equal for a match.
var typeMatch = query.type === 'all' || values.type === query.type;

// Quit early when `type` doesn't match, but take "not" into account.
if ((typeMatch && inverse) || !(typeMatch || inverse)) {
return false;
}

var expressionsMatch = query.expressions.every(function (expression) {
var feature = expression.feature,
modifier = expression.modifier,
expValue = expression.value,
value = values[feature];

// Missing or falsy values don't match.
if (!value) { return false; }

switch (feature) {
case 'orientation':
case 'scan':
return value.toLowerCase() === expValue.toLowerCase();

case 'width':
case 'height':
case 'device-width':
case 'device-height':
expValue = toPx(expValue);
value = toPx(value);
break;

case 'resolution':
expValue = toDpi(expValue);
value = toDpi(value);
break;

case 'aspect-ratio':
case 'device-aspect-ratio':
case /* Deprecated */ 'device-pixel-ratio':
expValue = toDecimal(expValue);
value = toDecimal(value);
break;

case 'grid':
case 'color':
case 'color-index':
case 'monochrome':
expValue = parseInt(expValue, 10) || 1;
value = parseInt(value, 10) || 0;
break;
}

switch (modifier) {
case 'min': return value >= expValue;
case 'max': return value <= expValue;
default : return value === expValue;
}
});

return (expressionsMatch && !inverse) || (!expressionsMatch && inverse);
}

function toDecimal(ratio) {
var decimal = Number(ratio),
numbers;
Expand Down
12 changes: 11 additions & 1 deletion test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe('mediaQuery.parse()', function () {
});

describe('mediaQuery.match()', function () {
describe('Equality Check', function () {

describe('Equality Check', function () {
it('Orientation: should return true for a correct match (===)', function () {
expect(mediaQuery.match(
'(orientation: portrait)', {orientation: 'portrait'}
Expand Down Expand Up @@ -415,3 +416,12 @@ describe('mediaQuery.match()', function () {
});
});


describe('mediaQuery.matchFromAST()', function () {
it('should take parsed ast and match it', function(){
expect(mediaQuery.matchFromAST(
mediaQuery.parse('screen and (min-width: 767px) and (max-width: 979px)'),
{width: 800, type : 'screen',}
)).to.be.true;
});
});