API JavaScript
@mdgascar/ariary
Ce contenu n’est pas encore disponible dans votre langue.
Malagasy Ariary amounts: format, read, words and franc conversion. Part of mdg-utils by Mdgascar.
Classes
Section titled “Classes”MdgError
Section titled “MdgError”Error thrown by mdg-utils functions. Check code, which is the same in every language.
Example
Section titled “Example”try { numberToWords(-1);} catch (error) { if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') { // handle it }}Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MdgError(
code,message):MdgError
Creates an error with a shared spec code.
Parameters
Section titled “Parameters”Shared spec error code.
message
Section titled “message”string
Human-readable explanation, in English.
Returns
Section titled “Returns”Overrides
Section titled “Overrides”Error.constructor
Properties
Section titled “Properties”
readonlycode:ErrorCode
Why the call failed; the same code in every language.
readonlyname:"MdgError"='MdgError'
Always 'MdgError'.
Overrides
Section titled “Overrides”Error.name
Methods
Section titled “Methods”[hasInstance]()
Section titled “[hasInstance]()”
static[hasInstance](value):boolean
Makes instanceof MdgError true for errors from any mdg-utils package or build.
Parameters
Section titled “Parameters”unknown
Any value, usually a caught error.
Returns
Section titled “Returns”boolean
true when value is an mdg-utils error.
Remarks
Section titled “Remarks”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.
Interfaces
Section titled “Interfaces”AriaryFormatOptions
Section titled “AriaryFormatOptions”Layout options for formatAriary and formatFmg.
Example
Section titled “Example”const options: AriaryFormatOptions = { grouping: 'dot', symbolPosition: 'after' };Extends
Section titled “Extends”Properties
Section titled “Properties”decimal?
Section titled “decimal?”
readonlyoptionaldecimal?:AriaryDecimal
Decimal separator: comma (default), or dot for another convention.
Inherited from
Section titled “Inherited from”AriarySeparatorOptions.decimal
grouping?
Section titled “grouping?”
readonlyoptionalgrouping?:AriaryGrouping
Thousand separator: space (default), dot, or comma for another convention.
Inherited from
Section titled “Inherited from”AriarySeparatorOptions.grouping
symbolPosition?
Section titled “symbolPosition?”
readonlyoptionalsymbolPosition?:AriarySymbolPosition
Symbol before (default) or after the amount.
AriarySeparatorOptions
Section titled “AriarySeparatorOptions”Separator options for formatting and reading amounts.
Example
Section titled “Example”const english: AriarySeparatorOptions = { grouping: 'comma', decimal: 'dot' };Extended by
Section titled “Extended by”Properties
Section titled “Properties”decimal?
Section titled “decimal?”
readonlyoptionaldecimal?:AriaryDecimal
Decimal separator: comma (default), or dot for another convention.
grouping?
Section titled “grouping?”
readonlyoptionalgrouping?:AriaryGrouping
Thousand separator: space (default), dot, or comma for another convention.
Type Aliases
Section titled “Type Aliases”AriaryDecimal
Section titled “AriaryDecimal”AriaryDecimal =
"comma"|"dot"
Decimal separator used by formatAriary and the parsers: comma (default) or dot.
AriaryGrouping
Section titled “AriaryGrouping”AriaryGrouping =
"space"|"dot"|"comma"
Thousand separator used by formatAriary: space (default) or dot.
AriaryParseOptions
Section titled “AriaryParseOptions”AriaryParseOptions =
AriarySeparatorOptions
Options for reading amounts: which grouping and decimal separators the text uses.
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' }.
Example
Section titled “Example”const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };AriarySymbolPosition
Section titled “AriarySymbolPosition”AriarySymbolPosition =
"before"|"after"
Where the currency symbol goes: before (default, Ar 1 500) or after (1 500 Ar).
Example
Section titled “Example”const position: AriarySymbolPosition = 'after';ErrorCode
Section titled “ErrorCode”ErrorCode =
"INVALID_TYPE"|"OUT_OF_RANGE"|"INVALID_FORMAT"|"UNKNOWN_OPERATOR"
Error code shared by every mdg-utils 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 a mobile operator.
SafeParseResult
Section titled “SafeParseResult”SafeParseResult<
T> = {data:T;success:true; } | {error:MdgError;success:false; }
Outcome of a safeParse… function: the value, or the error that would have been thrown.
Type Parameters
Section titled “Type Parameters”T
Example
Section titled “Example”const result = safeParsePhoneNumber(input);if (result.success) console.log(result.data);else console.log(result.error.code);Variables
Section titled “Variables”SPEC_VERSION
Section titled “SPEC_VERSION”
constSPEC_VERSION:"0.6.0"='0.6.0'
Version of the shared spec this code was generated from.
Functions
Section titled “Functions”ariaryToFmg()
Section titled “ariaryToFmg()”ariaryToFmg(
amount):number
Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
Parameters
Section titled “Parameters”amount
Section titled “amount”number
Ariary from 0 up, whole or decimal.
Returns
Section titled “Returns”number
The amount in francs: 0.07 → 0.35, never 0.35000000000000003.
Throws
Section titled “Throws”MdgError INVALID_TYPE if amount is not a finite number, OUT_OF_RANGE when
it is negative or the francs would exceed Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”ariaryToFmg(1500); // 7500ariaryToFmg(0.07); // 0.35ariaryToWords()
Section titled “ariaryToWords()”ariaryToWords(
amount):string
Writes an Ariary amount in Malagasy words.
Parameters
Section titled “Parameters”amount
Section titled “amount”number
Ariary from 0 to Number.MAX_SAFE_INTEGER, whole or decimal.
Returns
Section titled “Returns”string
The amount in words followed by ariary; decimals follow faingo.
Throws
Section titled “Throws”MdgError INVALID_TYPE or OUT_OF_RANGE.
Example
Section titled “Example”ariaryToWords(1500); // 'dimanjato sy arivo ariary'ariaryToWords(1500.5); // 'dimanjato sy arivo faingo dimy ariary'fmgToAriary()
Section titled “fmgToAriary()”fmgToAriary(
francs):number
Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
Parameters
Section titled “Parameters”francs
Section titled “francs”number
Francs from 0 to Number.MAX_SAFE_INTEGER, whole or decimal.
Returns
Section titled “Returns”number
The amount in ariary: 7501 → 1500.2.
Throws
Section titled “Throws”MdgError INVALID_TYPE if francs is not a finite number, OUT_OF_RANGE when
it is negative or above Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”fmgToAriary(7500); // 1500fmgToAriary(7501); // 1500.2formatAriary()
Section titled “formatAriary()”formatAriary(
amount,options?):string
Writes an Ariary amount the Malagasy way: symbol Ar, digits grouped by no-break spaces.
Parameters
Section titled “Parameters”amount
Section titled “amount”number
Ariary from 0 to Number.MAX_SAFE_INTEGER, whole or decimal.
options?
Section titled “options?”AriaryFormatOptions = {}
Optional grouping (space, dot or comma), decimal (comma or dot)
and symbolPosition (before or after). The defaults are the Malagasy way.
Returns
Section titled “Returns”string
The formatted amount, e.g. Ar 1 500 or Ar 1 500,5 (the spaces are U+00A0).
Throws
Section titled “Throws”MdgError INVALID_TYPE for a value that is not a finite number or unknown options,
OUT_OF_RANGE outside 0 to Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”formatAriary(1500); // 'Ar 1 500'formatAriary(1500, { grouping: 'dot', symbolPosition: 'after' }); // '1.500 Ar'formatAriary(1500.5); // 'Ar 1 500,5'formatFmg()
Section titled “formatFmg()”formatFmg(
francs,options?):string
Writes an amount in old Malagasy francs, with the same layout as formatAriary.
Parameters
Section titled “Parameters”francs
Section titled “francs”number
Francs from 0 to Number.MAX_SAFE_INTEGER, whole or decimal.
options?
Section titled “options?”AriaryFormatOptions = {}
Same options as formatAriary.
Returns
Section titled “Returns”string
The formatted amount, e.g. Fmg 7 500.
Throws
Section titled “Throws”MdgError INVALID_TYPE or OUT_OF_RANGE, as formatAriary.
Example
Section titled “Example”formatFmg(7500); // 'Fmg 7 500'parseAriary()
Section titled “parseAriary()”parseAriary(
text,options?):number
Reads an Ariary amount written in any common way.
Parameters
Section titled “Parameters”string
An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.
options?
Section titled “options?”Optional grouping and decimal; see AriaryParseOptions.
Returns
Section titled “Returns”number
The amount, whole or decimal.
Throws
Section titled “Throws”MdgError 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), OUT_OF_RANGE above
Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”parseAriary('Ar 1 500'); // 1500parseAriary('1.500 Ar'); // 1500parseAriary('1 500,50 Ar'); // 1500.5parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5safeParseAriary()
Section titled “safeParseAriary()”safeParseAriary(
text,options?):SafeParseResult<number>
Reads an Ariary amount without throwing.
Parameters
Section titled “Parameters”unknown
An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.
options?
Section titled “options?”Optional grouping and decimal; see AriaryParseOptions.
Returns
Section titled “Returns”SafeParseResult<number>
{ success: true, data } with the amount, or { success: false, error }.
Example
Section titled “Example”const result = safeParseAriary('1 500 Ar');if (result.success) result.data; // 1500tryParseAriary()
Section titled “tryParseAriary()”tryParseAriary(
text,options?):number|undefined
Reads an Ariary amount, or gives undefined instead of throwing.
Use safeParseAriary when you need to know why the text was rejected.
Parameters
Section titled “Parameters”unknown
An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.
options?
Section titled “options?”Optional grouping and decimal; see AriaryParseOptions.
Returns
Section titled “Returns”number | undefined
The amount, or undefined when parseAriary would throw.
Example
Section titled “Example”tryParseAriary('1.500 Ar'); // 1500tryParseAriary('1.5'); // undefined