UNPKG

tanisa

Version:

A utility to convert Malagasy 🇲🇬 numbers, dates, and times into their word representations.

71 lines (70 loc) • 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseTime = parseTime; exports.getTimePeriod = getTimePeriod; exports.formatTime = formatTime; const dictionary_1 = require("./dictionary"); const utils_1 = require("./utils"); function parseTime(input) { var _a; if (input instanceof Date || typeof input === 'number') { const d = (0, utils_1.inputToNativeDate)(input); return { hours: d.getHours(), minutes: d.getMinutes(), seconds: d.getSeconds(), }; } const timeStr = input.includes('T') ? input.split('T')[1].split(/[-Z+]/)[0] : input; const parts = timeStr.split(':'); if (parts.length < 2) throw new TypeError(`Invalid time input: "${input}"`); const hourStr = parts[0]; const minuteStr = parts[1]; const secondStr = (_a = parts[2]) !== null && _a !== void 0 ? _a : '0'; if (!/^\d+$/.test(hourStr) || !/^\d+$/.test(minuteStr) || !/^\d+$/.test(secondStr)) { throw new TypeError(`Invalid time input: "${input}"`); } const hours = parseInt(hourStr, 10); const minutes = parseInt(minuteStr, 10); const seconds = parseInt(secondStr, 10); if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) { throw new TypeError(`Invalid time input: "${input}"`); } return { hours, minutes, seconds }; } function getTimePeriod(hours) { if (hours >= 1 && hours <= 9) return dictionary_1.MalagasyNumerals.TIME_PERIODS.MARAINA; if (hours >= 10 && hours <= 12) return dictionary_1.MalagasyNumerals.TIME_PERIODS.ANTOANDRO; if (hours >= 13 && hours <= 16) return dictionary_1.MalagasyNumerals.TIME_PERIODS.TOLAKANDRO; if (hours >= 17 && hours <= 19) return dictionary_1.MalagasyNumerals.TIME_PERIODS.HARIVA; return dictionary_1.MalagasyNumerals.TIME_PERIODS.ALINA; } function formatTime(hours, minutes, seconds, precision, toWords) { const period = getTimePeriod(hours); const displayHour = hours === 0 ? 12 : hours > 12 ? hours - 12 : hours; const parts = [ `${toWords(displayHour)} ${dictionary_1.MalagasyNumerals.HOUR_UNIT}`, ]; if (minutes > 0) { parts.push(`${dictionary_1.MalagasyNumerals.CONJ_SY} ${toWords(minutes)} ${dictionary_1.MalagasyNumerals.MINUTE_UNIT}`); } if (seconds > 0 && precision === 'seconds') { parts.push(`${dictionary_1.MalagasyNumerals.CONJ_SY} ${toWords(seconds)} ${dictionary_1.MalagasyNumerals.SECOND_UNIT}`); } parts.push(period); return parts.join(' '); }