Sign in

    vicoproplus/moon-humanize/src/humanize does not have a README file

    NumberFormatter

    pub trait NumberFormatter {
    fn scientific_localized(value : Double, precision~ : Int, exponent~ : Int) -> String
    }
    Trait for locale-aware number formatting. Concrete number types can implement scientific to produce a localized scientific-notation string. The free scientific(value : Double, ~precision, ~exponent, ~locale) function below is the default, generic implementation.

    Date

    pub(all) struct Date {
    year : Int
    month : Int
    day : Int
    }
    A calendar date (proleptic Gregorian).

    Date::today

    fn Date::today() -> Date
    Current local calendar day, derived from now() (nanosecond epoch).

    DateTime

    pub struct DateTime {
    year : Int
    month : Int
    day : Int
    hour : Int
    minute : Int
    second : Int
    }
    An absolute date-time. Declared to mirror python-humanize's naturaltime(when=datetime) reference point. NOTE: MoonBit's TimeInput only models relative durations (seconds/delta), so when does not currently change output for those inputs (Python also cancels when for timedelta/float inputs). Full effect requires an absolute-datetime TimeInput variant, deferred (YAGNI).

    Locale

    pub(all) enum Locale {
    Ar
    BnBD
    CaES
    DaDK
    DeDE
    ElGR
    Eo
    EsES
    Eu
    FaIR
    FiFI
    FrFR
    HeIL
    HuHU
    IdID
    ItIT
    JaJP
    KoKR
    Lv
    Nb
    NlNL
    PlPL
    PtBR
    PtPT
    RuRU
    SiLK
    SkSK
    SlSI
    SvSE
    Tlh
    TrTR
    UkUA
    Uz
    ViVN
    ZhCN
    ZhHK
    En
    Zh
    }
    A supported humanize locale.

    The variant name is the raw locale directory name under src/humanize/locale/ (e.g. zh_CN, pt_BR, fr_FR). This mirrors python-humanize, where activation is performed by locale directory name.

    Locale::from_string

    fn Locale::from_string(locale : String) -> Locale?
    Parse a raw locale identifier (e.g. "zh_CN", "en_GB", "fr_FR") into a Locale. Returns None if the locale is unknown or an English variant (English locales use no translation, matching python-humanize behaviour).

    Locale::to_dir

    fn Locale::to_dir(self : Locale) -> String
    The locale directory name for this Locale (used to look up the right translation table in i18n_data.mbt).

    MsgId

    pub struct MsgId {
    template : String
    }
    A message id already resolved to its translated template string for a locale. format substitutes positional %s placeholders.

    MsgId::format

    fn MsgId::format(self : MsgId, args : Array[String]) -> String
    Substitute positional %s placeholders in the template with args.

    MsgId::to_string

    fn MsgId::to_string(self : MsgId) -> String
    The raw resolved template (no substitution).

    MsgKey

    pub(all) enum MsgKey {
    ERR
    SEC
    MIN
    HR
    DAY
    WK
    NOW
    RECENT
    FUTURE
    PAST
    SCItmpl
    EXPword1
    EXPword2
    EXPword3
    EXPword4
    EXPword5
    EXPword6
    }
    Logical identifiers for humanize messages that participate in localized formatting. msg_str resolves one (given a Locale) to a MsgId whose format method substitutes positional %s placeholders.

    Not every locale ships every key (only en/ru_RU/zh/zh_CN currently carry the scientific-notation keys). msg_str falls back to the English catalog, then to the raw key string, so formatting is always total.

    Rational

    pub struct Rational {
    numer : Int
    denom : Int
    }
    A normalized fraction numer / denom with denom > 0. The sign (if any) is carried by numer.

    Rational::denominator

    fn Rational::denominator(self : Rational) -> Int
    The denominator (always > 0).

    Rational::from_float_approx

    fn Rational::from_float_approx(x : Double, max_den : Int) -> Rational
    Best rational approximation of x with denominator <= max_den, using a brute-force search over denominators (same result as Python's Fraction.limit_denominator because we minimize |p/q - x| and prefer smaller q on ties).

    Rational::new

    fn Rational::new(numer : Int, denom : Int) -> Rational
    Construct a normalized Rational from integer numerator/denominator.

    Rational::numerator

    fn Rational::numerator(self : Rational) -> Int
    The numerator (sign carried here).

    TimeDelta

    pub struct TimeDelta {
    days : Int
    seconds : Int
    microseconds : Int
    is_neg : Bool
    }

    TimeInput

    pub enum TimeInput {
    Seconds(Double)
    Delta(TimeDelta)
    }
    Dual input for the time humanizers: a number of seconds or an explicit delta. Construct via the from_seconds / from_delta factory functions (pub enum constructors are not individually exported in this toolchain).

    TimeInput::from_delta

    fn TimeInput::from_delta(d : TimeDelta) -> TimeInput
    Public entry point: build a TimeInput from an explicit delta.

    TimeInput::from_seconds

    fn TimeInput::from_seconds(s : Double) -> TimeInput
    Public entry point: build a TimeInput from a number of seconds.

    TimeUnit

    pub enum TimeUnit {
    MICROSECONDS
    MILLISECONDS
    SECONDS
    MINUTES
    HOURS
    DAYS
    MONTHS
    YEARS
    }
    Enumeration of the supported time units, ordered from smallest to largest.

    TimeUnit::days

    fn TimeUnit::days() -> TimeUnit

    TimeUnit::hours

    fn TimeUnit::hours() -> TimeUnit

    TimeUnit::microseconds

    fn TimeUnit::microseconds() -> TimeUnit

    TimeUnit::milliseconds

    fn TimeUnit::milliseconds() -> TimeUnit

    TimeUnit::minutes

    fn TimeUnit::minutes() -> TimeUnit

    TimeUnit::months

    fn TimeUnit::months() -> TimeUnit

    TimeUnit::seconds

    fn TimeUnit::seconds() -> TimeUnit

    TimeUnit::years

    fn TimeUnit::years() -> TimeUnit

    activate

    fn activate(locale : String) -> Locale?
    Activate a locale by raw identifier.

    None or any en* locale disables translation (equivalent to deactivate()), matching python-humanize's activate(None). Returns the resulting active locale (None means no translation).

    apnumber

    fn apnumber(value : String) -> String
    Convert an integer (given as String) to AP-style words for 0-9, else the decimal string. Mirrors python-humanize apnumber.

    clamp

    fn clamp(value : Double, floor? : Double?, ceil? : Double?, floor_token? : String, ceil_token? : String, format? : (Double) -> String) -> String
    Clamp value into [floor, ceil], formatting with format and prepending floor_token/ceil_token when clamped. Mirrors python-humanize clamp. None / non-finite inputs are not representable as a String here; non-finite values return their Inf / NaN string form (Python returns None/Inf).

    current_locale

    fn current_locale() -> Locale?
    Return the currently active locale, or None for no translation.

    deactivate

    fn deactivate() -> Unit
    Deactivate internationalisation (revert to English / no translation).

    decimal_sep_for

    fn decimal_sep_for(loc : Locale) -> String

    decimal_separator

    fn decimal_separator() -> String
    Decimal separator for the active locale (defaults to ".").

    default_clamp_fmt

    fn default_clamp_fmt(x : Double) -> String

    format_fixed

    fn format_fixed(value : Double, digits : Int) -> String
    Format a Double with fixed digits after the decimal point, rounding half-up — approximating Python's "%.Nf". MoonBit's Double::to_string does not support precision specifiers, so this is a self-implemented helper (known porting gap R1). A trailing ".0" is kept (callers decide whether to strip it).

    fractional

    fn fractional(value : String) -> String
    Convert a float/number (given as String) to a human-readable fraction or mixed fraction. Mirrors python-humanize fractional.

    get_translation

    fn get_translation() -> Locale?
    Return the current translation handle (the active Locale), or None for no translation (English). Mirrors python-humanize's humanize.i18n.get_translation() which returns the active locale's NullTranslations object (or a fresh NullTranslations() when none is set).

    gettext

    fn gettext(message : String) -> String
    Translate message for the active locale. Falls back to the original English message when no translation is active or none is found.

    intcomma

    fn intcomma(value : String, ndigits? : Int?) -> String
    Add thousands separators to a numeric string, optionally rounding to ndigits decimals. Mirrors python-humanize intcomma(value, ndigits=None).

    integer_to_words

    fn integer_to_words(n : Int) -> String
    Spell out an integer in English cardinal words, e.g. 123 -> "one hundred and twenty-three".

    Negative numbers are prefixed with "negative". Locale-specific spelling (e.g. Chinese 读数) is out of scope for the initial implementation; this returns English regardless of the active locale.

    intword

    fn intword(value : String, format? : String) -> String
    Convert a large integer (given as String) to a friendly magnitude, e.g. "1.0 million". Mirrors python-humanize intword(value, format="%.1f").

    lookup_context

    fn lookup_context(loc : Locale, ctx : String, msg : String, default : String) -> String

    lookup_message

    fn lookup_message(loc : Locale, msg : String, default : String) -> String

    lookup_plural

    fn lookup_plural(loc : Locale, singular : String, _plural : String, form : Int, default : String) -> String

    metric

    fn metric(value : Double, unit? : String, precision? : Int) -> String
    Format a magnitude with an SI metric prefix. Mirrors python-humanize metric(value, unit="", precision=3).

    msg_str

    fn msg_str(locale : Locale, id : MsgKey) -> MsgId
    Resolve a message key to its translated template string in locale, falling back to the English catalog and finally to the msgid itself.

    natural_list

    fn natural_list(value : ArrayView[String], style? : String, cx? : String, ox? : String) -> String
    Humanize a collection of items into a natural-language enumeration.

    • value is the list of items (each rendered with to_string).
    • style selects the connector: "standard" (and), "or", or "and".
    • cx (default ", ") separates items; ox (default " ") separates the final item from the connector.

    naturaldate

    fn naturaldate(value : Date, when? : Date) -> String
    Like naturalday, but append a year for dates more than ~five months away from when (Python's abs(value - when).days >= 5 * 365 / 12 rule).

    naturalday

    fn naturalday(value : Date, when? : Date, format? : String) -> String
    Humanize a date relative to when. Returns today/tomorrow/yesterday or a formatted date string.

    naturaldelta

    fn naturaldelta(value : TimeInput, months? : Bool, minimum_unit? : TimeUnit) -> String
    Humanize a time delta into a short, natural-language string.

    naturalsize

    fn naturalsize(value : Double, binary? : Bool, gnu? : Bool, suffix? : String, format? : String, symbols? : String?) -> String
    Humanize a byte count into a friendly size string.

    • value is the number of bytes (a Double or Int).
    • binary selects binary (1024-based) units when true and symbols is None.
    • gnu selects GNU units (1024-based, space-free, e.g. "300B", "2.9K") — mirrors python-humanize's gnu=True exactly, including the quirk that the suffix is only shown below one unit (ls -sh style).
    • suffix is appended after the unit symbol (default "B").
    • format is a Python-style format string (default "%.1f"); passing "%.0f" mirrors python-humanize's format=True (no fractional digits).
    • symbols selects the unit set: None, "decimal", "binary", or "gnu".

    naturaltime

    fn naturaltime(value : TimeInput, future? : Bool, months? : Bool, minimum_unit? : TimeUnit, when? : DateTime?) -> String
    Humanize a time relative to a reference point. For TimeInput::Delta the tense follows the sign of the delta (negative → "from now"); for TimeInput::Seconds the value is rounded to the nearest second (matching Python) and rendered in the past tense by default.

    ngettext

    fn ngettext(singular : String, plural : String, num : Int) -> String
    Select the correct plural form for num given the active locale's plural rules, returning the translated plural string. Falls back to plural for English / no translation.

    now

    fn now() -> Int64
    Current time in nanoseconds since the Unix epoch.

    ordinal

    fn ordinal(value : String) -> String
    Convert an integer (given as String) to its English ordinal, e.g. "1st". Mirrors python-humanize ordinal (gender ignored, uses male form).

    pgettext

    fn pgettext(msgctxt : String, message : String) -> String
    Translate message with a context (msgctxt), allowing duplicate source strings to map to different translations. Mirrors python-humanize's pgettext.

    plural_index

    fn plural_index(loc : Locale, n : Int) -> Int
    Select the gettext plural form index for n.

    precisedelta

    fn precisedelta(value : TimeInput, minimum_unit? : TimeUnit, format? : String, suppress? : Array[TimeUnit]) -> String
    Humanize a time delta in full detail, combining several units (e.g. "5 days, 4 hours, 3 seconds and 2 microseconds").

    scientific

    fn scientific(value : String, precision? : Int) -> String
    Render a number (given as String) in scientific notation "z.wq x 10ⁿ". Mirrors python-humanize scientific(value, precision=2).

    scientific_localized

    fn scientific_localized(value : Double, precision? : Int, exponent? : Int, locale? : Locale) -> String
    Format a real number in scientific notation, localized via the i18n catalog.

    Uses the scientific_tmpl message ("%s x 10^%s" for en, "%s × 10^%s" for ru_RU, "%s×10^%s" for zh/zh_CN) and substitutes the mantissa and the exponent (as a plain decimal number) into the localized connector. The exponent is always rendered as digits (e.g. 10^3); only the connector between mantissa and 10^ is localized.

    superscript

    let superscript : Map[Char, Char]
    Minimal superscript map used by scientific to render "x 10ⁿ". Mirrors Python's str.maketrans based _SUPERSCRIPT_TRANS.

    thousands_sep_for

    fn thousands_sep_for(loc : Locale) -> String

    thousands_separator

    fn thousands_separator() -> String
    Thousands separator for the active locale (defaults to ",").

    timedelta

    fn timedelta(days? : Int, seconds? : Int, microseconds? : Int) -> TimeDelta
    Build a TimeDelta from explicit days/seconds/microseconds, normalized the same way as Python's datetime.timedelta.

    to_decimal

    fn to_decimal(value : Double, decimals? : Int) -> String
    Format a number with grouped thousands and a fixed number of decimals, e.g. to_decimal(1234567.891, decimals=2) == "1,234,567.89".

    to_scientific

    fn to_scientific(value : Double, precision? : Int, exponent? : Int) -> String
    Convert a real number to scientific notation as a plain (locale-neutral) string, e.g. to_scientific(3.14159, 2, 3) == "3.14 x 10^3".

    This is the building block used by the localized scientific overload.

    to_superscript

    fn to_superscript(s : String) -> String
    Translate a string into superscript form using superscript.