CSS Font Value Parser

0.2.3-patch1 · maintenance · verified Tue Apr 21

The `css-font-parser-papandreou` package provides a lightweight, dedicated parser for CSS font shorthand property values. It transforms a CSS font string, such as `15px/18px "Neue Helvetica", Helvetica, sans-serif`, into a structured JavaScript object, extracting components like `font-family` (as an array), `font-size`, and `line-height`. The current stable version, `0.2.3-patch1`, signifies a pre-1.0 development stage, and as a fork of the original `css-font-parser`, it suggests a maintenance-oriented release cadence by Andreas Papandreou. This library differentiates itself through its singular focus on parsing this specific CSS property, offering a minimal footprint solution for environments where a full CSS parser is overkill. It's particularly useful for applications that need to programmatically inspect or manipulate font declarations from user input or dynamic styles.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `parse` function to extract font properties from various CSS font shorthand strings.

const parse = require('css-font-parser-papandreou');

// Parse a simple font value
const simpleFont = parse('15px sans-serif');
console.log('Simple Font:', simpleFont);
/* Expected output:
{
  'font-family': ['sans-serif'],
  'font-size': '15px'
}
*/

// Parse a more complex font value with line-height and multiple font families
const complexFont = parse('15px/18px "Neue Helvetica", Helvetica, sans-serif');
console.log('Complex Font:', complexFont);
/* Expected output:
{
  'font-family': ['Neue Helvetica', 'Helvetica', 'sans-serif'],
  'font-size': '15px',
  'line-height': '18px'
}
*/

// Example with a different font style (assuming it's supported by the parser)
const italicFont = parse('italic bold 16px/24px Arial, sans-serif');
console.log('Italic Font:', italicFont);
/* Expected structure (actual parsed values depend on parser's full capabilities):
{
  'font-style': 'italic',
  'font-weight': 'bold',
  'font-size': '16px',
  'line-height': '24px',
  'font-family': ['Arial', 'sans-serif']
}
*/

view raw JSON →