@@ -15,7 +15,11 @@ export interface DateParts {
1515
1616export type DateFormat = 'YMD' | 'MDY' | 'DMY' ;
1717
18- const SEGMENTS : Record < string , SegmentConfig > = { Y : { type : 'year' , len : 4 } , M : { type : 'month' , len : 2 } , D : { type : 'day' , len : 2 } } as const ;
18+ const SEGMENTS : Record < string , SegmentConfig > = {
19+ Y : { type : 'year' , len : 4 } ,
20+ M : { type : 'month' , len : 2 } ,
21+ D : { type : 'day' , len : 2 } ,
22+ } as const ;
1923
2024function segmentsFor ( fmt : DateFormat ) : SegmentConfig [ ] {
2125 return [ ...fmt ] . map ( ( c ) => SEGMENTS [ c as keyof typeof SEGMENTS ] ) ;
@@ -46,17 +50,19 @@ function parseSegmentToNum(s: string): number {
4650}
4751
4852function parse ( parts : DateParts ) : { year : number ; month : number ; day : number } {
49- return { year : parseSegmentToNum ( parts . year ) , month : parseSegmentToNum ( parts . month ) , day : parseSegmentToNum ( parts . day ) } ;
53+ return {
54+ year : parseSegmentToNum ( parts . year ) ,
55+ month : parseSegmentToNum ( parts . month ) ,
56+ day : parseSegmentToNum ( parts . day ) ,
57+ } ;
5058}
5159
5260function daysInMonth ( year : number , month : number ) : number {
5361 return new Date ( year || 2001 , month || 1 , 0 ) . getDate ( ) ;
5462}
5563
5664/** Validate and return calendar parts, or undefined if invalid */
57- function validParts (
58- parts : DateParts
59- ) : { year : number ; month : number ; day : number } | undefined {
65+ function validParts ( parts : DateParts ) : { year : number ; month : number ; day : number } | undefined {
6066 const { year, month, day } = parse ( parts ) ;
6167 if ( ! year || year < 0 || year > 9999 ) return undefined ;
6268 if ( ! month || month < 1 || month > 12 ) return undefined ;
@@ -79,10 +85,18 @@ function segmentBounds(
7985 maxDate : Date | undefined
8086) : { min : number ; max : number } {
8187 const minP = minDate
82- ? { year : minDate . getUTCFullYear ( ) , month : minDate . getUTCMonth ( ) + 1 , day : minDate . getUTCDate ( ) }
88+ ? {
89+ year : minDate . getUTCFullYear ( ) ,
90+ month : minDate . getUTCMonth ( ) + 1 ,
91+ day : minDate . getUTCDate ( ) ,
92+ }
8393 : null ;
8494 const maxP = maxDate
85- ? { year : maxDate . getUTCFullYear ( ) , month : maxDate . getUTCMonth ( ) + 1 , day : maxDate . getUTCDate ( ) }
95+ ? {
96+ year : maxDate . getUTCFullYear ( ) ,
97+ month : maxDate . getUTCMonth ( ) + 1 ,
98+ day : maxDate . getUTCDate ( ) ,
99+ }
86100 : null ;
87101
88102 if ( type === 'year' ) {
@@ -96,7 +110,10 @@ function segmentBounds(
96110 }
97111 return {
98112 min : minP && ctx . year === minP . year && ctx . month === minP . month ? minP . day : 1 ,
99- max : maxP && ctx . year === maxP . year && ctx . month === maxP . month ? maxP . day : daysInMonth ( ctx . year , ctx . month ) ,
113+ max :
114+ maxP && ctx . year === maxP . year && ctx . month === maxP . month
115+ ? maxP . day
116+ : daysInMonth ( ctx . year , ctx . month ) ,
100117 } ;
101118}
102119
@@ -186,7 +203,10 @@ export default class DatePrompt extends Prompt<Date> {
186203 const index = Math . max ( 0 , Math . min ( this . #cursor. segmentIndex , this . #segments. length - 1 ) ) ;
187204 const segment = this . #segments[ index ] ;
188205 if ( ! segment ) return undefined ;
189- this . #cursor. positionInSegment = Math . max ( 0 , Math . min ( this . #cursor. positionInSegment , segment . len - 1 ) ) ;
206+ this . #cursor. positionInSegment = Math . max (
207+ 0 ,
208+ Math . min ( this . #cursor. positionInSegment , segment . len - 1 )
209+ ) ;
190210 return { segment, index } ;
191211 }
192212
@@ -195,7 +215,10 @@ export default class DatePrompt extends Prompt<Date> {
195215 this . #pendingTensDigit = null ;
196216 const ctx = this . #seg( ) ;
197217 if ( ! ctx ) return ;
198- this . #cursor. segmentIndex = Math . max ( 0 , Math . min ( this . #segments. length - 1 , ctx . index + direction ) ) ;
218+ this . #cursor. segmentIndex = Math . max (
219+ 0 ,
220+ Math . min ( this . #segments. length - 1 , ctx . index + direction )
221+ ) ;
199222 this . #cursor. positionInSegment = 0 ;
200223 this . #segmentSelected = true ;
201224 }
@@ -207,7 +230,12 @@ export default class DatePrompt extends Prompt<Date> {
207230 const raw = this . #segmentValues[ segment . type ] ;
208231 const isBlank = ! raw || raw . replace ( / _ / g, '' ) === '' ;
209232 const num = Number . parseInt ( ( raw || '0' ) . replace ( / _ / g, '0' ) , 10 ) || 0 ;
210- const bounds = segmentBounds ( segment . type , parse ( this . #segmentValues) , this . #minDate, this . #maxDate) ;
233+ const bounds = segmentBounds (
234+ segment . type ,
235+ parse ( this . #segmentValues) ,
236+ this . #minDate,
237+ this . #maxDate
238+ ) ;
211239
212240 let next : number ;
213241 if ( isBlank ) {
@@ -216,7 +244,10 @@ export default class DatePrompt extends Prompt<Date> {
216244 next = Math . max ( Math . min ( bounds . max , num + direction ) , bounds . min ) ;
217245 }
218246
219- this . #segmentValues = { ...this . #segmentValues, [ segment . type ] : next . toString ( ) . padStart ( segment . len , '0' ) } ;
247+ this . #segmentValues = {
248+ ...this . #segmentValues,
249+ [ segment . type ] : next . toString ( ) . padStart ( segment . len , '0' ) ,
250+ } ;
220251 this . #segmentSelected = true ;
221252 this . #pendingTensDigit = null ;
222253 this . #refresh( ) ;
@@ -225,18 +256,25 @@ export default class DatePrompt extends Prompt<Date> {
225256 #onCursor( key ?: string ) {
226257 if ( ! key ) return ;
227258 switch ( key ) {
228- case 'right' : return this . #navigate( 1 ) ;
229- case 'left' : return this . #navigate( - 1 ) ;
230- case 'up' : return this . #adjust( 1 ) ;
231- case 'down' : return this . #adjust( - 1 ) ;
259+ case 'right' :
260+ return this . #navigate( 1 ) ;
261+ case 'left' :
262+ return this . #navigate( - 1 ) ;
263+ case 'up' :
264+ return this . #adjust( 1 ) ;
265+ case 'down' :
266+ return this . #adjust( - 1 ) ;
232267 }
233268 }
234269
235270 #onKey( char : string | undefined , key : Key ) {
236271 // Backspace
237272 const isBackspace =
238- key ?. name === 'backspace' || key ?. sequence === '\x7f' || key ?. sequence === '\b' ||
239- char === '\x7f' || char === '\b' ;
273+ key ?. name === 'backspace' ||
274+ key ?. sequence === '\x7f' ||
275+ key ?. sequence === '\b' ||
276+ char === '\x7f' ||
277+ char === '\b' ;
240278 if ( isBackspace ) {
241279 this . inlineError = '' ;
242280 const ctx = this . #seg( ) ;
@@ -308,7 +346,8 @@ export default class DatePrompt extends Prompt<Date> {
308346
309347 const display = this . #segmentValues[ segment . type ] ;
310348 const firstBlank = display . indexOf ( '_' ) ;
311- const pos = firstBlank >= 0 ? firstBlank : Math . min ( this . #cursor. positionInSegment , segment . len - 1 ) ;
349+ const pos =
350+ firstBlank >= 0 ? firstBlank : Math . min ( this . #cursor. positionInSegment , segment . len - 1 ) ;
312351 if ( pos < 0 || pos >= segment . len ) return ;
313352
314353 let newVal = display . slice ( 0 , pos ) + char + display . slice ( pos + 1 ) ;
0 commit comments