Code coverage report for parse-css-font\index.js

Statements: 100% (63 / 63)      Branches: 100% (32 / 32)      Functions: 100% (4 / 4)      Lines: 100% (63 / 63)      Ignored: none     

All files » parse-css-font/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1211 1 1 1 1 1 1   1   1       1                   1   1     47 1     46 6     40               40 40 40 40   92 7 28   7 7     85 18 4   14 14     67 8 1   7 7     59 14 4   10 10     45 38 38 38 9   38 1   37 37     7 1     6 1   5     1       1 4     1 9 9 7   2    
var t = require('tcomb');
var unquote = require('unquote');
var cssGlobalValues = require('css-global-values');
var cssSystemFonts = require('css-system-fonts');
var cssFontWeights = require('css-font-weights');
var cssFontStyles = require('css-font-styles');
var cssFontStretches = require('css-font-stretches');
 
var helpers = require('./lib/helpers');
 
var SystemFont = t.struct({
	system: t.String
});
 
var Font = t.struct({
	style: t.String,
	variant: t.String,
	weight: t.String,
	stretch: t.String,
	size: t.String,
	lineHeight: t.union([t.String, t.Number]),
	family: t.list(t.String)
});
 
var Result = t.union([Font, SystemFont]);
 
module.exports = t.func(t.String, t.Object).of(
	function(value) {
 
		if (value === '') {
			throw error('Cannot parse an empty string.');
		}
 
		if (cssSystemFonts().indexOf(value) !== -1) {
			return SystemFont({ system: value });
		}
 
		var font = {
			style: 'normal',
			variant: 'normal',
			weight: 'normal',
			stretch: 'normal',
			lineHeight: 'normal'
		};
 
		var isLocked = false;
		var tokens = value.split(/\s+/);
		var token = tokens.shift();
		for (; !t.Nil.is(token); token = tokens.shift()) {
 
			if (token === 'normal' || cssGlobalValues.indexOf(token) !== -1) {
				['style', 'variant', 'weight', 'stretch'].forEach(function(prop) {
					font[prop] = token;
				});
				isLocked = true;
				continue;
			}
 
			if (cssFontWeights.indexOf(token) !== -1) {
				if (isLocked) {
					continue;
				}
				font.weight = token;
				continue;
			}
 
			if (cssFontStyles.indexOf(token) !== -1) {
				if (isLocked) {
					continue;
				}
				font.style = token;
				continue;
			}
 
			if (cssFontStretches.indexOf(token) !== -1) {
				if (isLocked) {
					continue;
				}
				font.stretch = token;
				continue;
			}
 
			if (helpers.isSize(token)) {
				var parts = token.split('/');
				font.size = parts[0];
				if (!t.Nil.is(parts[1])) {
					font.lineHeight = parseLineHeight(parts[1]);
				}
				if (!tokens.length) {
					throw error('Missing required font-family.');
				}
				font.family = tokens.join(' ').split(/\s*,\s*/).map(unquote);
				return Font(font);
			}
 
			if (font.variant !== 'normal') {
				throw error('Unknown or unsupported font token: ' + font.variant);
			}
 
			if (isLocked) {
				continue;
			}
			font.variant = token;
		}
 
		throw error('Missing required font-size.');
	}
);
 
function error(message) {
	return new Error('[parse-css-font] ' + message);
}
 
function parseLineHeight(value) {
	var parsed = parseFloat(value);
	if (parsed.toString() === value) {
		return parsed;
	}
	return value;
}