English | Français | Kreyòl Ayisyen
haiti-utils is a lightweight, zero-dependency npm package that provides simple and reliable utilities for working with Haitian geographical data.
- Save Time: Stop searching for and hardcoding lists of departments, communes, or postal codes.
- Reliable Data: Access a complete and structured dataset for all 10 departments.
- Easy to Use: A simple and intuitive API that is fully typed with TypeScript.
- Case-Insensitive: All search functions are case-insensitive for a better developer experience.
npm install haiti-utils
# or
yarn add haiti-utils
# or
bun add haiti-utilsHere are a few examples of how to use the package.
import { getDepartments } from 'haiti-utils';
const departments = getDepartments();
console.log(departments);
// Output: ["Artibonite", "Centre", ...]import { getCommunes } from 'haiti-utils';
// The search is case-insensitive
const communes = getCommunes('Ouest');
console.log(communes);
// Output: ["Arcahaie", "Cabaret", ...]import { getPostalCode } from 'haiti-utils';
// The search is case-insensitive for both department and commune
const postalCode = getPostalCode('Ouest', 'Pétion-Ville');
console.log(postalCode);
// Output: "HT6122"
const notFound = getPostalCode('Nord', 'InvalidCommune');
console.log(notFound);
// Output: nullimport { getAllData } from 'haiti-utils';
const allData = getAllData();
console.log(allData[0].department); // "Artibonite"
console.log(allData[0].communes[0].name); // "Dessalines"import { validatePhoneNumber } from 'haiti-utils';
validatePhoneNumber('+509 34 56 78 90'); // true
validatePhoneNumber('55555555'); // falseimport { formatPhoneNumber } from 'haiti-utils';
// Default format (international)
formatPhoneNumber('34567890'); // "+509 34 56 78 90"
// National format
formatPhoneNumber('+509 34.56.78.90', { format: 'national' }); // "3456-7890"
// Compact format
formatPhoneNumber('34567890', { format: 'compact' }); // "+50934567890"
// Invalid number
formatPhoneNumber('12345'); // nullReturns a list of all department names.
Returns a list of all communes for a given department. The search is case-insensitive. Returns an empty array if the department is not found.
Returns the postal code for a specific commune within a department. The search is case-insensitive for both department and commune. Returns null if not found or not available.
Returns the complete dataset of all departments and their communes.
Validates a Haitian phone number. It handles common formats, including the +509 country code, and ignores spaces or hyphens. Returns true if the number is valid, false otherwise.
formatPhoneNumber(phoneNumber: string, options?: { format: 'international' | 'national' | 'compact' }): string | null
Formats a Haitian phone number into a consistent, readable format. If the number is invalid, it returns null. The default format is international.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
Si ou jwenn pake haiti-utils itil, ou ka sipòte devlopman li.