Skip to content

Commit 0c68153

Browse files
committed
Conditional class for managing conditions
1 parent 0876b56 commit 0c68153

File tree

2 files changed

+88
-49
lines changed

2 files changed

+88
-49
lines changed

src/tests/dspf.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, describe, it } from "vitest";
2-
import { DdsLineRange, DisplayFile } from "../ui/dspf";
2+
import { Conditional, DdsLineRange, DisplayFile } from "../ui/dspf";
33

44
describe('DisplayFile tests', () => {
55

@@ -42,4 +42,19 @@ describe('DisplayFile tests', () => {
4242
expect(new Set(names).size).toBe(names.length);
4343
});
4444

45+
it('Test for Conditional class', () => {
46+
47+
let cond = new Conditional();
48+
cond.push(` N10 11N12`);
49+
50+
expect(cond.getConditions().length).toBe(1);
51+
expect(cond.getConditions().at(0)?.indicators.length).toBe(3);
52+
53+
cond.push(`O 20 21`);
54+
55+
expect(cond.getConditions().length).toBe(2);
56+
expect(cond.getConditions().at(1)?.indicators.length).toBe(2);
57+
58+
});
59+
4560
});

src/ui/dspf.ts

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class DisplayFile {
146146
this.currentField.keywords.push({
147147
name: `DATE`,
148148
value: undefined,
149-
conditions: []
149+
conditional: new Conditional()
150150
});
151151
break;
152152
case `T`: //Time
@@ -155,17 +155,15 @@ export class DisplayFile {
155155
this.currentField.keywords.push({
156156
name: `TIME`,
157157
value: undefined,
158-
conditions: []
158+
conditional: new Conditional()
159159
});
160160
break;
161161
default:
162162
this.currentField.primitiveType = `char`;
163163
break;
164164
}
165165

166-
this.currentField.conditions.push(
167-
...DisplayFile.parseConditionals(conditionals)
168-
);
166+
this.currentField.conditional.push(conditionals);
169167
}
170168
this.HandleKeywords(keywords, conditionals);
171169
}
@@ -178,9 +176,7 @@ export class DisplayFile {
178176
this.currentField.length = this.currentField.value.length;
179177
this.currentField.displayType = `const`;
180178

181-
this.currentField.conditions.push(
182-
...DisplayFile.parseConditionals(conditionals)
183-
);
179+
this.currentField.conditional.push(conditionals);
184180
}
185181
}
186182
this.HandleKeywords(keywords, conditionals);
@@ -227,42 +223,11 @@ export class DisplayFile {
227223

228224
}
229225

230-
static parseConditionals(conditionColumns: string): Conditional[] {
231-
if (conditionColumns.trim() === "") {return [];}
232-
233-
/** @type {Conditional[]} */
234-
let conditionals = [];
235-
236-
//TODO: something with condition
237-
//const condition = conditionColumns.substring(0, 1); //A (and) or O (or)
238-
239-
let current = "";
240-
let negate = false;
241-
let indicator = 0;
242-
243-
let cIndex = 1;
244-
245-
while (cIndex <= 7) {
246-
current = conditionColumns.substring(cIndex, cIndex + 3);
247-
248-
if (current.trim() !== "") {
249-
negate = (conditionColumns.substring(cIndex, cIndex + 1) === "N");
250-
indicator = Number(conditionColumns.substring(cIndex + 1, cIndex + 3));
251-
252-
conditionals.push({indicator, negate});
253-
}
254-
255-
cIndex += 3;
256-
}
257-
258-
return conditionals;
259-
}
260-
261226
static parseKeywords(keywordStrings: string[], conditionalStrings?: { [line: number]: string }) {
262-
let result: { value: string, keywords: Keyword[], conditions: Conditional[] } = {
227+
let result: { value: string, keywords: Keyword[], conditional: Conditional } = {
263228
value: ``,
264229
keywords: [],
265-
conditions: []
230+
conditional: new Conditional()
266231
};
267232

268233
const newLineMark = `~`;
@@ -330,7 +295,7 @@ export class DisplayFile {
330295
result.keywords.push({
331296
name: word.toUpperCase(),
332297
value: innerValue.length > 0 ? innerValue : undefined,
333-
conditions: conditionals ? DisplayFile.parseConditionals(conditionals) : []
298+
conditional: new Conditional(conditionals)
334299
});
335300

336301
word = ``;
@@ -564,7 +529,7 @@ export class RecordInfo {
564529
}
565530
}
566531

567-
export interface Keyword { name: string, value?: string, conditions: Conditional[] };
532+
export interface Keyword { name: string, value?: string, conditional: Conditional };
568533

569534
export type DisplayType = "input" | "output" | "both" | "const" | "hidden";
570535

@@ -577,7 +542,7 @@ export class FieldInfo {
577542
public decimals: number = 0;
578543
public position: { x: number, y: number } = { x: 0, y: 0 };
579544
public keywordStrings: { keywordLines: string[], conditionalLines: { [lineIndex: number]: string } } = { keywordLines: [], conditionalLines: {} };
580-
public conditions: Conditional[] = [];
545+
public conditional: Conditional = new Conditional();
581546
public keywords: Keyword[] = [];
582547

583548
constructor(public startRange: number, public name?: string) {}
@@ -593,7 +558,66 @@ export class FieldInfo {
593558
}
594559
}
595560

596-
export interface Conditional {
597-
indicator: number,
598-
negate: boolean
599-
}
561+
export interface Condition {
562+
indicators: Indicator[];
563+
}
564+
565+
export interface Indicator {
566+
indicator: number,
567+
negate: boolean
568+
}
569+
570+
export class Conditional {
571+
private conditions: Condition[] = [{
572+
indicators: []
573+
}];
574+
575+
constructor(indicatorStr?: string) {
576+
if (indicatorStr !== undefined) {
577+
this.push(indicatorStr);
578+
}
579+
}
580+
581+
push(indicatorStr: string) {
582+
if (indicatorStr.substring(0, 1) === `O` && this.conditions[this.conditions.length - 1].indicators.length > 0) {
583+
if (this.conditions.length >= 8) {
584+
throw new Error("Too many conditions");
585+
}
586+
this.conditions.push({indicators: []});
587+
}
588+
589+
let cIndex = 1;
590+
let current = ``;
591+
let negate = false;
592+
let indicator = 0;
593+
594+
while (cIndex <= 7) {
595+
current = indicatorStr.substring(cIndex, cIndex + 3);
596+
597+
if (current.trim() !== "") {
598+
negate = (indicatorStr.substring(cIndex, cIndex + 1) === "N");
599+
indicator = Number(indicatorStr.substring(cIndex + 1, cIndex + 3));
600+
if (indicator !== 0) {
601+
if (this.conditions[this.conditions.length - 1].indicators.length >= 8) {
602+
throw new Error("Too many option indicators specified for one condition");
603+
}
604+
this.conditions[this.conditions.length - 1].indicators.push({
605+
indicator: indicator,
606+
negate: negate
607+
})
608+
}
609+
}
610+
611+
cIndex += 3;
612+
}
613+
}
614+
615+
getConditions(): Condition[] {
616+
return this.conditions;
617+
}
618+
619+
getLines(line: string): string[] {
620+
return [];
621+
}
622+
623+
}

0 commit comments

Comments
 (0)