@subrotosaha/bangla-date
Version:
A simple utility package for string manipulation in JavaScript.
113 lines • 5.74 kB
TypeScript
type NumberInWords = {
[key: number]: string;
};
type NumbersInWords = {
en: NumberInWords;
bn: NumberInWords;
hi: NumberInWords;
};
/**
* Converts an integer to its natural-language word equivalent in English,
* Bengali, or Hindi.
*
* - **English** uses the short-scale system: thousand / million / billion.
* - **Bengali & Hindi** use the South-Asian denomination system:
* হাজার/हज़ार (1,000) → লাখ/लाख (1,00,000) → কোটি/करोड़ (1,00,00,000).
* Crore-level values are handled recursively, so very large numbers such
* as 10,000,000,000 correctly render as "এক হাজার কোটি" / "एक हज़ार करोड़".
*
* @param num - An integer (positive, negative, or zero). Throws if `num`
* is not an integer (i.e. `Number.isInteger(num)` is `false`).
* @param language - Target language for word output.
* - `"en"` (default) — English words, e.g. `"twenty-one"`
* - `"bn"` — Bengali words, e.g. `"একাশ"`
* - `"hi"` — Hindi words, e.g. `"इक्कीस"`
* @returns The word representation as a string.
* @throws {Error} When `num` is not an integer.
*
* @example
* numberToWords(21, 'en'); // "twenty-one"
* numberToWords(21, 'bn'); // "একাশ"
* numberToWords(21, 'hi'); // "इक्कीस"
* numberToWords(1000, 'en'); // "one thousand"
* numberToWords(100000, 'bn'); // "এক লাখ"
* numberToWords(-5, 'en'); // "-five"
* numberToWords(0, 'bn'); // "শূন্য"
*/
export declare const numberToWords: (num: number, language?: keyof NumbersInWords) => string;
/**
* Replaces every ASCII digit (0–9) in a number or string with the
* equivalent digit character in the target language/script.
*
* This is the low-level utility used by all `BanglaDate` formatting and
* output methods to localise numeric output. Passing `"en"` is a no-op
* (digits remain 0–9).
*
* @param num - The value to localise. Accepts a `number` or a `string`
* that may contain digits anywhere (e.g. formatted date strings, time
* strings, ISO strings). Non-digit characters are passed through unchanged.
* @param language - Target script for digit substitution.
* - `"en"` (default) — ASCII digits (no change)
* - `"bn"` — Bengali digits ০১২৩৪৫৬৭৮৯
* - `"hi"` — Devanagari digits ०१२३४५६७८९
* @returns The input with all ASCII digits replaced by the target script's digits.
*
* @example
* numberToNumber(2025, 'bn'); // "২০২৫"
* numberToNumber('14/4/1432', 'bn'); // "১৪/৪/১৪৩২"
* numberToNumber('08:30:00', 'hi'); // "०८:८०:००"
* numberToNumber(42, 'en'); // "42" (no change)
*/
export declare const numberToNumber: (num: number | string, language?: keyof NumbersInWords) => string;
/**
* Timezone offsets in fractional hours (e.g. 5.5 = UTC+5:30).
* Ambiguous abbreviations are resolved toward the most common / South-Asian context:
* IST → India Standard Time (+5:30) not Ireland (+1) or Israel (+2)
* AST → Arabia Standard Time (+3) not Atlantic (-4)
* CST → Central Standard Time (-6, US/Canada) — use CST_CN alias for China (+8)
* GST → Gulf Standard Time (+4) not South Georgia (-2) — use GST_SG alias
* AMT → Amazon Time (-4) not Armenia (+4)
*/
export declare const TIME_ZONE_OFFSETS: Record<string, number>;
/**
* Replaces Gregorian numeric fields inside a pre-formatted locale string
* with the corresponding Bangla calendar values.
*
* This was used by `BanglaDate.toLocaleString()` / `toLocaleDateString()` to
* substitute the year, month, and day in an `Intl.DateTimeFormat` output
* string. Since v1.7 `BanglaDate._applyLocale()` uses
* `Intl.DateTimeFormat.formatToParts()` directly and no longer calls this
* helper, but the function remains exported for any external callers.
*
* @deprecated Since v1.7.0 \u2014 `BanglaDate._applyLocale` now uses
* `Intl.DateTimeFormat.formatToParts()` internally. This function may be
* removed in the next major release. Migrate to `BanglaDate.format()` or
* `BanglaDate.toLocaleString()` instead.
*
* @param banglaDateStr - An ISO-like string in the form
* `\"YYYY-MM-DDThh:mm:ss.mmmZ\"` where `YYYY-MM-DD` are Bangla calendar
* values (as produced by `BanglaDate.toISOString()`).
* @param templateStr - A locale-formatted date string (e.g. from
* `Intl.DateTimeFormat`) that contains the Gregorian values to be replaced.\n * May optionally contain a timezone label (e.g. `\"GMT+6\"`, `\"BST\"`)
* to localise the time component.
* @param gregorianRef - Optional. The original Gregorian `Date` that was
* used to generate `templateStr`. When supplied, replacement uses exact
* field matching to avoid false positives on ambiguous substrings.
* @returns A new string equal to `templateStr` with Gregorian year, month,
* day, and time fields replaced by their Bangla equivalents.
* @throws {Error} If `banglaDateStr` cannot be parsed (missing `YYYY-MM-DD`
* prefix or missing time component).
*
* @example
* // Convert a Gregorian locale string to Bangla calendar values:
* const bd = BanglaDate.fromBanglaDate(1432, 1, 14);
* formatBanglaDateToMatchTemplate(
* bd.toISOString(), // \"1432-01-14T00:00:00.000Z\"
* '4/14/2025, 12:00:00 AM', // Gregorian en-US formatted string
* bd.toGregorian()
* );
* // => '1/14/1432, 12:00:00 AM'
*/
export declare function formatBanglaDateToMatchTemplate(banglaDateStr: string, templateStr: string, gregorianRef?: Date): string;
export {};
//# sourceMappingURL=index.d.ts.map