API
@agape/string

quantify Function

Formats a number with a unit, automatically pluralizing the unit based on the count.

API

function quantify(  count: string | number,  unit: string,  plural?: string): string

quantify

string

Formats a number with a unit, automatically pluralizing the unit based on the count.

@paramcount:  string | number
Number of units, as a number or numeric string.
@paramunit:  string
Label for the singular unit (e.g., "apple").
@paramplural:  string
Optional plural label. If not provided, the unit is pluralized automatically.
@returnsstring

Description

Formats a number with a unit, automatically pluralizing the unit based on the count.

If the count is exactly 1, the singular unit is used. Otherwise, it either uses the provided plural form or calls toPlural(unit) to generate one.

Usage

quantify(1, "apple"); // "1 apple"
quantify(3, "apple"); // "3 apples"
quantify("02", "box"); // "02 boxes"
quantify(1, "child", "children"); // "1 child"
quantify(2, "child", "children"); // "2 children"
quantify(5, "CPU"); // "5 CPUs"