tanisa
Version:
A utility to convert Malagasy 🇲🇬 numbers, dates, and times into their word representations.
34 lines (33 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDate = parseDate;
exports.formatShortDate = formatShortDate;
exports.formatLongDate = formatLongDate;
const dictionary_1 = require("./dictionary");
const utils_1 = require("./utils");
function parseDate(input) {
if (input instanceof Date || typeof input === 'number') {
const d = (0, utils_1.inputToNativeDate)(input);
return { year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate() };
}
const cleaned = input.replace(/\b(\d+)(th|st|nd|rd)\b/g, '$1');
const isoMatch = cleaned.match(/^(\d{4})-(\d{2})-(\d{2})/);
if (isoMatch) {
const year = +isoMatch[1], month = +isoMatch[2], day = +isoMatch[3];
if (month < 1 || month > 12 || day < 1 || day > 31) {
throw new TypeError(`Invalid date input: "${input}"`);
}
return { year, month, day };
}
const d = new Date(cleaned);
if (isNaN(d.getTime()))
throw new TypeError(`Invalid date input: "${input}"`);
return { year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate() };
}
function formatShortDate(year, month, day) {
return `${String(day).padStart(2, '0')} ${dictionary_1.MalagasyNumerals.MONTHS[month]} ${year}`;
}
function formatLongDate(year, month, day, toWords) {
const dayWord = day === 1 ? dictionary_1.MalagasyNumerals.FIRST_OF_MONTH : toWords(day);
return `${dayWord} ${dictionary_1.MalagasyNumerals.MONTHS[month]}, ${dictionary_1.MalagasyNumerals.YEAR_UNIT} ${toWords(year)}`;
}