Aller au contenu
GitHub

API JavaScript

@mdgascar/number-words

Ce contenu n’est pas encore disponible dans votre langue.

Malagasy number words. 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 parseWords.

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

readonly optional as?: ParseResultKind

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


Options for spell and spellOrdinal.

const options: SpellOptions = { system: 'traditional' };

readonly optional system?: NumberSystem

standard (default) or traditional.

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 = "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).

const system: NumberSystem = 'traditional';

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

The value a reader gives back for a ParseResultKind: string for text, otherwise number.

Kind extends ParseResultKind

const digits: ParsedValue<'text'> = '2000';

ParseResultKind = "number" | "text"

What parseWords gives back: a number (default), or the exact decimal text.

const exact: ParseResultKind = 'text';

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.

parseWords<Kind>(text, options?): ParsedValue<Kind>

Reads Malagasy number words back into a number: the exact inverse of spell.

Kind extends ParseResultKind = "number"

string

Words as spell writes them, in either system. Case, spacing, curly apostrophes and the older ambin' ny and amby ny do not matter.

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

Kind

ParsedValue<Kind>

The number, or its digits as text with { as: 'text' }.

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.

parseWords('valo amby efapolo amby telonjato'); // 348
parseWords("telo faingo efatra ambin'ny folo"); // 3.14
parseWords('telo safatsiroa'); // 30000000
parseWords('iray lavitrisa lavitrisa', { as: 'text' }); // '1000000000000000000'

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

Reads Malagasy number words without throwing.

unknown

Words as spell writes them, in either system, in any case and spacing.

ParseWordsOptions = {}

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

SafeParseResult<string | number>

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

const result = safeParseWords('roa arivo');
if (result.success) result.data; // 2000

spell(value, options?): string

Writes a number in Malagasy words, smallest part first, with faingo before the decimals. parseWords reads the words back.

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).

SpellOptions = {}

Optional system: standard (default) or traditional, which names every power of ten (telo safatsiroa for 30 000 000).

string

The number in Malagasy words.

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.

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(value, options?): string

Writes the Malagasy ordinal of a whole number: faha before the cardinal words, fah before a vowel, and voalohany for 1.

string | number

A whole number from 1 to Number.MAX_SAFE_INTEGER, or digit text such as '1000000000000000000', read exactly.

SpellOptions = {}

Optional system: standard (default) or traditional.

string

The ordinal: 3 → 'fahatelo'.

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.

spellOrdinal(1); // 'voalohany'
spellOrdinal(3); // 'fahatelo'
spellOrdinal(4); // 'fahefatra'
spellOrdinal(11); // "fahiraika ambin'ny folo"
spellOrdinal(1000); // 'faharivo'

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

Reads Malagasy number words, or gives undefined instead of throwing.

unknown

Words as spell writes them.

ParseWordsOptions = {}

Optional as: number (default) or text.

string | number | undefined

The value, or undefined when parseWords would throw.

tryParseWords('roa arivo'); // 2000
tryParseWords('roa sy arivo sy'); // undefined