Language

Errors

Invalid input throws a typed error you can check with instanceof. Validation helpers like isValidBsDate return false instead of throwing.

ErrorExtendsThrown whengo-bs equivalent
InvalidBSDateErrorRangeErrorA BS year, month or day is out of range or not an integer (including NaN, Infinity and fractions). field says which: "year", "month" or "day".ErrInvalidYear / ErrInvalidMonth / ErrInvalidDay
DateOutOfRangeErrorRangeErrorAn AD date, or the result of addBsDays/subtractBsDays, falls outside the supported range.ErrOutOfRange
BSDateFormatErrorSyntaxErrorparseBsDate gets a string not shaped like "YYYY-MM-DD". input holds the string.ErrInvalidFormat
InvalidDateOrderErrorRangeErrorgetBsAge's birth is after its today.ErrInvalidDateOrder
RangeErroraddBsDays/subtractBsDays gets a day count that isn't a safe integer, or a weekday-name function gets a weekday outside 0–6.— / ErrInvalidWeekday
TypeErrorAn argument has the wrong type: not a Date, an Invalid Date, not an object, not a string.

Checking for a specific error

import { InvalidBSDateError, bsToAd } from "bikram-sambat-ts";
 
try {
  bsToAd({ year: 2083, month: 13, day: 1 });
} catch (error) {
  if (error instanceof InvalidBSDateError) {
    error.field; // "month"
    error.message; // "bs: invalid month: 13"
  }
}

Because every error extends RangeError, SyntaxError or TypeError, code that only checks the built-in classes still works. If your app somehow loads both the ESM and the CommonJS build of the package, their error classes are distinct and instanceof across them fails; checking error.name (e.g. "InvalidBSDateError") works either way.