From e8db5e7536d340f3decb6998cd1298b251ace402 Mon Sep 17 00:00:00 2001 From: dutchenkoOleg Date: Fri, 7 Jul 2017 20:49:34 +0300 Subject: [PATCH 1/2] happiness code style --- index.js | 28 +++++++++++++--------------- package.json | 9 +++++++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 5dfac10..744c911 100644 --- a/index.js +++ b/index.js @@ -51,20 +51,20 @@ function getQueryLength (length) { let unit = length[2]; switch (unit) { - case "ch": + case 'ch': num = parseFloat(num) * 8.8984375; break; - case "em": - case "rem": + case 'em': + case 'rem': num = parseFloat(num) * 16; break; - case "ex": + case 'ex': num = parseFloat(num) * 8.296875; break; - case "px": + case 'px': num = parseFloat(num); break; } @@ -86,18 +86,16 @@ function testQuery (doubleTestTrue, doubleTestFalse, singleTest) { * @param {string} query * @return {boolean} */ - return function(query) { - if ( doubleTestTrue.test(query) ) { + return function (query) { + if (doubleTestTrue.test(query)) { return true; - } else if ( doubleTestFalse.test(query) ) { + } else if (doubleTestFalse.test(query)) { return false; } return singleTest.test(query); - } + }; } - - // ---------------------------------------- // Exports // ---------------------------------------- @@ -112,20 +110,20 @@ function testQuery (doubleTestTrue, doubleTestFalse, singleTest) { module.exports = function (a, b) { let minA = isMinWidth(a) || isMinHeight(a); let maxA = isMaxWidth(a) || isMaxHeight(a); - + let minB = isMinWidth(b) || isMinHeight(b); let maxB = isMaxWidth(b) || isMaxHeight(b); - + if (minA && maxB) { return -1; } if (maxA && minB) { return 1; } - + let lengthA = getQueryLength(a); let lengthB = getQueryLength(b); - + if (lengthA > lengthB) { if (maxA) { return -1; diff --git a/package.json b/package.json index c06c99f..4d429a0 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "The custom `sort` method for `css-mqpacker` or `pleeease` (which uses css-mqpacker) or, perhaps, something else ))", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "happiness --verbose | snazzy", + "fix": "happiness --verbose --fix | snazzy" }, "repository": { "type": "git", @@ -26,5 +27,9 @@ "engines": { "node": ">= 6.3.0" }, - "homepage": "https://github.com/dutchenkoOleg/sort-css-media-queries#readme" + "homepage": "https://github.com/dutchenkoOleg/sort-css-media-queries#readme", + "devDependencies": { + "happiness": "^7.1.2", + "snazzy": "^7.0.0" + } } From 5269e88cde99c6c745dc7920bcd01984e19dde88 Mon Sep 17 00:00:00 2001 From: dutchenkoOleg Date: Fri, 7 Jul 2017 22:06:31 +0300 Subject: [PATCH 2/2] tests --- .travis.yml | 5 +++ CHANGELOG.md | 23 ++++++++++++++ README.md | 13 ++++++-- package.json | 6 ++-- tests/index.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 tests/index.js diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..53be739 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +sudo: false +language: node_js +node_js: + - "7" + - "6" diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b25ee0f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,23 @@ +# Change log + +:back: [README.md](./README.md) + +> _All notable changes to this project will be documented in this file._ +> _This project adheres to [Semantic Versioning](http://semver.org/)._ + +--- + +### [1.1.9] - 2017-07-07 + +#### Added + +- this CHANGELOG.md file )) +- test for code style +- test of sorting result, see [./tests/index.js](./tests/index.js) +- [Travis CI](https://travis-ci.org/dutchenkoOleg/gulp-not-supported-file) builds + +#### Changed + +- js code style with accordance to [`happiness`]((https://github.com/JedWatson/happiness)) + +--- diff --git a/README.md b/README.md index e03dbdf..ccc62b0 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,12 @@ ![npm](https://img.shields.io/badge/node-6.3.0-yellow.svg) ![es2015](https://img.shields.io/badge/ECMAScript-2015_(ES6)-blue.svg) +![license](https://img.shields.io/badge/License-MIT-orange.svg) +[![Build Status](https://travis-ci.org/dutchenkoOleg/sort-css-media-queries.svg?branch=master)](https://travis-ci.org/dutchenkoOleg/sort-css-media-queries) -The custom `sort` method for [`css-mqpacker`](https://www.npmjs.com/package/css-mqpacker) or [`pleeease`](https://www.npmjs.com/package/pleeease) (which uses css-mqpacker) or, perhaps, something else )) +> The custom `sort` method for [`css-mqpacker`](https://www.npmjs.com/package/css-mqpacker) or [`pleeease`](https://www.npmjs.com/package/pleeease) (which uses css-mqpacker) or, perhaps, something else )) + +[![js happiness style](https://cdn.rawgit.com/JedWatson/happiness/master/badge.svg)](https://github.com/JedWatson/happiness) ## Installing @@ -42,7 +46,12 @@ it's use es6 syntax ## Tests -Sorry but here no tests yet. +1. `npm test` for testing js code style and test sorting method +1. `npm run fix` for automatically fix most of problems with **js code style** + +## Changelog + +Please read [CHANGELOG.md](https://github.com/dutchenkoOleg/sort-css-media-queries/blob/master/CHANGELOG.md) ## Contributing diff --git a/package.json b/package.json index 4d429a0..40f03e9 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "sort-css-media-queries", - "version": "1.1.1", + "version": "1.1.9", "description": "The custom `sort` method for `css-mqpacker` or `pleeease` (which uses css-mqpacker) or, perhaps, something else ))", "main": "index.js", "scripts": { - "test": "happiness --verbose | snazzy", + "test": "happiness --verbose | snazzy && node ./tests/index.js", "fix": "happiness --verbose --fix | snazzy" }, "repository": { @@ -19,7 +19,7 @@ "media", "queries" ], - "author": "Oleg Dutchenko ", + "author": "Oleg Dutchenko ", "license": "MIT", "bugs": { "url": "https://github.com/dutchenkoOleg/sort-css-media-queries/issues" diff --git a/tests/index.js b/tests/index.js new file mode 100644 index 0000000..e4cd0ab --- /dev/null +++ b/tests/index.js @@ -0,0 +1,82 @@ +/** + * > Sorting test + * @module path/to + * @author Oleg Dutchenko + */ + +'use strict'; + +// ---------------------------------------- +// Imports +// ---------------------------------------- + +const sortCSSmq = require('../'); + +// ---------------------------------------- +// Helpers / Private +// ---------------------------------------- + +// randomize +function shuffleArray (array) { + let newArray = array.concat(); + + for (let i = array.length - 1; i > 0; i--) { + let j = Math.floor(Math.random() * (i + 1)); + let temp = newArray[i]; + + newArray[i] = newArray[j]; + newArray[j] = temp; + } + return newArray; +} + +// ---------------------------------------- +// Public +// ---------------------------------------- + +// correct sorted order +const queries = [ + // min-width/-height -> from smallest to largest + 'only screen and (min-width: 320px) and (max-width: 767px)', + 'screen and (min-height: 480px)', + 'screen and (min-height: 480px) and (min-width: 320px)', + 'only screen and (min-width: 640px)', + 'screen and (min-width: 1024px)', + 'only screen and (min-width: 1280px)', + + // device + 'only screen and (min-device-width: 320px) and (max-device-width: 767px)', + + // max-width/-height <- from largest to smallest + 'only screen and (max-width: 1023px)', + 'only screen and (max-height: 767px) and (min-height: 320px)', + 'only screen and (max-width: 767px) and (min-width: 320px)', + 'screen and (max-width: 639px)' +]; + +// shuffle it +const random = shuffleArray(queries); + +// sort by module +random.sort(sortCSSmq); + +// make strings for compare +const correct = queries.join(','); +const sorted = random.join(','); + +// lets test +if (correct !== sorted) { + let msg = [ + '', + 'ERROR -----------------', + 'sortCSSmq result should be same as correct!', + 'Correct sort', + `- ${queries.join('\n- ')}`, + 'sortCSSmq result:', + `- ${random.join('\n- ')}`, + '' + ].join('\n\n'); + + console.log(msg); + process.exit(1); +}