This repository was archived by the owner on Oct 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathvariable-info.interface.ts
More file actions
152 lines (133 loc) · 4.42 KB
/
Copy pathvariable-info.interface.ts
File metadata and controls
152 lines (133 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
export type VariableInfoValueType = string | number | boolean;
export interface VariableValue {
value: VariableInfoValueType;
label: string;
}
export interface VariableInfo {
id: string;
alias?: string;
type: 'STRING' | 'INTEGER' | 'NUMBER' | 'BOOLEAN' | 'ATTACHMENT' | 'JSON' | 'NO_VALUE' | 'CODED';
format: 'TEXT_SELECTION' | 'IMAGE' | 'CAPTURE_IMAGE' | 'AUDIO' | 'GGB_FILE' | 'NON_NEGATIVE' |
'LATEX' | 'MATH_ML' | 'MATH_TABLE' | 'MATH_TEXT_MIX' | 'GGB_VARIABLE' |
'CHEM_ELEMENT_SYMBOLS' | 'CHEM_MOLECULE' | '';
multiple: boolean;
nullable: boolean;
values: VariableValue[];
valuePositionLabels: string[];
valuesComplete?: boolean;
}
export class VariableList {
variables: VariableInfo[] = [];
constructor(varInfos: VariableInfo[] | null) {
// later clean/validate if necessary
this.variables = varInfos || [];
}
}
export type VariableValidationErrorCode =
'EMPTY_IDENTIFIER' |
'INVALID_CHARACTERS' |
'DUPLICATE_ID' |
'DUPLICATE_ALIAS' |
'PUBLIC_IDENTIFIER_COLLISION';
export interface VariableValidationError {
code: VariableValidationErrorCode;
variableIndex: number;
property: 'id' | 'alias';
value: unknown;
conflictingVariableIndex?: number;
}
type VariableIdentifiers = Pick<VariableInfo, 'id' | 'alias'>;
type IdentifierProperty = VariableValidationError['property'];
const variableIdentifierPattern = /^[0-9A-Za-z_-]+$/;
/**
* Checks a single variable identifier without modifying it.
*/
export function isValidVariableIdentifier(value: unknown): value is string {
return typeof value === 'string' && variableIdentifierPattern.test(value);
}
/**
* Validates identifiers and their uniqueness within a complete variable list.
* Public identifiers are aliases when present and technical ids otherwise.
*/
export function validateVariableList(
variables: ReadonlyArray<VariableIdentifiers>
): VariableValidationError[] {
const errors: VariableValidationError[] = [];
variables.forEach((variable, variableIndex) => {
addIdentifierError(errors, variable.id, variableIndex, 'id');
if (variable.alias !== undefined) {
addIdentifierError(errors, variable.alias, variableIndex, 'alias');
}
});
addDuplicateErrors(variables, 'id', 'DUPLICATE_ID', errors);
addDuplicateErrors(variables, 'alias', 'DUPLICATE_ALIAS', errors);
addPublicIdentifierCollisionErrors(variables, errors);
return errors;
}
function addIdentifierError(
errors: VariableValidationError[],
value: unknown,
variableIndex: number,
property: IdentifierProperty
): void {
if (!isValidVariableIdentifier(value)) {
errors.push({
code: value === '' ? 'EMPTY_IDENTIFIER' : 'INVALID_CHARACTERS',
variableIndex,
property,
value
});
}
}
function addDuplicateErrors(
variables: ReadonlyArray<VariableIdentifiers>,
property: IdentifierProperty,
code: 'DUPLICATE_ID' | 'DUPLICATE_ALIAS',
errors: VariableValidationError[]
): void {
const firstIndexByIdentifier = new Map<string, number>();
variables.forEach((variable, variableIndex) => {
const value = variable[property];
if (!isValidVariableIdentifier(value)) return;
const normalizedValue = value.toLowerCase();
const conflictingVariableIndex = firstIndexByIdentifier.get(normalizedValue);
if (conflictingVariableIndex === undefined) {
firstIndexByIdentifier.set(normalizedValue, variableIndex);
} else {
errors.push({
code,
variableIndex,
property,
value,
conflictingVariableIndex
});
}
});
}
function addPublicIdentifierCollisionErrors(
variables: ReadonlyArray<VariableIdentifiers>,
errors: VariableValidationError[]
): void {
const firstPublicIdentifier = new Map<string, {
variableIndex: number;
property: IdentifierProperty;
}>();
variables.forEach((variable, variableIndex) => {
const property: IdentifierProperty = variable.alias === undefined ? 'id' : 'alias';
const value = variable[property];
if (!isValidVariableIdentifier(value)) return;
const normalizedValue = value.toLowerCase();
const conflicting = firstPublicIdentifier.get(normalizedValue);
if (!conflicting) {
firstPublicIdentifier.set(normalizedValue, { variableIndex, property });
} else if (conflicting.property !== property) {
errors.push({
code: 'PUBLIC_IDENTIFIER_COLLISION',
variableIndex,
property,
value,
conflictingVariableIndex: conflicting.variableIndex
});
}
});
}