Skip to content
GitHub

Installation

Get started with mdg-utils

mdg-utils gives you Malagasy number words, Madagascar phone numbers, Ariary amounts and dates. Install only the modules you need — each one is a small package with zero dependencies, ESM + CommonJS + types.

Each module is its own package: @mdgascar/words, @mdgascar/phone, @mdgascar/ariary and @mdgascar/date. They run in Node.js 22.18+, Deno, Bun and every modern browser.

  1. Add the module you need. This example uses phone numbers.

    Terminal
    npm install @mdgascar/phone
  2. Every module exports plain functions. Read a number written in any common way and format it.

    signup.ts
    import { formatPhoneNumber, parsePhoneNumber } from '@mdgascar/phone';
    const phone = parsePhoneNumber('034 12 345 67');
    phone.operator; // 'yas'
    formatPhoneNumber('0341234567', 'international'); // '+261 34 12 345 67'
  3. parse… functions throw an MdgError. Use safeParse… to get the error code without throwing, or tryParse… to get undefined.

    signup.ts
    import { safeParsePhoneNumber, tryParsePhoneNumber } from '@mdgascar/phone';
    const result = safeParsePhoneNumber('031 12 345 67');
    if (!result.success) result.error.code; // 'UNKNOWN_OPERATOR'
    tryParsePhoneNumber('not a number'); // undefined
  4. Error codes are the same in every language, so a form can show the same message whether the check ran in the browser or on a PHP server.

    signup.ts
    import { MdgError, parsePhoneNumber } from '@mdgascar/phone';
    try {
    parsePhoneNumber(userInput);
    } catch (error) {
    if (error instanceof MdgError) console.log(error.code); // 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | …
    }