@@ -230,8 +230,7 @@ export class DisplayFile {
230
230
static parseConditionals ( conditionColumns : string ) : Conditional [ ] {
231
231
if ( conditionColumns . trim ( ) === "" ) { return [ ] ; }
232
232
233
- /** @type {Conditional[] } */
234
- let conditionals = [ ] ;
233
+ let conditionals : Conditional [ ] = [ ] ;
235
234
236
235
//TODO: something with condition
237
236
//const condition = conditionColumns.substring(0, 1); //A (and) or O (or)
@@ -351,6 +350,37 @@ export class DisplayFile {
351
350
return result ;
352
351
}
353
352
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
+
354
384
public static getLinesForField ( field : FieldInfo ) : string [ ] {
355
385
const newLines : string [ ] = [ ] ;
356
386
@@ -366,25 +396,27 @@ export class DisplayFile {
366
396
const y = String ( field . position . y ) . padStart ( 3 , ` ` ) ;
367
397
const displayType = FIELD_TYPE [ field . displayType ! ] ;
368
398
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
+
369
404
if ( field . displayType === `const` ) {
370
405
const value = field . value ;
371
406
newLines . push (
372
- ` A ${ y } ${ x } '${ value } '` ,
407
+ ` A ${ conditionStrings } ${ y } ${ x } '${ value } '` ,
373
408
) ;
374
409
} else if ( displayType && field . name ) {
375
410
const definitionType = field . type ;
376
411
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 ) ;
378
413
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 } ` ,
380
415
) ;
381
416
}
382
417
383
418
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 ) ) ;
388
420
}
389
421
390
422
return newLines ;
@@ -437,24 +469,22 @@ export class DisplayFile {
437
469
}
438
470
439
471
// TODO: test cases
440
- static getLinesForFormat ( recordFormat : RecordInfo ) : string [ ] {
472
+ static getHeaderLinesForFormat ( recordFormat : string , keywords : Keyword [ ] ) : string [ ] {
441
473
const lines : string [ ] = [ ] ;
442
474
443
- if ( recordFormat . name !== GLOBAL_RECORD_NAME ) {
444
- lines . push ( ` A R ${ recordFormat . name } ` ) ;
475
+ if ( recordFormat ) {
476
+ lines . push ( ` A R ${ recordFormat } ` ) ;
445
477
}
446
478
447
- for ( const keyword of recordFormat . keywords ) {
479
+ for ( const keyword of keywords ) {
448
480
// TODO: support conditions
449
- lines . push (
450
- ` A ${ keyword . name } ${ keyword . value ? `(${ keyword . value } )` : `` } ` ,
451
- ) ;
481
+ lines . push ( ...DisplayFile . getLinesForKeyword ( keyword ) ) ;
452
482
}
453
483
454
484
return lines ;
455
485
}
456
486
457
- public getRangeForFormat ( recordFormat : string ) : DdsLineRange | undefined {
487
+ public getHeaderRangeForFormat ( recordFormat : string ) : DdsLineRange | undefined {
458
488
let range : DdsLineRange | undefined = undefined ;
459
489
const currentFormatI = this . formats . findIndex ( format => format . name === recordFormat ) ;
460
490
if ( currentFormatI > 0 ) {
@@ -474,9 +504,9 @@ export class DisplayFile {
474
504
}
475
505
476
506
// 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 ) ;
480
510
481
511
if ( range ) {
482
512
range = {
0 commit comments