Cosmo (JavaScript) — API reference
    Preparing search index...

    Class Cosmo

    Index

    Constructors

    • Parameters

      • Optionallocale: string | null

        BCP-47 (or underscore-separated en_AU) locale identifier. Unicode extensions such as -u-nu-latn-ca-buddhist are honoured. Defaults to the runtime locale.

      • modifiers: Modifiers = {}

        Optional calendar, currency, and timeZone overrides.

      Returns Cosmo

    Properties

    locale: string

    Canonical BCP-47 locale identifier, e.g. "en-AU".

    modifiers: Required<Modifiers>

    Resolved modifiers (calendar / currency / timeZone).

    subtags: Subtags

    Parsed language / script / region subtags.

    Methods

    • Returns a new Cosmo with likely subtags added (e.g. "en""en-Latn-US"). Uses Intl.Locale#maximize.

      Returns Cosmo

    • Localised calendar name (e.g. "buddhist""Buddhist Calendar"). Returns "" for an empty argument.

      Parameters

      • code: string

      Returns string

    • Compact number notation (e.g. 1200"1.2K", or "1.2 thousand"). JS-only.

      Parameters

      • value: number
      • width: FormatWidth = "short"

        full/long → long words, otherwise short.

      Returns string

    • Locale-aware comparison of two strings, suitable for Array#sort. Returns a negative number, 0, or a positive number.

      Parameters

      Returns number

    • Locale-aware substring test that honours the locale's collation, so accents/case can be ignored.

      Parameters

      • haystack: string
      • needle: string
      • sensitivity: "base" | "accent" | "case" | "variant" = "base"

        base (ignore case & accents, default), accent, case, or variant (exact). See Intl.Collator.

      • options: CollationOptions = {}

      Returns boolean

    • Localised country/region name (e.g. "AU""Australia"). Accepts a region code or a locale containing one. Returns "" when empty.

      Parameters

      • code: string | null = ...

      Returns string

    • Localised currency name (default) or symbol.

      Parameters

      • code: string | null = ...

        ISO 4217 code; defaults to the currency modifier.

      • symbol: boolean = false

        When true, returns the standard (disambiguated) symbol — e.g. "A$" for AUD in en-US, not the ambiguous narrow "$".

      • strict: boolean = false

        Throw on an unknown currency instead of echoing the code back.

      Returns string

    • Formats a moment range (e.g. "2–5 Feb 2020"). Uses DateTimeFormat#formatRange. JS-only.

      Parameters

      Returns string

    • Text direction of the locale (or a given language): "rtl" or "ltr".

      Parameters

      • language: string | null = ...

      Returns string

    • Generic localised display name — a single entry point over the dedicated lookups. Mirrors Intl.DisplayNames.

      Parameters

      • type: DisplayType

        language, region, script, calendar, or currency.

      • code: string

        The code to translate (e.g. "en", "AU", "Hans", "buddhist", "EUR").

      Returns string

    • Formats an undirected duration (magnitude only) given in seconds. For the directed form (with a past/future orientation, e.g. "3 days ago") see Cosmo.relativeDuration.

      Parameters

      • value: number | DurationParts
      • withWords: boolean = false

        When true, spells out the units ("339 hours, …"), otherwise uses the digital clock form ("339:27:40").

        Requires Intl.DurationFormat (Node 22+). Both forms are produced entirely by ICU.

      Returns string

    • Truncates text to at most max graphemes, breaking on a word boundary and appending ellipsis. Grapheme- and word-aware via Intl.Segmenter, so it never splits a combining sequence. Returns the original text if it already fits.

      Parameters

      • text: string
      • max: number
      • ellipsis: string = "…"

      Returns string

    • Country flag emoji for a region (e.g. "AU""🇦🇺"). Defaults to the locale's region. Uses the Unicode regional-indicator transform, so no data table is involved.

      Parameters

      • country: string | null = ...

      Returns string

    • Joins a list using the locale's conventions (e.g. "A, B, and C"). Mirrors Intl.ListFormat.

      Parameters

      • items: readonly string[]
      • type: ListType = "conjunction"

        conjunction (and, default), disjunction (or), or unit.

      • width: FormatWidth = "full"

        maps to long/short/narrow list styles.

      Returns string

    • Localised language name (e.g. "en""English", in fa"انگلیسی"). Accepts a bare language code or a full locale (the language subtag is used). Returns "" for an empty/nullish argument.

      Parameters

      • code: string | null = ...

      Returns string

    • Locale-aware lower-casing.

      Parameters

      • text: string

      Returns string

    • Formats an ICU MessageFormat pattern (subset: args, number, plural, select).

      Parameters

      Returns string

    • Formats a moment (date and/or time) using the locale's conventions.

      Parameters

      • moment: Moment

        A Date or Unix-millisecond timestamp.

      • dateWidth: FormatWidth = "short"

        none/short/medium/long/full (default short).

      • timeWidth: FormatWidth = "short"

        none/short/medium/long/full (default short).

      • Optionalcalendar: string | null

        Pass "gregorian" to force Gregorian; otherwise the locale/modifier calendar is used.

      Returns string

    • Formats a monetary value.

      No currency is inferred from the region: the Intl API exposes no region→currency mapping, and this library bundles no data. Provide a currency code or set the currency modifier.

      Parameters

      • value: number
      • code: string | null = ...
      • options: { precision?: number; strict?: boolean } & NumberOptions = {}

      Returns string

      The formatted amount, or "" when no currency is available (unless strict, which throws).

    • Formats a monetary range (e.g. "$3.00 – $5.00"). JS-only.

      Parameters

      • start: number
      • end: number
      • code: string | null = ...

      Returns string

      "" when no currency is available (or throws if none and required).

    • Localised month names (January … December), following the active calendar. Mirrors IntlDateFormatter month symbols.

      Parameters

      Returns string[]

    • Formats a numeric range (e.g. "3–5"). Uses NumberFormat#formatRange. JS-only.

      Parameters

      • start: number
      • end: number

      Returns string

    • Formats a fraction as a localised percentage (e.g. 0.2"20%").

      Parameters

      • value: number
      • precision: number = 3

        Maximum fraction digits (default 3).

      • options: NumberOptions = {}

        Optional rounding/grouping controls (NumberOptions); an explicit maximumFractionDigits here overrides precision.

      Returns string

    • The LDML plural category a number falls into for this locale (e.g. 1"one", 2"other" in English). Mirrors the category selection inside ICU MessageFormat.

      Parameters

      • value: number
      • ordinal: boolean = false

        Use ordinal rules (1st/2nd/3rd …) instead of cardinal.

      Returns PluralCategory

    • Renders a directed duration — a signed amount carrying a past/future orientation — in the locale's words (e.g. (-3, "day")"3 days ago", (2, "hour")"in 2 hours"). The directed counterpart of Cosmo.duration, which is undirected (magnitude only). Wraps Intl.RelativeTimeFormat.

      Parameters

      • amount: number

        Signed amount: negative = past ("… ago"), positive = future ("in …").

      • unit: RelativeTimeFormatUnit

        second, minute, hour, day, week, month, quarter, year.

      • numeric: "auto" | "always" = "always"

        always (default, "1 day ago") or auto (allows "yesterday").

      Returns string

    • Renders the directed duration between two moments as relative text, auto-selecting the largest sensible unit (e.g. "in 5 days", "3 days ago"). Computes target − reference, then formats via Cosmo.relativeDuration. JS-only.

      Parameters

      • target: Moment

        The moment being described.

      • reference: Moment = ...

        The moment target is measured against. Defaults to now. When target is after reference the result is future ("in …"); when before, it is past ("… ago").

      • numeric: "auto" | "always" = "auto"

        auto (default, allows "yesterday") or always.

      Returns string

    • Returns a new Cosmo with likely subtags removed (e.g. "en-Latn-US""en"). Uses Intl.Locale#minimize.

      Returns Cosmo

    • Scientific notation (e.g. 12345"1.2345E4"). Mirrors NumberFormatter::SCIENTIFIC.

      Parameters

      • value: number

      Returns string

    • Localised script name (e.g. "Latn""Latin"). Defaults to the locale's script subtag. Returns "" when there is no script.

      Parameters

      • code: string | null = ...

      Returns string

    • Returns a new array sorted by the locale's collation rules.

      Type Parameters

      • T

      Parameters

      • items: readonly T[]
      • Optionalkey: (item: T) => string

        Optional accessor returning the string to sort each item by.

      • options: CollationOptions = {}

        Optional collation tailoring (CollationOptions).

      Returns T[]

    • Splits text into grapheme clusters (user-perceived characters), so combining marks and emoji ZWJ sequences stay intact. Mirrors Intl.Segmenter granularity:"grapheme".

      Parameters

      • text: string

      Returns string[]

    • Splits text into sentences using the locale's sentence-boundary rules.

      Parameters

      • text: string

      Returns string[]

    • Splits text into words using the locale's word-boundary rules, keeping only word-like segments (drops whitespace and punctuation). Mirrors ICU's word BreakIterator.

      Parameters

      • text: string

      Returns string[]

    • The values the runtime's ICU supports for a given key (e.g. all IANA time zones, calendars, currencies). Mirrors Intl.supportedValuesOf.

      Parameters

      • key: SupportedKey

        calendar, collation, currency, numberingSystem, timeZone, or unit.

      Returns string[]

    • Returns a single localised number symbol that the Intl API exposes via formatToParts (decimal/group separators, percent, sign symbols, nan, infinity). Symbols ICU keeps internal (permille, pad escape, …) are not available and throw.

      Parameters

      • name: string

        One of: decimal, group, percent, minusSign, plusSign, nan, infinity, currency (case/_-insensitive; _separator/_symbol suffixes are ignored).

      Returns string

    • Localised display name of a time zone (e.g. "Australian Eastern Standard Time"). Defaults to the timeZone modifier, falling back to the runtime zone.

      Parameters

      • style: TimeZoneNameStyle = "long"

        long (default), short, shortOffset, longOffset, shortGeneric, or longGeneric.

      Returns string

    • Formats a measurement with a localised unit (e.g. 2.19 gigabytes).

      Parameters

      • category: string

        Informational unit category (e.g. "digital"); accepted for descriptive grouping but not required by Intl.

      • unit: string

        The unit identifier, e.g. "gigabyte", "celsius", "gram". Must be one of the units sanctioned by ECMA-402.

      • value: number

        Numeric value.

      • width: FormatWidth = "full"

        full/long → long, medium → short, short → narrow.

      Returns string

      CosmoError if the unit is not supported by Intl.

    • Locale-aware upper-casing (e.g. Turkish dotted/dotless I).

      Parameters

      • text: string

      Returns string

    • Localised weekday names, Sunday first (matching ICU symbol order). Mirrors IntlDateFormatter weekday symbols.

      Parameters

      Returns string[]

    • Week conventions for the locale: first day of the week, weekend days, and the minimal days in the first week. Mirrors IntlCalendar accessors.

      Returns WeekInfo

      CosmoError if the runtime lacks Intl.Locale#getWeekInfo.

    • Builds a Cosmo from an HTTP Accept-Language header, picking the highest-quality tag.

      Parameters

      Returns Cosmo