Skip to main content

Pretty Printing

The pretty function converts an Ingredient object back into a human-readable string, with intelligent fraction formatting.

Usage

import {pretty} from '@magrinj/parse-ingredients';

pretty({
quantity: '1.5',
unit: 'cup',
ingredient: 'milk',
article: 'of',
symbol: 'c',
minQty: '1.5',
maxQty: '1.5',
});
// "1 1/2 cups of milk"

Fraction Conversion

Decimals are automatically converted to readable fractions:

DecimalOutput
0.251/4
0.3331/3
0.51/2
0.6662/3
0.753/4
1.51 1/2
1.3331 1/3
2.6662 2/3

The function handles common recurring fractions (1/3, 2/3, 1/6, 1/9, 5/6) using a lookup table, and reduces other fractions using GCD.

Output Format

The output follows the pattern: [quantity] [unit] [article] [ingredient]

  • Quantity 1 → singular unit form
  • Quantity > 1 → plural unit form
  • No quantity → just the ingredient name
  • No unit → [quantity] [ingredient]