Skip to content
GitHub

JavaScript API

@mdgascar/ariary

Malagasy Ariary amounts: format, read, words and franc conversion. Part of malagasy-intl by Mdgascar.

Error thrown by malagasy-intl functions. Check code, which is the same in every language.

try {
spell(-1);
} catch (error) {
if (error instanceof MalagasyIntlError && error.code === 'OUT_OF_RANGE') {
// handle it
}
}
  • Error

new MalagasyIntlError(code, message): MalagasyIntlError

Creates an error with a shared spec code.

ErrorCode

Shared spec error code.

string

Human-readable explanation, in English.

MalagasyIntlError

Error.constructor

readonly code: ErrorCode

Why the call failed; the same code in every language.

readonly name: "MalagasyIntlError" = 'MalagasyIntlError'

Always 'MalagasyIntlError'.

Error.name

static [hasInstance](value): boolean

Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.

unknown

Any value, usually a caught error.

boolean

true when value is an malagasy-intl error.

Each package, and its ESM and CommonJS builds, has its own copy of this class, so the check looks at the error’s shape instead of its prototype chain.

Options for the functions that give back an amount.

const options: AmountResultOptions = { as: 'text' };

readonly optional as?: AmountResultKind

number (default), or text for the exact decimal digits.


Layout options for formatAriary and formatFmg.

const options: AriaryFormatOptions = { grouping: 'dot', symbolPosition: 'after' };

readonly optional currencyDisplay?: AriaryCurrencyDisplay

symbol (default, Ar) or the ISO code (MGA).

readonly optional decimal?: AriaryDecimal

Decimal separator: comma (default), or dot for another convention.

AriarySeparatorOptions.decimal

readonly optional grouping?: AriaryGrouping

Thousand separator: space (default), dot, comma for another convention, or none for no separator at all (1500,5; reading then accepts only digits and one decimal mark).

AriarySeparatorOptions.grouping

readonly optional symbolPosition?: AriarySymbolPosition

Symbol before (default) or after the amount.


Options for reading amounts: which grouping and decimal separators the text uses, and what to give back. Without grouping, every Malagasy one is accepted (spaces or dot); decimal defaults to a comma. Say so for text in another convention: { grouping: 'comma', decimal: 'dot' }.

const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };

readonly optional as?: AmountResultKind

number (default), or text for the exact decimal digits of any amount.

readonly optional decimal?: AriaryDecimal

Decimal separator: comma (default), or dot for another convention.

AriarySeparatorOptions.decimal

readonly optional grouping?: AriaryGrouping

Thousand separator: space (default), dot, comma for another convention, or none for no separator at all (1500,5; reading then accepts only digits and one decimal mark).

AriarySeparatorOptions.grouping


Separator options for formatting and reading amounts.

const english: AriarySeparatorOptions = { grouping: 'comma', decimal: 'dot' };

readonly optional decimal?: AriaryDecimal

Decimal separator: comma (default), or dot for another convention.

readonly optional grouping?: AriaryGrouping

Thousand separator: space (default), dot, comma for another convention, or none for no separator at all (1500,5; reading then accepts only digits and one decimal mark).

AmountResultKind = "number" | "text"

What the functions that compute or read an amount give back: a number (default), or the exact decimal text, of any size.

const exact: AmountResultKind = 'text';

AmountValue<Kind> = Kind extends "text" ? string : number

The value an amount function gives back for an AmountResultKind: string for text, otherwise number.

Kind extends AmountResultKind

const digits: AmountValue<'text'> = '1500.5';

AriaryCurrencyDisplay = "symbol" | "code"

How the currency is shown: symbol (default, Ar 1 500) or the ISO 4217 code (MGA 1 500; MGF for old francs).

const display: AriaryCurrencyDisplay = 'code';

AriaryDecimal = "comma" | "dot"

Decimal separator used by formatAriary and the parsers: comma (default) or dot.


AriaryGrouping = "space" | "dot" | "comma" | "none"

Thousand separator used by formatAriary: space (default) or dot.


AriarySymbolPosition = "before" | "after"

Where the currency symbol goes: before (default, Ar 1 500) or after (1 500 Ar).

const position: AriarySymbolPosition = 'after';

ErrorCode = "INVALID_TYPE" | "OUT_OF_RANGE" | "INVALID_FORMAT" | "UNKNOWN_OPERATOR" | "PRECISION_LOSS"

Error code shared by every malagasy-intl language port.

  • INVALID_TYPE: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
  • OUT_OF_RANGE: The input has the right type but lies outside the supported range.
  • INVALID_FORMAT: The input text cannot be read as a value of this module (wrong characters or wrong length).
  • UNKNOWN_OPERATOR: The phone number is well formed but its operator code is not assigned to an operator.
  • PRECISION_LOSS: The value is exact in the text but the port’s number type cannot hold it exactly; ask for the exact text instead.

SafeParseResult<T> = { data: T; success: true; } | { error: MalagasyIntlError; success: false; }

Outcome of a safeParse… function: the value, or the error that would have been thrown.

T

