Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Forked from ericf/gist:8026041
Last active December 31, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/8028026 to your computer and use it in GitHub Desktop.
Save juandopazo/8028026 to your computer and use it in GitHub Desktop.
'use strict';
var fs = require('fs'),
path = require('path'),
parseCSS = require('css-parse'),
Promise = require('es6-promise').Promise;
module.exports = function gridUnits(pureDir, callback) {
readUnits(path.join(pureDir, 'grids-units.css'))
.then(parseCSS)
.then(getUnits)
.then(function (value) {
setImmediate(function () {
callback(null, value);
});
}, function (err) {
setImmediate(function () {
callback(err);
});
});
};
// -----------------------------------------------------------------------------
function getUnits(unitsCSS) {
var units = {n5: [], n24: []};
unitsCSS.stylesheet.rules.forEach(function (rule) {
if (!rule.declarations) { return; }
rule.declarations.forEach(function (declaration) {
if (declaration.property !== 'width') { return; }
rule.selectors.map(function (selector) {
return selector.substring(1);
}).forEach(function (selector) {
var captures = selector.match(/(\d+)(?:-(\d+))?$/),
numerator = captures[1],
denominator = captures[2] || null;
var entry = {
width : numerator + (denominator ? '-' + denominator : ''),
selector : selector,
isReduced: isReducedFraction(numerator, denominator)
};
switch (denominator) {
case null:
case '1':
units.n5.push(entry);
units.n24.push(entry);
break;
case '5' :
units.n5.push(entry);
break;
default:
units.n24.push(entry);
}
});
});
});
units.n5.sort(compareWidths);
units.n24.sort(compareWidths);
return units;
}
function compareWidths(a, b) {
a = a.width.split('-').map(Number);
b = b.width.split('-').map(Number);
// Whole numbers.
a[1] || (a[1] = 1);
b[1] || (b[1] = 1);
if ((a[0] / a[1]) < (b[0] / b[1])) { return -1; }
if ((a[0] / a[1]) > (b[0] / b[1])) { return 1; }
if (a[1] < b[1]) { return -1; }
if (a[1] > b[1]) { return 1; }
return 0;
}
function isReducedFraction(numerator, denominator) {
numerator = Number(numerator);
denominator = Number(denominator) || 1;
var gcd = getGCD(numerator, denominator),
rNumerator = numerator / gcd,
rDenominator = denominator / gcd;
return rNumerator === numerator && rDenominator === denominator;
}
function getGCD(a, b) {
return b ? getGCD(b, a % b) : a;
}
function readUnits(filePath) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath, 'utf8', function (err, file) {
if (err) { return reject(err); }
resolve(file);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment