diff --git a/src/NepaliDate.ts b/src/NepaliDate.ts index 08f269e..b6ff959 100644 --- a/src/NepaliDate.ts +++ b/src/NepaliDate.ts @@ -22,79 +22,87 @@ class NepaliDate { static maximum: () => Date /** - * Creates NepaliDate instance from current date - * @example new Date() + * Creates a NepaliDate instance for the current date and time. + * + * @example + * const now = new NepaliDate() */ constructor() /** - * Create NepaliDate instance from provided english date + * Creates a NepaliDate instance from a provided Javascript Date object. * - * @param {Date} date + * @param {Date} date - The Javascript Date object. * * @example - * const nepaliDate = new NepaliDate( new Date('2020-01-01') ) - * + * const jsDate = new Date("2020-01-01") + * const nepaliDate = new NepaliDate(jsDate) */ constructor(date: Date) /** - * Create NepaliDate instance from using a provided english date - * @param {NepaliDate} date: nepali date value provided as a value. + * Creates a new NepaliDate instance from another NepaliDate object. + * + * @param {NepaliDate} date - The NepaliDate object. * @example * const nepaliDateOld = new NepaliDate('2080-01-01') - * const nepaliDate = new NepaliDate( nepaliDateOld ) - * + * const nepaliDate = new NepaliDate(nepaliDateOld) */ constructor(date: NepaliDate) /** - * Create NepaliDate instance by parsing a provided string value - * @param {string} value: string date time. + * Creates a NepaliDate instance by parsing a provided date-time string. + * + * @param {string} value - The date-time string. * @example * const nepaliDate = new NepaliDate('2080-01-01') - * */ constructor(value: string) /** - * Create NepaliDate instance by parsing a provided numeric value - * @param {number} value: numeric value - * @example - * const n = new NepaliDate(new Date(373314600000)) + * Creates a NepaliDate instance from a provided Unix timestamp. * + * @param {number} value - The Unix timestamp. + * @example + * const timestamp = 1695569762 // Unix timestamp in milliseconds + * const nepaliDate = new NepaliDate(timestamp) */ constructor(value: number) /** - * Create NepaliDate instance by parsing a provided time string with format provided - * @param dateString: string date time - * @param format: string format of the date provided - * @example - * const n1 = new NepaliDate('2042/08/12 14-05-23.789', 'YYYY/MM/DD HH-mm-ss.SSS') + * Creates a NepaliDate instance by parsing a provided date-time string + * with the given format. * + * @param dateString - The date-time string to parse. + * @param format - The format of the provided date-time string. + * @example + * const dateTimeString = '2080/08/12 14-05-23.789' + * const format = 'YYYY/MM/DD HH-mm-ss.SSS' + * const nepaliDate = new NepaliDate(dateTimeString, format) */ constructor(dateString: string, format: string) /** - * Creates a new instance of the Date class. + * Creates a NepaliDate instance from a provided Nepali calendar date components. * * @constructor - * @param {number} year - The year. - * @param {number} month - The month (0-11, where 0 is January and 11 is December). - * @param {number=} day - The day of the month (1-31). - * @param {number=} hour - The hour of the day (0-23). - * @param {number=} minute - The minute (0-59). - * @param {number=} second - The second (0-59). - * @param {number=} ms - The milliseconds (0-999). + * @param {number} year - The year (e.g., 2080). + * @param {number} month - The month (0-11, where 0 is Baisakh and 11 is Chaitra). + * @param {number} [day=1] - The day of the month (1-31). + * @param {number} [hour=0] - The hour of the day (0-23). + * @param {number} [minute=0] - The minute (0-59). + * @param {number} [second=0] - The second (0-59). + * @param {number} [ms=0] - The milliseconds (0-999). * * @example - * const [year, month, day] = [2080, 1, 12] + * const year = 2080 + * const month = 1 // Jestha + * const day = 12 * const hour = 12 * const minute = 30 * const second = 45 * const ms = 500 - * const nd = new NepaliDate(year, month, day, hour, minute, second, ms) + * const nepaliDate = new NepaliDate(year, month, day, hour, minute, second, ms) */ constructor( year: number, @@ -107,54 +115,98 @@ class NepaliDate { ) constructor(...args: any[]) { if (args.length === 0) { - this._setDateObject(new Date()) - } else if (args.length === 1) { - const e = args[0] - if (typeof e === 'object') { - if (e instanceof Date) { - this._setDateObject(e) - } else if (e instanceof NepaliDate) { - this.timestamp = e.timestamp - this.year = e.year - this.yearEn = e.yearEn - this.month = e.month - this.monthEn = e.monthEn - this.day = e.day - this.dayEn = e.dayEn - this.hour = e.hour - this.minute = e.minute - this.weekDay = e.weekDay - } else { - throw new Error('Invalid date argument') - } - } else if (typeof e === 'number') { - this._setDateObject(new Date(e)) - } else if (typeof e === 'string') { - // Try to parse the date - this.set.apply(this, parse(e)) - } else { - throw new Error('Invalid date argument') - } + this.initFromCurrentDate() + } else if (args.length === 1 && args[0] instanceof Date) { + this.initFromEnglishDate(args[0]) + } else if (args.length === 1 && args[0] instanceof NepaliDate) { + this.initFromNepaliDate(args[0]) + } else if (args.length === 1 && typeof args[0] === 'string') { + this.parseFromString(args[0]) + } else if (args.length === 1 && typeof args[0] === 'number') { + this.initFromTimestamp(args[0]) } else if ( args.length === 2 && typeof args[0] === 'string' && typeof args[1] === 'string' ) { - const [dateTimeString, format] = args - this.set.apply(this, parseFormat(dateTimeString, format)) + this.parseFromStringWithFormat(args[0], args[1]) + } else if ( + args.length >= 2 && + args.length <= 8 && + args.every(arg => typeof arg === 'number') + ) { + this.initFromComponents(args) } else { - this.set( - args[0], // year - args[1], // month - args[2] ?? 1, // day - args[3] ?? 0, // hour - args[4] ?? 0, // minute - args[5] ?? 0, // second - args[6] ?? 0 // ms - ) + throw new Error('Invalid date argument') } } + /* Object initialization */ + + private initFromCurrentDate() { + this._setDateObject(new Date()) + } + + private initFromEnglishDate(date: Date) { + this._setDateObject(date) + } + + private initFromNepaliDate(date: NepaliDate) { + this.set( + date.year, + date.month, + date.day, + date.hour, + date.minute, + date.getSeconds(), + date.getMilliseconds() + ) + } + + private parseFromString(value: string) { + const parsedResult = parse(value) + this.set( + parsedResult[0], // Year + parsedResult[1], // Month + parsedResult[2], // Date + parsedResult[3], // Hour + parsedResult[4], // Minute + parsedResult[5], // Second + parsedResult[6] // Millisecond + ) + } + + private initFromTimestamp(value: number) { + this._setDateObject(new Date(value)) + } + + private parseFromStringWithFormat(dateString: string, format: string) { + const parsedResult = parseFormat(dateString, format) + this.set( + parsedResult[0], // Year + parsedResult[1], // Month + parsedResult[2], // Date + parsedResult[3], // Hour + parsedResult[4], // Minute + parsedResult[5], // Second + parsedResult[6] // Millisecond + ) + } + + private initFromComponents(args: number[]) { + this.set( + args[0], // year + args[1], // month + args[2] ?? 1, // day + args[3] ?? 0, // hour + args[4] ?? 0, // minute + args[5] ?? 0, // second + args[6] ?? 0 // ms + ) + } + + /* Object methods */ + /** * Sets the English date and optionally computes the corresponding Nepali date. * Handles all the operations and variables while setting the English date. @@ -217,7 +269,7 @@ class NepaliDate { /** * Retrieves the month of the Nepali date in the Nepali calendar. * - * @returns {number} The numeric value representing the month. 0 for Baishakh and 11 for Chaitra. + * @returns {number} The numeric value representing the month. 0 for Baisakh and 11 for Chaitra. */ getMonth(): number { return this.month