@@ -230,8 +230,7 @@ export class DisplayFile {
230230 static parseConditionals ( conditionColumns : string ) : Conditional [ ] {
231231 if ( conditionColumns . trim ( ) === "" ) { return [ ] ; }
232232
233- /** @type {Conditional[] } */
234- let conditionals = [ ] ;
233+ let conditionals : Conditional [ ] = [ ] ;
235234
236235 //TODO: something with condition
237236 //const condition = conditionColumns.substring(0, 1); //A (and) or O (or)
@@ -351,6 +350,37 @@ export class DisplayFile {
351350 return result ;
352351 }
353352
353+ private static conditionalGroups ( conditions : Conditional [ ] ) {
354+ return conditions . reduce ( ( acc , curr , index ) => {
355+ if ( index % 3 === 0 ) {
356+ acc . push ( [ curr ] ) ;
357+ } else {
358+ acc [ acc . length - 1 ] . push ( curr ) ;
359+ }
360+ return acc ;
361+ } , [ ] as Conditional [ ] [ ] ) ;
362+ }
363+
364+ public static getLinesForKeyword ( keyword : Keyword ) : string [ ] {
365+ const lines : string [ ] = [ ] ;
366+
367+ // Convert array into groups of three
368+ const condition = this . conditionalGroups ( keyword . conditions ) ;
369+
370+ const firstConditions = condition [ 0 ] || [ ] ;
371+ const conditionStrings = firstConditions . map ( c => `${ c . negate ? 'N' : ' ' } ${ c . indicator } ` ) . join ( '' ) . padEnd ( 9 ) ;
372+
373+ lines . push ( ` A ${ conditionStrings } ${ keyword . name } ${ keyword . value ? `(${ keyword . value } )` : `` } ` ) ;
374+
375+ for ( let g = 1 ; g < condition . length ; g ++ ) {
376+ const group = condition [ g ] ;
377+ const conditionStrings = group . map ( c => `${ c . negate ? 'N' : ' ' } ${ c . indicator } ` ) . join ( '' ) ;
378+ lines . push ( ` A ${ conditionStrings } ` ) ;
379+ }
380+
381+ return lines ;
382+ }
383+
354384 public static getLinesForField ( field : FieldInfo ) : string [ ] {
355385 const newLines : string [ ] = [ ] ;
356386
@@ -366,25 +396,27 @@ export class DisplayFile {
366396 const y = String ( field . position . y ) . padStart ( 3 , ` ` ) ;
367397 const displayType = FIELD_TYPE [ field . displayType ! ] ;
368398
399+ // Convert array into groups of three
400+ const condition = this . conditionalGroups ( field . conditions ) ;
401+ const firstConditions = condition [ 0 ] || [ ] ;
402+ const conditionStrings = firstConditions . map ( c => `${ c . negate ? 'N' : ' ' } ${ c . indicator } ` ) . join ( '' ) . padEnd ( 9 ) ;
403+
369404 if ( field . displayType === `const` ) {
370405 const value = field . value ;
371406 newLines . push (
372- ` A ${ y } ${ x } '${ value } '` ,
407+ ` A ${ conditionStrings } ${ y } ${ x } '${ value } '` ,
373408 ) ;
374409 } else if ( displayType && field . name ) {
375410 const definitionType = field . type ;
376411 const length = String ( field . length ) . padStart ( 5 ) ;
377- const decimals = String ( field . decimals ) . padStart ( 2 ) ;
412+ const decimals = ( field . type !== `A` ? String ( field . decimals ) : `` ) . padStart ( 2 ) ;
378413 newLines . push (
379- ` A ${ field . name . padEnd ( 10 ) } ${ length } ${ definitionType } ${ decimals } ${ displayType } ${ y } ${ x } ` ,
414+ ` A ${ conditionStrings } ${ field . name . padEnd ( 10 ) } ${ length } ${ definitionType } ${ decimals } ${ displayType } ${ y } ${ x } ` ,
380415 ) ;
381416 }
382417
383418 for ( const keyword of field . keywords ) {
384- // TODO: support conditions
385- newLines . push (
386- ` A ${ keyword . name } ${ keyword . value ? `(${ keyword . value } )` : `` } ` ,
387- ) ;
419+ newLines . push ( ...DisplayFile . getLinesForKeyword ( keyword ) ) ;
388420 }
389421
390422 return newLines ;
@@ -437,24 +469,22 @@ export class DisplayFile {
437469 }
438470
439471 // TODO: test cases
440- static getLinesForFormat ( recordFormat : RecordInfo ) : string [ ] {
472+ static getHeaderLinesForFormat ( recordFormat : string , keywords : Keyword [ ] ) : string [ ] {
441473 const lines : string [ ] = [ ] ;
442474
443- if ( recordFormat . name !== GLOBAL_RECORD_NAME ) {
444- lines . push ( ` A R ${ recordFormat . name } ` ) ;
475+ if ( recordFormat ) {
476+ lines . push ( ` A R ${ recordFormat } ` ) ;
445477 }
446478
447- for ( const keyword of recordFormat . keywords ) {
479+ for ( const keyword of keywords ) {
448480 // TODO: support conditions
449- lines . push (
450- ` A ${ keyword . name } ${ keyword . value ? `(${ keyword . value } )` : `` } ` ,
451- ) ;
481+ lines . push ( ...DisplayFile . getLinesForKeyword ( keyword ) ) ;
452482 }
453483
454484 return lines ;
455485 }
456486
457- public getRangeForFormat ( recordFormat : string ) : DdsLineRange | undefined {
487+ public getHeaderRangeForFormat ( recordFormat : string ) : DdsLineRange | undefined {
458488 let range : DdsLineRange | undefined = undefined ;
459489 const currentFormatI = this . formats . findIndex ( format => format . name === recordFormat ) ;
460490 if ( currentFormatI > 0 ) {
@@ -474,9 +504,9 @@ export class DisplayFile {
474504 }
475505
476506 // TODO: test cases
477- public updateFormat ( originalFormatName : string , newRecordFormat : RecordInfo ) : DdsUpdate | undefined {
478- const newLines = DisplayFile . getLinesForFormat ( newRecordFormat ) ;
479- let range = this . getRangeForFormat ( originalFormatName ) ;
507+ public updateFormatHeader ( originalFormatName : string , keywords : Keyword [ ] ) : DdsUpdate | undefined {
508+ const newLines = DisplayFile . getHeaderLinesForFormat ( originalFormatName , keywords ) ;
509+ let range = this . getHeaderRangeForFormat ( originalFormatName ) ;
480510
481511 if ( range ) {
482512 range = {
0 commit comments