All files / dist index.js

100% Statements 66/66
100% Branches 36/36
100% Functions 4/4
100% Lines 66/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   51x 1x   50x 1x   49x 6x   43x             43x 43x 43x 43x 95x   7x 28x   7x 7x   88x 18x 4x   14x 14x   70x 8x 1x   7x 7x   62x 14x 4x   10x 10x   48x 41x 41x 41x 11x   30x 1x 1x   41x 1x   40x     40x   7x 1x   6x 1x   5x   1x   1x   4x     12x 12x 8x   4x     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var unquote = require('unquote');
var globalKeywords = require('css-global-keywords');
var systemFontKeywords = require('css-system-font-keywords');
var fontWeightKeywords = require('css-font-weight-keywords');
var fontStyleKeywords = require('css-font-style-keywords');
var fontStretchKeywords = require('css-font-stretch-keywords');
var cssListHelpers = require("css-list-helpers");
var helpers = require("./helpers");
var errorPrefix = '[parse-css-font] ';
function parseCSSFont(value) {
    if (typeof value !== 'string') {
        throw new TypeError(errorPrefix + 'Expected a string.');
    }
    if (value === '') {
        throw error('Cannot parse an empty string.');
    }
    if (systemFontKeywords.indexOf(value) !== -1) {
        return { system: value };
    }
    var font = {
        lineHeight: 'normal',
        stretch: 'normal',
        style: 'normal',
        variant: 'normal',
        weight: 'normal',
    };
    var isLocked = false;
    var tokens = cssListHelpers.splitBySpaces(value);
    var token = tokens.shift();
    for (; !!token; token = tokens.shift()) {
        if (token === 'normal' || globalKeywords.indexOf(token) !== -1) {
            ;
            ['style', 'variant', 'weight', 'stretch'].forEach(function (prop) {
                font[prop] = token;
            });
            isLocked = true;
            continue;
        }
        if (fontWeightKeywords.indexOf(token) !== -1) {
            if (isLocked) {
                continue;
            }
            font.weight = token;
            continue;
        }
        if (fontStyleKeywords.indexOf(token) !== -1) {
            if (isLocked) {
                continue;
            }
            font.style = token;
            continue;
        }
        if (fontStretchKeywords.indexOf(token) !== -1) {
            if (isLocked) {
                continue;
            }
            font.stretch = token;
            continue;
        }
        if (helpers.isSize(token)) {
            var parts = cssListHelpers.split(token, ['/']);
            font.size = parts[0];
            if (!!parts[1]) {
                font.lineHeight = parseLineHeight(parts[1]);
            }
            else if (tokens[0] === '/') {
                tokens.shift();
                font.lineHeight = parseLineHeight(tokens.shift());
            }
            if (!tokens.length) {
                throw error('Missing required font-family.');
            }
            font.family = cssListHelpers
                .splitByCommas(tokens.join(' '))
                .map(unquote);
            return 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.');
}
exports.default = parseCSSFont;
function error(message) {
    return new Error(errorPrefix + message);
}
function parseLineHeight(value) {
    var parsed = parseFloat(value);
    if (parsed.toString() === value) {
        return parsed;
    }
    return value;
}
// @ts-ignore
module.exports = Object.assign(exports.default, exports);