All files / src recipeFormat.ts

100% Statements 14/14
100% Branches 7/7
100% Functions 3/3
100% Lines 11/11

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    3x                                       70x   70x 42x 42x 595x 42x     65x                                 3x 3x 948x  
import recipeTags from './recipeTags.json'
 
const FRACTIONS: [number, string][] = [
	[1 / 8, '⅛'],
	[1 / 6, '⅙'],
	[1 / 5, '⅕'],
	[1 / 4, '¼'],
	[1 / 3, '⅓'],
	[3 / 8, '⅜'],
	[2 / 5, '⅖'],
	[1 / 2, '½'],
	[3 / 5, '⅗'],
	[5 / 8, '⅝'],
	[2 / 3, '⅔'],
	[3 / 4, '¾'],
	[4 / 5, '⅘'],
	[5 / 6, '⅚'],
	[7 / 8, '⅞'],
]
 
// scale a quantity to the chosen portions, rendering fractional parts as glyphs (e.g. 1.5 → "1 ½")
export function formatQuantity(quantity: number, scale = 1): string {
	const value = quantity * scale
	// for larger amounts, plain digits read better than mixed-fraction glyphs
	if (value <= 5) {
		const whole = Math.floor(value)
		const frac = value - whole
		const glyph = FRACTIONS.find(([f]) => Math.abs(frac - f) < 0.02)?.[1]
		if (glyph) return whole > 0 ? `${whole} ${glyph}` : glyph
	}
	// round to 2 decimals and drop trailing zeros
	return String(Math.round(value * 100) / 100)
}
 
export interface RecipeCategory {
	id: string
	label: string
	defaultTags: string[]
}
 
export interface RecipeTag {
	id: string
	label: string
	categoryId: string
	/** Tag ids that are shown once this tag is selected */
	reveals: string[]
}
 
export const facets: RecipeCategory[] = recipeTags.facets
export const tags: RecipeTag[] = recipeTags.tags
export const tagsById: Map<string, RecipeTag> = new Map(tags.map((tag) => [tag.id, tag]))