CSS Font Value Parser
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
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ECMAScript Module (ESM) context without proper configuration or a bundler.fixEnsure your project is configured for CommonJS, or use a bundler to resolve CommonJS modules in an ESM project. If in Node.js ESM, consider dynamic `import('css-font-parser-papandreou').then(mod => mod.default)`. -
TypeError: parse is not a function
cause Incorrectly importing the `parse` function. This typically happens when using `import { parse } from '...'` on a CommonJS module that exports the function as `module.exports = parseFunction` (a default export pattern).fixFor CommonJS, use `const parse = require('css-font-parser-papandreou');`. If attempting ESM, `import parse from 'css-font-parser-papandreou';` might be the correct syntax if your bundler handles CJS interoperability, but `require()` is the canonical way. -
Parse result does not include expected font properties (e.g., 'font-weight', 'font-style').
cause The input CSS font value might be malformed, contain unsupported keywords, or the parser might not recognize all possible CSS font shorthand variations or complex values.fixDouble-check the input string against CSS font shorthand syntax rules. Test with simpler inputs first. If certain properties are consistently missed, the parser may not support them, and manual extraction or a different library might be needed.
Warnings
- gotcha This package is primarily a CommonJS module. Direct ESM imports (e.g., `import ... from '...'`) may not work as expected in pure ESM environments without a bundler or specific Node.js configuration, leading to `require is not defined` errors.
- gotcha As a pre-1.0 version (`0.2.3-patch1`), the API, while seemingly stable for its primary function, could theoretically have minor breaking changes in parsing behavior or output structure in future patch or minor releases, though less likely for such a focused utility.
- gotcha The parser is specifically designed for CSS font shorthand values. Supplying malformed or unexpected CSS strings (e.g., non-font properties) may result in incomplete parsing, `null` values, or unexpected object structures rather than throwing explicit errors.
- gotcha This package is a fork (`-papandreou`) of an older `css-font-parser` library. While providing ongoing maintenance, it might diverge in behavior or feature set from the original or other community forks. Be aware of the specific fork being used.
Install
-
npm install css-font-parser-papandreou -
yarn add css-font-parser-papandreou -
pnpm add css-font-parser-papandreou
Imports
- parse
import parse from 'css-font-parser-papandreou';
const parse = require('css-font-parser-papandreou'); - parse
import { parse } from 'css-font-parser-papandreou';const parse = require('css-font-parser-papandreou');
Quickstart
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']
}
*/