@@ -22,79 +22,87 @@ class NepaliDate {
2222 static maximum : ( ) => Date
2323
2424 /**
25- * Creates NepaliDate instance from current date
26- * @example new Date()
25+ * Creates a NepaliDate instance for the current date and time.
26+ *
27+ * @example
28+ * const now = new NepaliDate()
2729 */
2830 constructor ( )
2931
3032 /**
31- * Create NepaliDate instance from provided english date
33+ * Creates a NepaliDate instance from a provided Javascript Date object.
3234 *
33- * @param {Date } date
35+ * @param {Date } date - The Javascript Date object.
3436 *
3537 * @example
36- * const nepaliDate = new NepaliDate( new Date(' 2020-01-01') )
37- *
38+ * const jsDate = new Date(" 2020-01-01" )
39+ * const nepaliDate = new NepaliDate(jsDate)
3840 */
3941 constructor ( date : Date )
4042
4143 /**
42- * Create NepaliDate instance from using a provided english date
43- * @param {NepaliDate } date: nepali date value provided as a value.
44+ * Creates a new NepaliDate instance from another NepaliDate object.
45+ *
46+ * @param {NepaliDate } date - The NepaliDate object.
4447 * @example
4548 * const nepaliDateOld = new NepaliDate('2080-01-01')
46- * const nepaliDate = new NepaliDate( nepaliDateOld )
47- *
49+ * const nepaliDate = new NepaliDate(nepaliDateOld)
4850 */
4951 constructor ( date : NepaliDate )
5052
5153 /**
52- * Create NepaliDate instance by parsing a provided string value
53- * @param {string } value: string date time.
54+ * Creates a NepaliDate instance by parsing a provided date-time string.
55+ *
56+ * @param {string } value - The date-time string.
5457 * @example
5558 * const nepaliDate = new NepaliDate('2080-01-01')
56- *
5759 */
5860 constructor ( value : string )
5961
6062 /**
61- * Create NepaliDate instance by parsing a provided numeric value
62- * @param {number } value: numeric value
63- * @example
64- * const n = new NepaliDate(new Date(373314600000))
63+ * Creates a NepaliDate instance from a provided Unix timestamp.
6564 *
65+ * @param {number } value - The Unix timestamp.
66+ * @example
67+ * const timestamp = 1695569762 // Unix timestamp in milliseconds
68+ * const nepaliDate = new NepaliDate(timestamp)
6669 */
6770 constructor ( value : number )
6871
6972 /**
70- * Create NepaliDate instance by parsing a provided time string with format provided
71- * @param dateString: string date time
72- * @param format: string format of the date provided
73- * @example
74- * const n1 = new NepaliDate('2042/08/12 14-05-23.789', 'YYYY/MM/DD HH-mm-ss.SSS')
73+ * Creates a NepaliDate instance by parsing a provided date-time string
74+ * with the given format.
7575 *
76+ * @param dateString - The date-time string to parse.
77+ * @param format - The format of the provided date-time string.
78+ * @example
79+ * const dateTimeString = '2080/08/12 14-05-23.789'
80+ * const format = 'YYYY/MM/DD HH-mm-ss.SSS'
81+ * const nepaliDate = new NepaliDate(dateTimeString, format)
7682 */
7783 constructor ( dateString : string , format : string )
7884
7985 /**
80- * Creates a new instance of the Date class .
86+ * Creates a NepaliDate instance from a provided Nepali calendar date components .
8187 *
8288 * @constructor
83- * @param {number } year - The year.
84- * @param {number } month - The month (0-11, where 0 is January and 11 is December ).
85- * @param {number= } day - The day of the month (1-31).
86- * @param {number= } hour - The hour of the day (0-23).
87- * @param {number= } minute - The minute (0-59).
88- * @param {number= } second - The second (0-59).
89- * @param {number= } ms - The milliseconds (0-999).
89+ * @param {number } year - The year (e.g., 2080) .
90+ * @param {number } month - The month (0-11, where 0 is Baisakh and 11 is Chaitra ).
91+ * @param {number } [ day=1] - The day of the month (1-31).
92+ * @param {number } [ hour=0] - The hour of the day (0-23).
93+ * @param {number } [ minute=0] - The minute (0-59).
94+ * @param {number } [ second=0] - The second (0-59).
95+ * @param {number } [ms=0] - The milliseconds (0-999).
9096 *
9197 * @example
92- * const [year, month, day] = [2080, 1, 12]
98+ * const year = 2080
99+ * const month = 1 // Jestha
100+ * const day = 12
93101 * const hour = 12
94102 * const minute = 30
95103 * const second = 45
96104 * const ms = 500
97- * const nd = new NepaliDate(year, month, day, hour, minute, second, ms)
105+ * const nepaliDate = new NepaliDate(year, month, day, hour, minute, second, ms)
98106 */
99107 constructor (
100108 year : number ,
@@ -107,54 +115,98 @@ class NepaliDate {
107115 )
108116 constructor ( ...args : any [ ] ) {
109117 if ( args . length === 0 ) {
110- this . _setDateObject ( new Date ( ) )
111- } else if ( args . length === 1 ) {
112- const e = args [ 0 ]
113- if ( typeof e === 'object' ) {
114- if ( e instanceof Date ) {
115- this . _setDateObject ( e )
116- } else if ( e instanceof NepaliDate ) {
117- this . timestamp = e . timestamp
118- this . year = e . year
119- this . yearEn = e . yearEn
120- this . month = e . month
121- this . monthEn = e . monthEn
122- this . day = e . day
123- this . dayEn = e . dayEn
124- this . hour = e . hour
125- this . minute = e . minute
126- this . weekDay = e . weekDay
127- } else {
128- throw new Error ( 'Invalid date argument' )
129- }
130- } else if ( typeof e === 'number' ) {
131- this . _setDateObject ( new Date ( e ) )
132- } else if ( typeof e === 'string' ) {
133- // Try to parse the date
134- this . set . apply ( this , parse ( e ) )
135- } else {
136- throw new Error ( 'Invalid date argument' )
137- }
118+ this . initFromCurrentDate ( )
119+ } else if ( args . length === 1 && args [ 0 ] instanceof Date ) {
120+ this . initFromEnglishDate ( args [ 0 ] )
121+ } else if ( args . length === 1 && args [ 0 ] instanceof NepaliDate ) {
122+ this . initFromNepaliDate ( args [ 0 ] )
123+ } else if ( args . length === 1 && typeof args [ 0 ] === 'string' ) {
124+ this . parseFromString ( args [ 0 ] )
125+ } else if ( args . length === 1 && typeof args [ 0 ] === 'number' ) {
126+ this . initFromTimestamp ( args [ 0 ] )
138127 } else if (
139128 args . length === 2 &&
140129 typeof args [ 0 ] === 'string' &&
141130 typeof args [ 1 ] === 'string'
142131 ) {
143- const [ dateTimeString , format ] = args
144- this . set . apply ( this , parseFormat ( dateTimeString , format ) )
132+ this . parseFromStringWithFormat ( args [ 0 ] , args [ 1 ] )
133+ } else if (
134+ args . length >= 2 &&
135+ args . length <= 8 &&
136+ args . every ( arg => typeof arg === 'number' )
137+ ) {
138+ this . initFromComponents ( args )
145139 } else {
146- this . set (
147- args [ 0 ] , // year
148- args [ 1 ] , // month
149- args [ 2 ] ?? 1 , // day
150- args [ 3 ] ?? 0 , // hour
151- args [ 4 ] ?? 0 , // minute
152- args [ 5 ] ?? 0 , // second
153- args [ 6 ] ?? 0 // ms
154- )
140+ throw new Error ( 'Invalid date argument' )
155141 }
156142 }
157143
144+ /* Object initialization */
145+
146+ private initFromCurrentDate ( ) {
147+ this . _setDateObject ( new Date ( ) )
148+ }
149+
150+ private initFromEnglishDate ( date : Date ) {
151+ this . _setDateObject ( date )
152+ }
153+
154+ private initFromNepaliDate ( date : NepaliDate ) {
155+ this . set (
156+ date . year ,
157+ date . month ,
158+ date . day ,
159+ date . hour ,
160+ date . minute ,
161+ date . getSeconds ( ) ,
162+ date . getMilliseconds ( )
163+ )
164+ }
165+
166+ private parseFromString ( value : string ) {
167+ const parsedResult = parse ( value )
168+ this . set (
169+ parsedResult [ 0 ] , // Year
170+ parsedResult [ 1 ] , // Month
171+ parsedResult [ 2 ] , // Date
172+ parsedResult [ 3 ] , // Hour
173+ parsedResult [ 4 ] , // Minute
174+ parsedResult [ 5 ] , // Second
175+ parsedResult [ 6 ] // Millisecond
176+ )
177+ }
178+
179+ private initFromTimestamp ( value : number ) {
180+ this . _setDateObject ( new Date ( value ) )
181+ }
182+
183+ private parseFromStringWithFormat ( dateString : string , format : string ) {
184+ const parsedResult = parseFormat ( dateString , format )
185+ this . set (
186+ parsedResult [ 0 ] , // Year
187+ parsedResult [ 1 ] , // Month
188+ parsedResult [ 2 ] , // Date
189+ parsedResult [ 3 ] , // Hour
190+ parsedResult [ 4 ] , // Minute
191+ parsedResult [ 5 ] , // Second
192+ parsedResult [ 6 ] // Millisecond
193+ )
194+ }
195+
196+ private initFromComponents ( args : number [ ] ) {
197+ this . set (
198+ args [ 0 ] , // year
199+ args [ 1 ] , // month
200+ args [ 2 ] ?? 1 , // day
201+ args [ 3 ] ?? 0 , // hour
202+ args [ 4 ] ?? 0 , // minute
203+ args [ 5 ] ?? 0 , // second
204+ args [ 6 ] ?? 0 // ms
205+ )
206+ }
207+
208+ /* Object methods */
209+
158210 /**
159211 * Sets the English date and optionally computes the corresponding Nepali date.
160212 * Handles all the operations and variables while setting the English date.
@@ -217,7 +269,7 @@ class NepaliDate {
217269 /**
218270 * Retrieves the month of the Nepali date in the Nepali calendar.
219271 *
220- * @returns {number } The numeric value representing the month. 0 for Baishakh and 11 for Chaitra.
272+ * @returns {number } The numeric value representing the month. 0 for Baisakh and 11 for Chaitra.
221273 */
222274 getMonth ( ) : number {
223275 return this . month
0 commit comments