const result = safeParsePhone(input);
if (result.success) console.log(result.data);
else console.log(result.error.code);

const SPEC_VERSION: "0.14.0" = '0.14.0'

Version of the shared spec this code was generated from.

ariaryToFmg<Kind>(amount, options?): AmountValue<Kind>

Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.

Kind extends AmountResultKind = "number"

string | number

Ariary from 0 up, whole or decimal, as a number or decimal text.

Optional as: number (default) or text for the exact digits.

Kind

AmountValue<Kind>

The amount in francs: 0.07 → 0.35, never 0.35000000000000003.

MalagasyIntlError as fmgToAriary.

ariaryToFmg(1500); // 7500

fmgToAriary<Kind>(francs, options?): AmountValue<Kind>

Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.

Kind extends AmountResultKind = "number"

string | number

Francs from 0 up, whole or decimal, as a number or decimal text.

Optional as: number (default) or text for the exact digits.

Kind

AmountValue<Kind>

The amount in ariary: 7501 → 1500.2.

MalagasyIntlError INVALID_TYPE for a value that is not a finite number or text, INVALID_FORMAT for text that is not plain decimal, OUT_OF_RANGE for a negative number, PRECISION_LOSS when a number cannot hold the result exactly.

fmgToAriary(7500); // 1500
fmgToAriary(7501); // 1500.2

formatAriary(amount, options?): string

Writes an Ariary amount the Malagasy way: symbol Ar, digits grouped by no-break spaces.

string | number

Ariary from 0 to Number.MAX_SAFE_INTEGER, whole or decimal, or decimal text such as '1500.25', written exactly.

AriaryFormatOptions = {}

Optional grouping (space, dot or comma), decimal (comma or dot), symbolPosition (before or after) and currencyDisplay (symbol or code). The defaults are the Malagasy way.

string

The formatted amount, e.g. Ar 1 500 or Ar 1 500,5 (the spaces are U+00A0).

MalagasyIntlError INVALID_TYPE for a value that is not a finite number or text, or unknown options; INVALID_FORMAT for text that is not plain decimal; OUT_OF_RANGE for a number outside 0 to Number.MAX_SAFE_INTEGER.

formatAriary(1500); // 'Ar 1 500'
formatAriary(1500, { grouping: 'dot', symbolPosition: 'after' }); // '1.500 Ar'
formatAriary(1500.5); // 'Ar 1 500,5'
formatAriary(1500, { currencyDisplay: 'code' }); // 'MGA 1 500'

formatFmg(francs, options?): string

Writes an amount in old Malagasy francs, with the same layout as formatAriary.

string | number

Francs from 0 to Number.MAX_SAFE_INTEGER, whole or decimal, or decimal text.

AriaryFormatOptions = {}

Same options as formatAriary.

string

The formatted amount, e.g. Fmg 7 500.

MalagasyIntlError as formatAriary.

formatFmg(7500); // 'Fmg 7 500'

parseAriary<Kind>(text, options?): AmountValue<Kind>

Reads an Ariary amount written in any common way.

Kind extends AmountResultKind = "number"

string

An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.

AriarySeparatorOptions & object = {}

Optional grouping and decimal; see AriaryParseOptions.

AmountValue<Kind>

The amount, whole or decimal.

MalagasyIntlError INVALID_TYPE for non-strings or bad options, INVALID_FORMAT for text that is not an amount (signs, a dot as decimal point by default), PRECISION_LOSS when a number cannot hold the amount exactly (pass { as: 'text' } for the exact digits).

parseAriary('Ar 1 500'); // 1500
parseAriary('1.500 Ar'); // 1500
parseAriary('1 500,50 Ar'); // 1500.5
parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5

safeParseAriary(text, options?): SafeParseResult<string | number>

Reads an Ariary amount without throwing.

unknown

An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.

AriaryParseOptions = {}

Optional grouping and decimal; see AriaryParseOptions.

SafeParseResult<string | number>

{ success: true, data } with the amount, or { success: false, error }.

const result = safeParseAriary('1 500 Ar');
if (result.success) result.data; // 1500

spellAriary(amount, options?): string

Writes an Ariary amount in Malagasy words.

string | number

Ariary from 0 to Number.MAX_SAFE_INTEGER, whole or decimal, or plain decimal text such as '1500.25', read exactly.

SpellOptions = {}

The words system: standard (default) or traditional.

string

The amount in words followed by ariary; decimals follow faingo.

MalagasyIntlError INVALID_TYPE, INVALID_FORMAT (text) or OUT_OF_RANGE.

spellAriary(1500); // 'dimanjato sy arivo ariary'
spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'

tryParseAriary(text, options?): string | number | undefined

Reads an Ariary amount, or gives undefined instead of throwing.

Use safeParseAriary when you need to know why the text was rejected.

unknown

An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.

AriaryParseOptions = {}

Optional grouping and decimal; see AriaryParseOptions.

string | number | undefined

The amount, or undefined when parseAriary would throw.

tryParseAriary('1.500 Ar'); // 1500
tryParseAriary('1.5'); // undefined

Re-exports NumberSystem


Re-exports SpellOptions