feat: add .toDate() and .toTime() — Malagasy date/time formatting (v1.2.0) - #10
Merged
Conversation
.toDate(input, options?)
- Short (default): "25 Desambra 2022"
- Long: "dimy amby roapolo Desambra, taona roa amby roapolo sy roa arivo"
- Accepts string | Date | number; ISO date strings parsed via regex to
avoid UTC midnight timezone drift from new Date("YYYY-MM-DD")
- Validates ISO calendar range (month 1-12, day 1-31)
.toTime(input, options?)
- 5 time-of-day periods: maraina, antoandro, tolakandro, hariva, alina
- Configurable precision: "minutes" (default) | "seconds"
- Midnight correctly maps to "roa ambin'ny folo ora alina"
- Accepts HH:MM, HH:MM:SS, ISO datetime, Date, Unix timestamp (ms)
Refactor: Tanisa class -> src/tanisa.ts; pure functions in src/date.ts
and src/time.ts; shared inputToNativeDate() in src/utils.ts;
src/converter.ts is now the barrel entry point.
Input validation hardening:
- toWords: reject scientific-notation strings ("1e15" -> TypeError)
- toWords: reject negative fractional inputs ("-0.5" -> RangeError)
- toDate: throw TypeError for out-of-range ISO month/day
- toTime: lower-bound guards and pure-digit check before parseInt
- tests/toWords.test.ts — 73 tests (extracted from converter.test.ts) - tests/toDate.test.ts — 25 tests (new) - tests/toTime.test.ts — 26 tests (new) Total: 124 tests across 3 files (was 71 in 1 file). Each file covers one public method and is independently runnable.
- README: full API docs for .toDate() and .toTime() with examples, time-periods table, options tables, and error-handling section - CHANGELOG: [1.2.0] entry with Added / Fixed / Changed sections - package.json: version 1.1.5 -> 1.2.0, updated description
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two new public methods to the
Tanisaclass, hardens existing input validation, and refactors the source into focused modules.Changes by feature
.toDate(input, options?)Converts any
string | Date | numberto Malagasy date format..toTime(input, options?)Converts any
string | Date | numberto Malagasy spoken time with the correct time-of-day period.precision: 'minutes' | 'seconds'Refactor — separation of concerns
src/converter.ts— monolithic Tanisa classsrc/tanisa.ts— Tanisa class onlysrc/date.ts— pure date parse/format functionssrc/time.ts— pure time parse/format functionssrc/utils.ts— sharedinputToNativeDate()helpersrc/converter.tssrc/converter.ts— barrel entry pointtests/converter.test.ts(71 tests)tests/toWords.test.ts+tests/toDate.test.ts+tests/toTime.test.ts(124 tests)Input validation hardening
toWords('1e15')'iray'(silent wrong result)TypeErrortoWords('-0.5')'aotra faingo dimy'(silent wrong result)RangeErrortoDate('2024-13-01')'01 undefined 2024'TypeErrortoTime('-1:30')RangeErrorTypeErrortoTime('23abc:00')23:00TypeErrorTest plan
npx vitest run— 124 tests pass (was 71)eslint)