API JavaScript
@mdgascar/number-words
Ce contenu n’est pas encore disponible dans votre langue.
Malagasy number words. Part of malagasy-intl by Mdgascar.
Classes
Section titled “Classes”MalagasyIntlError
Section titled “MalagasyIntlError”Error thrown by malagasy-intl functions. Check code, which is the same in every language.
Example
Section titled “Example”try { spell(-1);} catch (error) { if (error instanceof MalagasyIntlError && error.code === 'OUT_OF_RANGE') { // handle it }}Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MalagasyIntlError(
code,message):MalagasyIntlError
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:"MalagasyIntlError"='MalagasyIntlError'
Always 'MalagasyIntlError'.
Overrides
Section titled “Overrides”Error.name
Methods
Section titled “Methods”[hasInstance]()
Section titled “[hasInstance]()”
static[hasInstance](value):boolean
Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.
Parameters
Section titled “Parameters”unknown
Any value, usually a caught error.
Returns
Section titled “Returns”boolean
true when value is an malagasy-intl 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”ParseWordsOptions
Section titled “ParseWordsOptions”Options for parseWords.
Example
Section titled “Example”const options: ParseWordsOptions = { as: 'text' };Properties
Section titled “Properties”
readonlyoptionalas?:ParseResultKind
number (default), or text for the exact decimal digits of any size.
SpellOptions
Section titled “SpellOptions”Options for spell and spellOrdinal.
Example
Section titled “Example”const options: SpellOptions = { system: 'traditional' };Properties
Section titled “Properties”system?
Section titled “system?”
readonlyoptionalsystem?:NumberSystem
standard (default) or traditional.
Type Aliases
Section titled “Type Aliases”ErrorCode
Section titled “ErrorCode”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.
NumberSystem
Section titled “NumberSystem”NumberSystem =
"standard"|"traditional"
How large numbers are named: standard (default; telopolo tapitrisa for 30 000 000) or
traditional, with one name per power of ten (telo safatsiroa).
Example
Section titled “Example”const system: NumberSystem = 'traditional';ParsedValue
Section titled “ParsedValue”ParsedValue<
Kind> =Kindextends"text"?string:number
The value a reader gives back for a ParseResultKind: string for text, otherwise
number.
Type Parameters
Section titled “Type Parameters”Kind extends ParseResultKind
Example
Section titled “Example”const digits: ParsedValue<'text'> = '2000';ParseResultKind
Section titled “ParseResultKind”ParseResultKind =
"number"|"text"
What parseWords gives back: a number (default), or the exact decimal text.
Example
Section titled “Example”const exact: ParseResultKind = 'text';SafeParseResult
Section titled “SafeParseResult”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.
Type Parameters
Section titled “Type Parameters”T
Example
Section titled “Example”const result = safeParsePhone(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.14.0"='0.14.0'
Version of the shared spec this code was generated from.
Functions
Section titled “Functions”parseWords()
Section titled “parseWords()”parseWords<
Kind>(text,options?):ParsedValue<Kind>
Reads Malagasy number words back into a number: the exact inverse of spell.
Type Parameters
Section titled “Type Parameters”Kind extends ParseResultKind = "number"
Parameters
Section titled “Parameters”string
Words as spell writes them, in either system. Case, spacing, curly
apostrophes and the older ambin' ny and amby ny do not matter.
options?
Section titled “options?”Optional as: number (default) or text for the exact decimal digits.
Kind
Returns
Section titled “Returns”ParsedValue<Kind>
The number, or its digits as text with { as: 'text' }.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for non-strings or bad options, INVALID_FORMAT for
anything spell does not write, OUT_OF_RANGE above 15 digits, PRECISION_LOSS when a number cannot hold the value exactly.
Example
Section titled “Example”parseWords('valo amby efapolo amby telonjato'); // 348parseWords("telo faingo efatra ambin'ny folo"); // 3.14parseWords('telo safatsiroa'); // 30000000parseWords('iray lavitrisa lavitrisa', { as: 'text' }); // '1000000000000000000'safeParseWords()
Section titled “safeParseWords()”safeParseWords(
text,options?):SafeParseResult<string|number>
Reads Malagasy number words without throwing.
Parameters
Section titled “Parameters”unknown
Words as spell writes them, in either system, in any case and spacing.
options?
Section titled “options?”ParseWordsOptions = {}
Optional as: number (default) or text for the exact digits.
Returns
Section titled “Returns”SafeParseResult<string | number>
{ success: true, data } with the number or text, or { success: false, error }.
Example
Section titled “Example”const result = safeParseWords('roa arivo');if (result.success) result.data; // 2000spell()
Section titled “spell()”spell(
value,options?):string
Writes a number in Malagasy words, smallest part first, with faingo before the decimals.
parseWords reads the words back.
Parameters
Section titled “Parameters”string | number
A number from 0 to Number.MAX_SAFE_INTEGER, whole or decimal, or plain
decimal text such as '2000000000000000.9', read exactly (a number that large has already
lost its decimals to floating point).
options?
Section titled “options?”SpellOptions = {}
Optional system: standard (default) or traditional, which names every
power of ten (telo safatsiroa for 30 000 000).
Returns
Section titled “Returns”string
The number in Malagasy words.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE if value is not a finite number or the options are
unknown, INVALID_FORMAT for text that is not plain decimal (at most 100 characters),
OUT_OF_RANGE if a number is negative or above Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”spell(21); // 'iraika amby roapolo'spell(2026); // 'enina amby roapolo sy roa arivo'spell(3.14); // "telo faingo efatra ambin'ny folo"spell(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'spell('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy'spellOrdinal()
Section titled “spellOrdinal()”spellOrdinal(
value,options?):string
Writes the Malagasy ordinal of a whole number: faha before the cardinal words, fah
before a vowel, and voalohany for 1.
Parameters
Section titled “Parameters”string | number
A whole number from 1 to Number.MAX_SAFE_INTEGER, or digit text such as
'1000000000000000000', read exactly.
options?
Section titled “options?”SpellOptions = {}
Optional system: standard (default) or traditional.
Returns
Section titled “Returns”string
The ordinal: 3 → 'fahatelo'.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for a number with decimals, a value that is not a
number or text, or unknown options; INVALID_FORMAT for text that is not digits;
OUT_OF_RANGE below 1 or above Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”spellOrdinal(1); // 'voalohany'spellOrdinal(3); // 'fahatelo'spellOrdinal(4); // 'fahefatra'spellOrdinal(11); // "fahiraika ambin'ny folo"spellOrdinal(1000); // 'faharivo'tryParseWords()
Section titled “tryParseWords()”tryParseWords(
text,options?):string|number|undefined
Reads Malagasy number words, or gives undefined instead of throwing.
Parameters
Section titled “Parameters”unknown
Words as spell writes them.
options?
Section titled “options?”ParseWordsOptions = {}
Optional as: number (default) or text.
Returns
Section titled “Returns”string | number | undefined
The value, or undefined when parseWords would throw.
Example
Section titled “Example”tryParseWords('roa arivo'); // 2000tryParseWords('roa sy arivo sy'); // undefined