Skip to content

add doesMQMatch() fn with unit test coverage #1

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

Merged
merged 3 commits into from
Jan 9, 2014

Conversation

tilomitra
Copy link
Contributor

This pull request adds the doesMQMatch() function which adds the ability to test a media query against an object. All the media query features that are part of the spec are supported.

Some examples:

doesMQMatch('screen and (min-width: 767px) and (max-width: 979px)', {type: 'screen', width: 800}) // returns true
doesMQMatch('screen and (min-width: 767px) and (max-width: 979px)', {type: 'screen', width: 980}) // returns false
doesMQMatch('screen and (min-width: 767px), screen and (color)', {type: 'screen', color: 1}) // returns true
doesMQMatch('not screen and (color)', {type: 'screen', color: 1}); //returns false

TODO

  • Add support for not operator.
  • Possible code cleanup (as per PR discussion)
  • Improve unit tests and get to 100% coverage.

I added a substantial suite of unit tests to make sure this works properly. You can check that out for some more examples. Just run npm test from the root directory. I'm interested in getting some feedback on the general logic while I finish the remaining tasks. Is there a simpler way to do what I'm doing in the doesMQMatch() function?

Thanks!

@@ -91,3 +91,222 @@ function toPx(length) {
default : return value;
}
}

function toDecimal(ratio) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just here because of merge conflicts when I merged your latest changes off master into my working branch.

@ericf
Copy link
Owner

ericf commented Jan 8, 2014

@tilomitra This is looking create and this package is going to be very useful :)

I opened tilomitra#1 which you can cleanly merge into your branch here.

I like the idea of adding tests. With my implementation of match() some tests don't pass, but that brings up some good talking points that I'll add line comments for. For the test code, can you do the following as a general practice:

  • Add 'use strict'; to the file
  • Add the Yahoo copyright banner
  • Hard break at 80 chars, and follow the coding conventions I used in index.js 😄
  • Use BDD-ish expect assertions, like express-state

Some of the tests repeat themselves and aren't very well isolated, e.g. the unit conversion tests also include a boolean (color) expression in the media queries. I think the tests could be refactored to build upon themselves instead of repeat themselves.

I think since you did all these already, we might as well add coverage and get that to 100% for this project. You can look at how express-state is setup for doing this in its package.json "scripts". Also checkout the .gitignore and .npmignore files for keeping test results out of Git and tests out of the npm package.

@tilomitra
Copy link
Contributor Author

@ericf Thanks for the feedback, I'll get on that.

ericf pushed a commit that referenced this pull request Jan 8, 2014
Cleaner match() implementation
@ericf
Copy link
Owner

ericf commented Jan 9, 2014

Sweet! Thanks for getting this up to 100% coverage!

@ericf ericf merged commit d2ede2e into ericf:master Jan 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants