diff --git a/README.md b/README.md index 6a4331d..5d3cf42 100644 --- a/README.md +++ b/README.md @@ -135,13 +135,17 @@ You can set individual components of a `NepaliDate` object using the following m #### Formatting the Nepali date You can format a `NepaliDate` object as a string using the `format()` and `formatNepali()` methods. +Additionally, you can convert the corresponding English date to a string using the `formatEnglishDate()` and `formatEnglishDateInNepali()` methods. - `format(formatStr)`: Returns a string representation (in English) of the `NepaliDate` object in the specified format. -- `formatNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari) script of the `NepaliDate` object in the specified format. +- `formatNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari script) of the `NepaliDate` object in the specified format. +- `formatEnglishDate(formatStr)`: Returns a string representation (in English) of the English Date in the specified format. +- `formatEnglishDateInNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari script) of the English Date in the specified format. ```javascript const now = new NepaliDate(2079, 5, 3, 16, 14) console.log(now.format('YYYY-MM-DD hh:mm A')) // Outputs: 2079-06-03 04:14 PM +console.log(now.formatEnglishDate('YYYY-MM-DD hh:mm A')) // Outputs: 2022-08-19 04:14 PM ``` The date formatting will follow the format codes mentioned below, which are similar to the date formats used in day.js. diff --git a/src/NepaliDate.ts b/src/NepaliDate.ts index b6ff959..c861da2 100644 --- a/src/NepaliDate.ts +++ b/src/NepaliDate.ts @@ -1,5 +1,12 @@ import dateConverter from './dateConverter' -import { format, formatNepali, nepaliDateToString } from './format' +import { + format, + formatEnglishDate, + formatEnglishDateInNepali, + formatNepali, + nepaliDateToString, +} from './format' + import { parse, parseFormat } from './parse' import { getDate, getNepalDateAndTime } from './utils' import { validateTime } from './validators' @@ -556,7 +563,7 @@ class NepaliDate { /** * Returns a string representation in the Nepali (Devanagari) of the NepaliDate object in the specified format. * @param formatStr The format string for the desired output. - * @returns {string} A string representation of the NepaliDate object in the specified format. + * @returns {string} The formatted Date string in Nepali (Devanagari). */ formatNepali(formatStr: string): string { return formatNepali(this, formatStr) @@ -571,6 +578,25 @@ class NepaliDate { return nepaliDateToString(this) } + /** + * Returns a string representation (in English) of the English Date in the specified format. + * + * @param {string} formatStr - The format string specifying the desired format. + * @returns {string} The formatted Date string. + */ + formatEnglishDate(formatStr: string): string { + return formatEnglishDate(this, formatStr) + } + + /** + * Returns a string representation in the Nepali (Devanagari) of the English Date in the specified format. + * @param formatStr The format string for the desired output. + * @returns {string} The formatted Date string in Nepali (Devanagari). + */ + formatEnglishDateInNepali(formatStr: string): string { + return formatEnglishDateInNepali(this, formatStr) + } + /* Static methods */ /** diff --git a/src/constants.ts b/src/constants.ts index 71fb509..d3f2fae 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -11,7 +11,7 @@ export const OLD_UTC_OFFSET_IN_MS = 19800000 // 5 hours 40 minutes in ms export const TIMEZONE_TRANSITION_TIMESTAMP = 504901800000 export const TIMEZONE_TRANSITION_DATE_REFERENCE = new Date(1986, 0, 1, 0, 15) -export const MONTHS_EN = [ +export const NEPALI_MONTHS_EN = [ 'Baisakh', 'Jestha', 'Asar', @@ -26,7 +26,7 @@ export const MONTHS_EN = [ 'Chaitra', ] -export const MONTHS_SHORT_EN = [ +export const NEPALI_MONTHS_SHORT_EN = [ 'Bai', 'Jes', 'Asa', @@ -41,7 +41,7 @@ export const MONTHS_SHORT_EN = [ 'Cha', ] -export const MONTHS_NP = [ +export const NEPALI_MONTHS_NE = [ 'बैशाख', 'जेठ', 'असार', @@ -56,7 +56,7 @@ export const MONTHS_NP = [ 'चैत्र', ] -export const MONTHS_SHORT_NP = [ +export const NEPALI_MONTHS_SHORT_NE = [ 'बै', 'जे', 'अ', @@ -71,7 +71,67 @@ export const MONTHS_SHORT_NP = [ 'चै', ] -export const NUM_NP = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'] +export const ENGLISH_MONTHS_EN = [ + 'January', + 'February', + 'March', + 'April', + 'May', + 'June', + 'July', + 'August', + 'September', + 'October', + 'November', + 'December', +] + +export const ENGLISH_MONTHS_SHORT_EN = [ + 'Jan', + 'Feb', + 'Mar', + 'Apr', + 'May', + 'Jun', + 'Jul', + 'Aug', + 'Sep', + 'Oct', + 'Nov', + 'Dec', +] + +export const ENGLISH_MONTHS_NE = [ + 'जनवरी', + 'फेब्रुअरी', + 'मार्च', + 'अप्रिल', + 'मे', + 'जुन', + 'जुलाई', + 'अगस्ट', + 'सेप्टेम्बर', + 'अक्टोबर', + 'नोभेम्बर', + 'डिसेम्बर', +] + +export const ENGLISH_MONTHS_SHORT_NE = [ + 'जन', + 'फेब', + 'मार', + 'अप्रि', + 'मे', + 'जुन', + 'जुला', + 'अग', + 'सेप', + 'अक्टो', + 'नोभे', + 'डिसे', +] + +export const NUM_NE = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'] export const WEEKDAYS_SHORT_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] @@ -85,9 +145,9 @@ export const WEEKDAYS_LONG_EN = [ 'Saturday', ] -export const WEEKDAYS_SHORT_NP = ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि'] +export const WEEKDAYS_SHORT_NE = ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि'] -export const WEEKDAYS_LONG_NP = [ +export const WEEKDAYS_LONG_NE = [ 'आइतबार', 'सोमबार', 'मंगलबार', diff --git a/src/format.ts b/src/format.ts deleted file mode 100644 index 8ac69b2..0000000 --- a/src/format.ts +++ /dev/null @@ -1,299 +0,0 @@ -import { - LOCALE_EN, - LOCALE_NE, - MONTHS_EN, - MONTHS_NP, - MONTHS_SHORT_EN, - MONTHS_SHORT_NP, - NUM_NP, - WEEKDAYS_LONG_EN, - WEEKDAYS_LONG_NP, - WEEKDAYS_SHORT_EN, - WEEKDAYS_SHORT_NP, -} from './constants' -import { parseFormatTokens } from './utils' - -interface NepaliDate { - getYear: () => number - getMonth: () => number - getDate: () => number - getDay: () => number - getHours: () => number - getMinutes: () => number - getSeconds: () => number - getMilliseconds: () => number -} - -type Locale = typeof LOCALE_EN | typeof LOCALE_NE - -interface Formatter { - (nepaliDate: NepaliDate, locale: Locale): string -} - -/* Helper functions */ - -/** - * Pads a number with a leading zero if it is less than 10. - * - * Output: 1 => 01, 11 => 11 - * - * @param value - The number to be padded. - * @returns The padded number as a string. - */ -const zeroPad = (value: number) => value.toString().padStart(2, '0') - -/** - * Pads a number with a leading zero if it is less than 100. - * - * Output: 1 => 001, 11 => 011, 111 => 111 - * - * @param value - The number to be padded. - * @returns The padded number as a string. - */ -const millisecondZeroPad = (value: number) => value.toString().padStart(3, '0') - -/** - * Converts English digits to Nepali digits (Devanagari script). - * - * @param {string} str - English digits in string format. - * @returns {string} Nepali digits in string format. - */ -const npDigit = (str: string): string => { - return str - .split('') - .map(chr => NUM_NP[chr.charCodeAt(0) - 48]) - .join('') -} - -/** - * Returns a localized number (digit) in string format. - * Converts to Nepali digits for Nepali localization. - * - * @param {string | number} obj - The string or number to be localized. - * @param {Locale} locale - The locale specifying the localization (e.g., 'en' or 'ne'). - * @returns {string} The localized number in string format. - */ -const localizedNumberString = (obj: string | number, locale: Locale): string => { - const objInString = typeof obj === 'string' ? obj : String(obj) - if (locale !== LOCALE_NE) { - return objInString - } - return npDigit(objInString) -} - -/* FORMATTERS */ - -const halfYear: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getYear(), locale).substring(2) -} - -const fullYear: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getYear(), locale) -} - -const monthNumber: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getMonth() + 1, locale) -} - -const monthTwoDigit: Formatter = (nepaliDate, locale) => { - return localizedNumberString(zeroPad(nepaliDate.getMonth() + 1), locale) -} - -const monthAbbrName: Formatter = (nepaliDate, locale) => { - if (locale === LOCALE_NE) { - return MONTHS_SHORT_NP[nepaliDate.getMonth()] - } - return MONTHS_SHORT_EN[nepaliDate.getMonth()] -} - -const monthFullName: Formatter = (nepaliDate, locale) => { - if (locale === LOCALE_NE) { - return MONTHS_NP[nepaliDate.getMonth()] - } - return MONTHS_EN[nepaliDate.getMonth()] -} - -const dayNumber: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getDate(), locale) -} - -const dayTwoDigit: Formatter = (nepaliDate, locale) => { - return localizedNumberString(zeroPad(nepaliDate.getDate()), locale) -} - -const weekDayNumber: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getDay(), locale) -} - -const weekDayShortName: Formatter = (nepaliDate, locale) => { - if (locale === LOCALE_NE) { - return WEEKDAYS_SHORT_NP[nepaliDate.getDay()] - } - return WEEKDAYS_SHORT_EN[nepaliDate.getDay()] -} - -const weekDayFullName: Formatter = (nepaliDate, locale) => { - if (locale === LOCALE_NE) { - return WEEKDAYS_LONG_NP[nepaliDate.getDay()] - } - return WEEKDAYS_LONG_EN[nepaliDate.getDay()] -} - -const hour24Number: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getHours(), locale) -} - -const hour24TwoDigit: Formatter = (nepaliDate, locale) => { - return localizedNumberString(zeroPad(nepaliDate.getHours()), locale) -} - -const hour12Number: Formatter = (nepaliDate, locale) => { - const hour24 = nepaliDate.getHours() - const hour = hour24 > 12 ? hour24 - 12 : hour24 - return localizedNumberString(hour, locale) -} - -const hour12TwoDigit: Formatter = (nepaliDate, locale) => { - const hour24 = nepaliDate.getHours() - const hour = hour24 > 12 ? hour24 - 12 : hour24 - return localizedNumberString(zeroPad(hour), locale) -} - -const minuteNumber: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getMinutes(), locale) -} - -const minuteTwoDigit: Formatter = (nepaliDate, locale) => { - return localizedNumberString(zeroPad(nepaliDate.getMinutes()), locale) -} - -const secondNumber: Formatter = (nepaliDate, locale) => { - return localizedNumberString(nepaliDate.getSeconds(), locale) -} - -const secondTwoDigit: Formatter = (nepaliDate, locale) => { - return localizedNumberString(zeroPad(nepaliDate.getSeconds()), locale) -} - -const millisecondThreeDigit: Formatter = (nepaliDate, locale) => { - return localizedNumberString( - millisecondZeroPad(nepaliDate.getMilliseconds()), - locale - ) -} - -const amPmUpperCase: Formatter = (nepaliDate, locale) => { - if (locale === LOCALE_NE) { - /** - * The output of this method is yet to be decided. - * Further discussion are needed for this method. - * - * The most common words used in Nepal are below: - * - बिहान - * - मध्यान्ह - * - दिउसो - * - बेलुका - * - रात - */ - return 'A' - } - return nepaliDate.getHours() >= 12 ? 'PM' : 'AM' -} - -const amPmLowerCase: Formatter = (nepaliDate, locale) => { - if (locale === LOCALE_NE) { - return 'a' - } - return nepaliDate.getHours() >= 12 ? 'pm' : 'am' -} - -/* Formatters mapping and implementations */ - -const TOKENS_TO_FORMATTER: { [key: string]: Formatter } = { - YY: halfYear, - YYYY: fullYear, - M: monthNumber, - MM: monthTwoDigit, - MMM: monthAbbrName, - MMMM: monthFullName, - D: dayNumber, - DD: dayTwoDigit, - d: weekDayNumber, - dd: weekDayShortName, - ddd: weekDayShortName, - dddd: weekDayFullName, - H: hour24Number, - HH: hour24TwoDigit, - h: hour12Number, - hh: hour12TwoDigit, - m: minuteNumber, - mm: minuteTwoDigit, - s: secondNumber, - ss: secondTwoDigit, - SSS: millisecondThreeDigit, - A: amPmUpperCase, - a: amPmLowerCase, -} - -/** - * Converts a Nepali date object to a formatted string. - * - * @param {NepaliDate} nepaliDate - The Nepali date object to be formatted. - * @param {string} format - The format string to specify the desired output format. - * @param {Locale} locale - The locale specifying the localization (e.g., 'en' or 'ne'). - * @returns {string} The formatted date in string format. - */ -const formatDate = (nepaliDate: NepaliDate, format: string, locale: Locale): string => { - const tokens = parseFormatTokens(format) - const formatToken = (token: string) => { - if (!(token in TOKENS_TO_FORMATTER)) { - return token - } - return TOKENS_TO_FORMATTER[token](nepaliDate, locale) - } - return tokens.map(formatToken).join('') -} - -/** - * Returns a string representation (in English) of the NepaliDate object in the specified format. - * - * @param {NepaliDate} nepaliDate - The Nepali date object to be formatted. - * @param {string} format - The format string for the desired output. - * @returns {string} - The formatted Nepali date string. - */ -export const format = (nepaliDate: NepaliDate, format: string): string => { - return formatDate(nepaliDate, format, LOCALE_EN) -} - -/** - * Returns a string representation in the Nepali (Devanagari) of the NepaliDate object in the specified format. - * - * @param {NepaliDate} nepaliDate - The Nepali date object to be formatted. - * @param {string} format - The format string for the desired output. - * @returns {string} - A string representation of the NepaliDate object in the specified format. - */ -export const formatNepali = (nepaliDate: NepaliDate, format: string): string => { - return formatDate(nepaliDate, format, LOCALE_NE) -} - -/** - * Converts a NepaliDate object to a toString() representation. - * Returns in format "YYYY-MM-DD HH:mm:ss[.SSS]". - * This method is lightweight compared to the format/formatNepali method. - * - * @param {NepaliDate} nepaliDate - The NepaliDate object to be converted. - * @returns {string} The formatted string representation of the NepaliDate. - */ -export const nepaliDateToString = (nepaliDate: NepaliDate): string => { - const year = zeroPad(nepaliDate.getYear()) - const month = zeroPad(nepaliDate.getMonth() + 1) - const date = zeroPad(nepaliDate.getDate()) - const hours = zeroPad(nepaliDate.getHours()) - const minutes = zeroPad(nepaliDate.getMinutes()) - const seconds = zeroPad(nepaliDate.getSeconds()) - const milliseconds = nepaliDate.getMilliseconds() - const millisecondString = - milliseconds === 0 ? '' : `.${millisecondZeroPad(milliseconds)}` - - return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}${millisecondString}` -} diff --git a/src/format/format.ts b/src/format/format.ts new file mode 100644 index 0000000..6ef8638 --- /dev/null +++ b/src/format/format.ts @@ -0,0 +1,122 @@ +import { LOCALE_EN, LOCALE_NE } from '../constants' +import { parseFormatTokens } from '../utils' +import { + amPmLowerCase, + amPmUpperCase, + Formatter, + fullNepaliYear, + halfNepaliYear, + hour12Number, + hour12TwoDigit, + hour24Number, + hour24TwoDigit, + INepaliDate, + Locale, + millisecondThreeDigit, + millisecondZeroPad, + minuteNumber, + minuteTwoDigit, + nepaliDayNumber, + nepaliDayTwoDigit, + nepaliMonthAbbrName, + nepaliMonthFullName, + nepaliMonthNumber, + nepaliMonthTwoDigit, + secondNumber, + secondTwoDigit, + weekDayFullName, + weekDayNumber, + weekDayShortName, + zeroPad, +} from './tokenFormatters' + +const TOKENS_TO_FORMATTER: { [key: string]: Formatter } = { + YY: halfNepaliYear, + YYYY: fullNepaliYear, + M: nepaliMonthNumber, + MM: nepaliMonthTwoDigit, + MMM: nepaliMonthAbbrName, + MMMM: nepaliMonthFullName, + D: nepaliDayNumber, + DD: nepaliDayTwoDigit, + d: weekDayNumber, + dd: weekDayShortName, + ddd: weekDayShortName, + dddd: weekDayFullName, + H: hour24Number, + HH: hour24TwoDigit, + h: hour12Number, + hh: hour12TwoDigit, + m: minuteNumber, + mm: minuteTwoDigit, + s: secondNumber, + ss: secondTwoDigit, + SSS: millisecondThreeDigit, + A: amPmUpperCase, + a: amPmLowerCase, +} + +/** + * Converts a Nepali date object to a formatted string. + * + * @param {INepaliDate} nepaliDate - The Nepali date object to be formatted. + * @param {string} format - The format string to specify the desired output format. + * @param {Locale} locale - The locale specifying the localization (e.g., 'en' or 'ne'). + * @returns {string} The formatted date in string format. + */ +const formatDate = ( + nepaliDate: INepaliDate, + format: string, + locale: Locale +): string => { + const tokens = parseFormatTokens(format) + const formatToken = (token: string) => { + if (!(token in TOKENS_TO_FORMATTER)) { + return token + } + return TOKENS_TO_FORMATTER[token](nepaliDate, locale) + } + return tokens.map(formatToken).join('') +} + +/** + * Returns a string representation (in English) of the NepaliDate object in the specified format. + * + * @param {INepaliDate} nepaliDate - The Nepali date object to be formatted. + * @param {string} format - The format string for the desired output. + * @returns {string} - The formatted Nepali date string. + */ +export const format = (nepaliDate: INepaliDate, format: string): string => + formatDate(nepaliDate, format, LOCALE_EN) + +/** + * Returns a string representation in the Nepali (Devanagari) of the NepaliDate object in the specified format. + * + * @param {INepaliDate} nepaliDate - The Nepali date object to be formatted. + * @param {string} format - The format string for the desired output. + * @returns {string} - A string representation of the NepaliDate object in the specified format. + */ +export const formatNepali = (nepaliDate: INepaliDate, format: string): string => + formatDate(nepaliDate, format, LOCALE_NE) + +/** + * Converts a NepaliDate object to a toString() representation. + * Returns in format "YYYY-MM-DD HH:mm:ss[.SSS]". + * This method is lightweight compared to the format/formatNepali method. + * + * @param {INepaliDate} nepaliDate - The NepaliDate object to be converted. + * @returns {string} The formatted string representation of the NepaliDate. + */ +export const nepaliDateToString = (nepaliDate: INepaliDate): string => { + const year = zeroPad(nepaliDate.getYear()) + const month = zeroPad(nepaliDate.getMonth() + 1) + const date = zeroPad(nepaliDate.getDate()) + const hours = zeroPad(nepaliDate.getHours()) + const minutes = zeroPad(nepaliDate.getMinutes()) + const seconds = zeroPad(nepaliDate.getSeconds()) + const milliseconds = nepaliDate.getMilliseconds() + const millisecondString = + milliseconds === 0 ? '' : `.${millisecondZeroPad(milliseconds)}` + + return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}${millisecondString}` +} diff --git a/src/format/formatEnglishDate.ts b/src/format/formatEnglishDate.ts new file mode 100644 index 0000000..350d61d --- /dev/null +++ b/src/format/formatEnglishDate.ts @@ -0,0 +1,102 @@ +import { LOCALE_EN, LOCALE_NE } from '../constants' +import { parseFormatTokens } from '../utils' +import { + amPmLowerCase, + amPmUpperCase, + englishDayNumber, + englishDayTwoDigit, + englishMonthAbbrName, + englishMonthFullName, + englishMonthNumber, + englishMonthTwoDigit, + Formatter, + fullEnglishYear, + halfEnglishYear, + hour12Number, + hour12TwoDigit, + hour24Number, + hour24TwoDigit, + INepaliDate, + Locale, + millisecondThreeDigit, + minuteNumber, + minuteTwoDigit, + secondNumber, + secondTwoDigit, + weekDayFullName, + weekDayNumber, + weekDayShortName, +} from './tokenFormatters' + +const TOKENS_TO_FORMATTER: { [key: string]: Formatter } = { + YY: halfEnglishYear, + YYYY: fullEnglishYear, + M: englishMonthNumber, + MM: englishMonthTwoDigit, + MMM: englishMonthAbbrName, + MMMM: englishMonthFullName, + D: englishDayNumber, + DD: englishDayTwoDigit, + d: weekDayNumber, + dd: weekDayShortName, + ddd: weekDayShortName, + dddd: weekDayFullName, + H: hour24Number, + HH: hour24TwoDigit, + h: hour12Number, + hh: hour12TwoDigit, + m: minuteNumber, + mm: minuteTwoDigit, + s: secondNumber, + ss: secondTwoDigit, + SSS: millisecondThreeDigit, + A: amPmUpperCase, + a: amPmLowerCase, +} + +/** + * Converts a NepaliDate object's English Date to a formatted string. + * + * @param {INepaliDate} nepaliDate - The Nepali date object to be formatted. + * @param {string} format - The format string to specify the desired output format. + * @param {Locale} locale - The locale specifying the localization (e.g., 'en' or 'ne'). + * @returns {string} The formatted date in string format. + */ +const formatDate = ( + nepaliDate: INepaliDate, + format: string, + locale: Locale +): string => { + const tokens = parseFormatTokens(format) + const formatToken = (token: string) => { + if (!(token in TOKENS_TO_FORMATTER)) { + return token + } + return TOKENS_TO_FORMATTER[token](nepaliDate, locale) + } + return tokens.map(formatToken).join('') +} + +/** + * Returns a string representation (in English) of the English Date from NepaliDate object + * in the specified format. + * + * @param {INepaliDate} nepaliDate - The Nepali date object to be formatted. + * @param {string} format - The format string for the desired output. + * @returns {string} - The formatted Nepali date string. + */ +export const formatEnglishDate = (nepaliDate: INepaliDate, format: string): string => + formatDate(nepaliDate, format, LOCALE_EN) + +/** + * Returns a string representation in the Nepali (Devanagari) of English Date from the + * NepaliDate object in the specified format. + * + * @param {INepaliDate} nepaliDate - The Nepali date object to be formatted. + * @param {string} format - The format string for the desired output. + * @returns {string} - A string representation of the NepaliDate object in the specified format. + */ +export const formatEnglishDateInNepali = ( + nepaliDate: INepaliDate, + format: string +): string => formatDate(nepaliDate, format, LOCALE_NE) diff --git a/src/format/index.ts b/src/format/index.ts new file mode 100644 index 0000000..4b1be97 --- /dev/null +++ b/src/format/index.ts @@ -0,0 +1,2 @@ +export { format, formatNepali, nepaliDateToString } from './format' +export { formatEnglishDate, formatEnglishDateInNepali } from './formatEnglishDate' diff --git a/src/format/tokenFormatters.ts b/src/format/tokenFormatters.ts new file mode 100644 index 0000000..f9598e0 --- /dev/null +++ b/src/format/tokenFormatters.ts @@ -0,0 +1,220 @@ +import { + ENGLISH_MONTHS_EN, + ENGLISH_MONTHS_NE, + ENGLISH_MONTHS_SHORT_EN, + ENGLISH_MONTHS_SHORT_NE, + LOCALE_EN, + LOCALE_NE, + NEPALI_MONTHS_EN, + NEPALI_MONTHS_NE, + NEPALI_MONTHS_SHORT_EN, + NEPALI_MONTHS_SHORT_NE, + NUM_NE, + WEEKDAYS_LONG_EN, + WEEKDAYS_LONG_NE, + WEEKDAYS_SHORT_EN, + WEEKDAYS_SHORT_NE, +} from '../constants' + +export interface INepaliDate { + getDateObject(): Date + getYear(): number + getEnglishYear(): number + getMonth(): number + getEnglishMonth(): number + getDate(): number + getEnglishDate(): number + getDay(): number + getHours(): number + getMinutes(): number + getSeconds(): number + getMilliseconds(): number + getTime(): number +} + +export type Locale = typeof LOCALE_EN | typeof LOCALE_NE + +export interface Formatter { + (nepaliDate: INepaliDate, locale: Locale): string +} + +/* Helper functions */ + +/** + * Pads a number with a leading zero if it is less than 10. + * + * Output: 1 => 01, 11 => 11 + * + * @param value - The number to be padded. + * @returns The padded number as a string. + */ +export const zeroPad = (value: number) => value.toString().padStart(2, '0') + +/** + * Pads a number with a leading zero if it is less than 100. + * + * Output: 1 => 001, 11 => 011, 111 => 111 + * + * @param value - The number to be padded. + * @returns The padded number as a string. + */ +export const millisecondZeroPad = (value: number) => value.toString().padStart(3, '0') + +/** + * Converts English digits to Nepali digits (Devanagari script). + * + * @param {string} str - English digits in string format. + * @returns {string} Nepali digits in string format. + */ +const npDigit = (str: string): string => { + return str + .split('') + .map(chr => NUM_NE[chr.charCodeAt(0) - 48]) + .join('') +} + +/** + * Returns a localized number (digit) in string format. + * Converts to Nepali digits for Nepali localization. + * + * @param {string | number} obj - The string or number to be localized. + * @param {Locale} locale - The locale specifying the localization (e.g., 'en' or 'ne'). + * @returns {string} The localized number in string format. + */ +const localizedNumberString = (obj: string | number, locale: Locale): string => { + const objInString = typeof obj === 'string' ? obj : String(obj) + if (locale !== LOCALE_NE) { + return objInString + } + return npDigit(objInString) +} + +/* FORMATTERS */ + +export const halfNepaliYear: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getYear(), locale).substring(2) + +export const fullNepaliYear: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getYear(), locale) + +export const halfEnglishYear: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getEnglishYear(), locale).substring(2) + +export const fullEnglishYear: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getEnglishYear(), locale) + +export const nepaliMonthNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getMonth() + 1, locale) + +export const nepaliMonthTwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getMonth() + 1), locale) + +export const nepaliMonthAbbrName: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return NEPALI_MONTHS_SHORT_NE[nepaliDate.getMonth()] + } + return NEPALI_MONTHS_SHORT_EN[nepaliDate.getMonth()] +} + +export const nepaliMonthFullName: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return NEPALI_MONTHS_NE[nepaliDate.getMonth()] + } + return NEPALI_MONTHS_EN[nepaliDate.getMonth()] +} + +export const englishMonthNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getEnglishMonth() + 1, locale) + +export const englishMonthTwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getEnglishMonth() + 1), locale) + +export const englishMonthAbbrName: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return ENGLISH_MONTHS_SHORT_NE[nepaliDate.getEnglishMonth()] + } + return ENGLISH_MONTHS_SHORT_EN[nepaliDate.getEnglishMonth()] +} + +export const englishMonthFullName: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return ENGLISH_MONTHS_NE[nepaliDate.getEnglishMonth()] + } + return ENGLISH_MONTHS_EN[nepaliDate.getEnglishMonth()] +} + +export const nepaliDayNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getDate(), locale) + +export const nepaliDayTwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getDate()), locale) + +export const englishDayNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getEnglishDate(), locale) + +export const englishDayTwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getEnglishDate()), locale) + +export const weekDayNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getDay(), locale) + +export const weekDayShortName: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return WEEKDAYS_SHORT_NE[nepaliDate.getDay()] + } + return WEEKDAYS_SHORT_EN[nepaliDate.getDay()] +} + +export const weekDayFullName: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return WEEKDAYS_LONG_NE[nepaliDate.getDay()] + } + return WEEKDAYS_LONG_EN[nepaliDate.getDay()] +} + +export const hour24Number: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getHours(), locale) + +export const hour24TwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getHours()), locale) + +export const hour12Number: Formatter = (nepaliDate, locale) => { + const hour24 = nepaliDate.getHours() + const hour = hour24 > 12 ? hour24 - 12 : hour24 + return localizedNumberString(hour, locale) +} + +export const hour12TwoDigit: Formatter = (nepaliDate, locale) => { + const hour24 = nepaliDate.getHours() + const hour = hour24 > 12 ? hour24 - 12 : hour24 + return localizedNumberString(zeroPad(hour), locale) +} + +export const minuteNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getMinutes(), locale) + +export const minuteTwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getMinutes()), locale) + +export const secondNumber: Formatter = (nepaliDate, locale) => + localizedNumberString(nepaliDate.getSeconds(), locale) + +export const secondTwoDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(zeroPad(nepaliDate.getSeconds()), locale) + +export const millisecondThreeDigit: Formatter = (nepaliDate, locale) => + localizedNumberString(millisecondZeroPad(nepaliDate.getMilliseconds()), locale) + +export const amPmUpperCase: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return nepaliDate.getHours() >= 12 ? 'पिम' : 'एम' + } + return nepaliDate.getHours() >= 12 ? 'PM' : 'AM' +} + +export const amPmLowerCase: Formatter = (nepaliDate, locale) => { + if (locale === LOCALE_NE) { + return nepaliDate.getHours() >= 12 ? 'पिम' : 'एम' + } + return nepaliDate.getHours() >= 12 ? 'pm' : 'am' +} diff --git a/src/parse.ts b/src/parse.ts index 57d814d..a7e411e 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -13,8 +13,8 @@ */ import { - MONTHS_EN, - MONTHS_SHORT_EN, + NEPALI_MONTHS_EN, + NEPALI_MONTHS_SHORT_EN, WEEKDAYS_LONG_EN, WEEKDAYS_SHORT_EN, } from './constants' @@ -116,8 +116,8 @@ const TOKEN_TO_REGEX: { [key: string]: RegExp } = { SSS: /(\d\d\d)/, A: /(AM|PM)/, a: /(am|pm)/, - MMMM: seqToRE(MONTHS_EN), - MMM: seqToRE(MONTHS_SHORT_EN), + MMMM: seqToRE(NEPALI_MONTHS_EN), + MMM: seqToRE(NEPALI_MONTHS_SHORT_EN), dddd: seqToRE(WEEKDAYS_LONG_EN), ddd: seqToRE(WEEKDAYS_SHORT_EN), dd: seqToRE(WEEKDAYS_SHORT_EN), @@ -169,10 +169,10 @@ function getDateParams( month = matchData break case 'MMMM': - month = MONTHS_EN.indexOf(match[i + 1]) + 1 + month = NEPALI_MONTHS_EN.indexOf(match[i + 1]) + 1 break case 'MMM': - month = MONTHS_SHORT_EN.indexOf(match[i + 1]) + 1 + month = NEPALI_MONTHS_SHORT_EN.indexOf(match[i + 1]) + 1 break case 'DD': case 'D': diff --git a/tests/NepaliDate.test.ts b/tests/NepaliDate.test.ts index 2a4c23e..444a89e 100644 --- a/tests/NepaliDate.test.ts +++ b/tests/NepaliDate.test.ts @@ -120,6 +120,16 @@ describe('NepaliDate', () => { expect(n.format('[YYY] YYYY')).toBe('YYY 2038') }) + it('checks format English Date', () => { + const n = new NepaliDate('2038-07-16') + expect(n.formatEnglishDate('YYYY-MM-DD')).toBe('1981-11-01') + expect(n.formatEnglishDate('YY-M-D')).toBe('81-11-1') + expect(n.formatEnglishDate('[YYY] YYYY')).toBe('YYY 1981') + expect(n.formatEnglishDateInNepali('YYYY/MM/DD')).toBe('१९८१/११/०१') + expect(n.formatEnglishDateInNepali('YY-M-D')).toBe('८१-११-१') + expect(n.formatEnglishDateInNepali('YYYY-MMMM-ddd')).toBe('१९८१-नोभेम्बर-आइत') + }) + it('checks month, date setting', () => { const n = new NepaliDate(2075, 11, 3) expect(n.toString()).toBe('2075-12-03 00:00:00') diff --git a/tests/format.test.ts b/tests/format/format.test.ts similarity index 94% rename from tests/format.test.ts rename to tests/format/format.test.ts index ad8b41b..10b0bf9 100644 --- a/tests/format.test.ts +++ b/tests/format/format.test.ts @@ -1,5 +1,5 @@ -import { format, formatNepali, nepaliDateToString } from '../src/format' -import NepaliDate from '../src/NepaliDate' +import { format, formatNepali, nepaliDateToString } from '../../src/format' +import NepaliDate from '../../src/NepaliDate' describe('format', () => { const nepaliDate1 = new NepaliDate(2080, 1, 32, 7, 40) @@ -282,7 +282,7 @@ describe('formatNepali', () => { const formatStr = 'YYYYY-MMMMM-DDD dddddd HHH hhh:mmm:sss:SSSS A a' const formattedDate = formatNepali(nepaliDate1, formatStr) expect(formattedDate).toEqual( - '२०८०Y-जेठ२-३२३२ बिहिबारबिहि ०७७ ०७७:४०४०:०००:०००S A a' + '२०८०Y-जेठ२-३२३२ बिहिबारबिहि ०७७ ०७७:४०४०:०००:०००S एम एम' ) }) @@ -433,6 +433,30 @@ describe('formatNepali', () => { const formattedDate = formatNepali(d, formatStr) expect(formattedDate).toEqual('०४१') }) + + it('should format NepaliDate (AM) for A', () => { + const formatStr = 'A' + const formattedDate = formatNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('एम') + }) + + it('should format NepaliDate (PM) for A', () => { + const formatStr = 'A' + const formattedDate = formatNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('पिम') + }) + + it('should format NepaliDate (AM) for a', () => { + const formatStr = 'a' + const formattedDate = formatNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('एम') + }) + + it('should format NepaliDate (PM) for a', () => { + const formatStr = 'a' + const formattedDate = formatNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('पिम') + }) }) describe('nepaliDateToString', () => { diff --git a/tests/format/formatEnglishDate.test.ts b/tests/format/formatEnglishDate.test.ts new file mode 100644 index 0000000..b527a23 --- /dev/null +++ b/tests/format/formatEnglishDate.test.ts @@ -0,0 +1,460 @@ +import { formatEnglishDate, formatEnglishDateInNepali } from '../../src/format' +import NepaliDate from '../../src/NepaliDate' + +describe('formatEnglishDate', () => { + const nepaliDate1 = new NepaliDate(2080, 1, 32, 7, 40) + const nepaliDate2 = new NepaliDate(2080, 1, 8, 20, 55, 32) + const nepaliDate3 = new NepaliDate(2080, 8, 15, 16, 9, 40, 413) + + it('should format NepaliDate for the provided format string', () => { + const formatStr = 'YYYY-MM-DD' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('2023-06-15') + }) + + it('should format NepaliDate for the provided format string with normal text', () => { + const formatStr = '[Hello] YYYY-MM-DD [World]!' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('Hello 2023-06-15 World!') + }) + + it('should handle a format string with repeated formats', () => { + const formatStr = 'YYYY YYYY' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('2023 2023') + }) + + it('should handle a format string with multiple formats', () => { + const formatStr = 'YYYY-MM-DD HH:mm' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('2023-06-15 07:40') + }) + + it('should handle a format string with unknown formats', () => { + const formatStr = 'YYYY-QQ-DD HH:mm:ss' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('2023-QQ-15 07:40:00') + }) + + it('should format NepaliDate with the non leading zeros format', () => { + const formatStr = 'YYYY-M-D' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('2023-6-15') + }) + + it('should not format NepaliDate for extended format size', () => { + const formatStr = 'YYYYY-MMMMM-DDD dddddd HHH hhh:mmm:sss:SSSS AA aa' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual( + '2023Y-June6-1515 ThursdayThu 077 077:4040:000:000S AMAM amam' + ) + }) + + /* individual format tests (positive cases) */ + + it('should format NepaliDate for YYYY: 4 digit year', () => { + const formatStr = 'YYYY' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('2023') + }) + + it('should format NepaliDate for YY: 2 digit year', () => { + const formatStr = 'YY' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('23') + }) + + it('should format NepaliDate for MMMM: full month name', () => { + const formatStr = 'MMMM' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('June') + }) + + it('should format NepaliDate for MMM: month abbr name', () => { + const formatStr = 'MMM' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('Jun') + }) + + it('should format NepaliDate for MM: month', () => { + const formatStr = 'MM' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('06') + }) + + it('should format NepaliDate for M: month with non leading zero', () => { + const formatStr = 'M' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('6') + }) + + it('should format NepaliDate for DD: day', () => { + const formatStr = 'DD' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('15') + }) + + it('should format NepaliDate for D: day', () => { + const formatStr = 'D' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('15') + }) + + it('should format NepaliDate for dddd: week day name', () => { + const formatStr = 'dddd' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('Thursday') + }) + + it('should format NepaliDate for ddd: week day abbr name', () => { + const formatStr = 'ddd' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('Thu') + }) + + it('should format NepaliDate for dd: week day abbr name', () => { + const formatStr = 'dd' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('Thu') + }) + + it('should format NepaliDate for d: week day number', () => { + const formatStr = 'd' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('4') + }) + + it('should format NepaliDate for HH: 24-hour', () => { + const formatStr = 'HH' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('07') + }) + + it('should format NepaliDate for H: 24-hour', () => { + const formatStr = 'H' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('16') + }) + + it('should format NepaliDate for hh: 12-hour', () => { + const formatStr = 'hh' + const formattedDate = formatEnglishDate(nepaliDate2, formatStr) + expect(formattedDate).toEqual('08') + }) + + it("should format NepaliDate for hh: 12-hour after 12 o'clock", () => { + const formatStr = 'hh' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('07') + }) + + it('should format NepaliDate for h: 12-hour', () => { + const formatStr = 'h' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('4') + }) + + it('should format NepaliDate for mm: minute', () => { + const formatStr = 'mm' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('09') + }) + + it('should format NepaliDate for m: minute', () => { + const formatStr = 'm' + const formattedDate = formatEnglishDate(nepaliDate2, formatStr) + expect(formattedDate).toEqual('55') + }) + + it('should format NepaliDate for ss: second', () => { + const formatStr = 'ss' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('00') + }) + + it('should format NepaliDate for s: second', () => { + const formatStr = 's' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('40') + }) + + it('should format NepaliDate for SSS: millisecond', () => { + const formatStr = 'SSS' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('413') + }) + + it('should format NepaliDate of 1digit ms for SSS: millisecond', () => { + const formatStr = 'SSS' + const d = new NepaliDate(2080, 2, 26, 1, 2, 3, 4) + const formattedDate = formatEnglishDate(d, formatStr) + expect(formattedDate).toEqual('004') + }) + + it('should format NepaliDate of 2digit ms for SSS: millisecond', () => { + const formatStr = 'SSS' + const d = new NepaliDate(2080, 2, 26, 1, 2, 3, 41) + const formattedDate = formatEnglishDate(d, formatStr) + expect(formattedDate).toEqual('041') + }) + + it('should format NepaliDate (AM) for A: Uppercase AM/PM indicator (e.g., AM, PM)', () => { + const formatStr = 'A' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('AM') + }) + + it('should format NepaliDate (PM) for A: Uppercase AM/PM indicator (e.g., AM, PM)', () => { + const formatStr = 'A' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('PM') + }) + + it('should format NepaliDate (AM) for a: Lowercase AM/PM indicator (e.g., am, pm)', () => { + const formatStr = 'a' + const formattedDate = formatEnglishDate(nepaliDate1, formatStr) + expect(formattedDate).toEqual('am') + }) + + it('should format NepaliDate (PM) for a: Lowercase AM/PM indicator (e.g., am, pm)', () => { + const formatStr = 'a' + const formattedDate = formatEnglishDate(nepaliDate3, formatStr) + expect(formattedDate).toEqual('pm') + }) + + it('should format NepaliDate (AM) for midnight', () => { + const nepaliDate = new NepaliDate(2079, 5, 3) + const formatStr = 'A' + const formattedDate = formatEnglishDate(nepaliDate, formatStr) + expect(formattedDate).toEqual('AM') + }) + + it('should format NepaliDate (PM) for noon', () => { + const nepaliDate = new NepaliDate(2079, 5, 3, 12) + const formatStr = 'A' + const formattedDate = formatEnglishDate(nepaliDate, formatStr) + expect(formattedDate).toEqual('PM') + }) +}) + +describe('formatEnglishDateInNepali', () => { + const nepaliDate1 = new NepaliDate(2080, 1, 32, 7, 40) + const nepaliDate2 = new NepaliDate(2080, 1, 8, 20, 55, 32) + const nepaliDate3 = new NepaliDate(2080, 8, 15, 16, 9, 40, 413) + + it('should format NepaliDate for the provided format string', () => { + const formatStr = 'YYYY-MM-DD' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२०२३-०६-१५') + }) + + it('should format NepaliDate for the provided format string with normal text', () => { + const formatStr = '[Hello] YYYY-MM-DD [World]!' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('Hello २०२३-०६-१५ World!') + }) + + it('should handle a format string with repeated formats', () => { + const formatStr = 'YYYY YYYY' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२०२३ २०२३') + }) + + it('should handle a format string with multiple formats', () => { + const formatStr = 'YYYY-MM-DD HH:mm' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२०२३-०६-१५ ०७:४०') + }) + + it('should handle a format string with unknown formats', () => { + const formatStr = 'YYYY-QQ-DD HH:mm:ss' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२०२३-QQ-१५ ०७:४०:००') + }) + + it('should format NepaliDate with the non leading zeros format', () => { + const formatStr = 'YYYY-M-D' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२०२३-६-१५') + }) + + it('should not format NepaliDate for extended format size', () => { + const formatStr = 'YYYYY-MMMMM-DDD dddddd HHH hhh:mmm:sss:SSSS A a' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual( + '२०२३Y-जुन६-१५१५ बिहिबारबिहि ०७७ ०७७:४०४०:०००:०००S एम एम' + ) + }) + + /* individual format tests (positive cases) */ + + it('should format NepaliDate for YYYY: 4 digit year', () => { + const formatStr = 'YYYY' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२०२३') + }) + + it('should format NepaliDate for YY: 2 digit year', () => { + const formatStr = 'YY' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('२३') + }) + + it('should format NepaliDate for MMMM: full month name', () => { + const formatStr = 'MMMM' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('डिसेम्बर') + }) + + it('should format NepaliDate for MMM: month abbr name', () => { + const formatStr = 'MMM' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('डिसे') + }) + + it('should format NepaliDate for MM: month', () => { + const formatStr = 'MM' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('०६') + }) + + it('should format NepaliDate for M: month with non leading zero', () => { + const formatStr = 'M' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('६') + }) + + it('should format NepaliDate for DD: day', () => { + const formatStr = 'DD' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('१५') + }) + + it('should format NepaliDate for D: day', () => { + const formatStr = 'D' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('१५') + }) + + it('should format NepaliDate for dddd: week day name', () => { + const formatStr = 'dddd' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('बिहिबार') + }) + + it('should format NepaliDate for ddd: week day abbr name', () => { + const formatStr = 'ddd' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('बिहि') + }) + + it('should format NepaliDate for dd: week day abbr name', () => { + const formatStr = 'dd' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('बिहि') + }) + + it('should format NepaliDate for d: week day number', () => { + const formatStr = 'd' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('४') + }) + + it('should format NepaliDate for HH: 24-hour', () => { + const formatStr = 'HH' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('०७') + }) + + it('should format NepaliDate for H: 24-hour', () => { + const formatStr = 'H' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('१६') + }) + + it('should format NepaliDate for hh: 12-hour', () => { + const formatStr = 'hh' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('०७') + }) + + it('should format NepaliDate for hh: 12-hour', () => { + const formatStr = 'hh' + const formattedDate = formatEnglishDateInNepali(nepaliDate2, formatStr) + expect(formattedDate).toEqual('०८') + }) + + it('should format NepaliDate for h: 12-hour', () => { + const formatStr = 'h' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('४') + }) + + it('should format NepaliDate for mm: minute', () => { + const formatStr = 'mm' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('०९') + }) + + it('should format NepaliDate for m: minute', () => { + const formatStr = 'm' + const formattedDate = formatEnglishDateInNepali(nepaliDate2, formatStr) + expect(formattedDate).toEqual('५५') + }) + + it('should format NepaliDate for ss: second', () => { + const formatStr = 'ss' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('००') + }) + + it('should format NepaliDate for s: second', () => { + const formatStr = 's' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('४०') + }) + + it('should format NepaliDate for SSS: millisecond', () => { + const formatStr = 'SSS' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('४१३') + }) + + it('should format NepaliDate of 1digit ms for SSS: millisecond', () => { + const formatStr = 'SSS' + const d = new NepaliDate(2080, 2, 26, 1, 2, 3, 4) + const formattedDate = formatEnglishDateInNepali(d, formatStr) + expect(formattedDate).toEqual('००४') + }) + + it('should format NepaliDate of 2digit ms for SSS: millisecond', () => { + const formatStr = 'SSS' + const d = new NepaliDate(2080, 2, 26, 1, 2, 3, 41) + const formattedDate = formatEnglishDateInNepali(d, formatStr) + expect(formattedDate).toEqual('०४१') + }) + + it('should format NepaliDate (AM) for A', () => { + const formatStr = 'A' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('एम') + }) + + it('should format NepaliDate (PM) for A', () => { + const formatStr = 'A' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('पिम') + }) + + it('should format NepaliDate (AM) for a', () => { + const formatStr = 'a' + const formattedDate = formatEnglishDateInNepali(nepaliDate1, formatStr) + expect(formattedDate).toEqual('एम') + }) + + it('should format NepaliDate (PM) for a', () => { + const formatStr = 'a' + const formattedDate = formatEnglishDateInNepali(nepaliDate3, formatStr) + expect(formattedDate).toEqual('पिम') + }) +})