All files / src/variables parser.ts

88.89% Statements 16/18
50% Branches 1/2
100% Functions 1/1
88.89% Lines 16/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 401x 1x 1x 1x 1x 1x 1x 1x       1x                   1x 40x 106x 106x 40x   66x 66x                      
import { CustomVariable, InvalidDefinitionError } from "./types/base";
import { BooleanCustomVariable } from "./types/boolean";
import { DateCustomVariable } from "./types/date";
import { EnumCustomVariable } from "./types/enum";
import { InvalidCustomVariable } from "./types/invalid";
import { NumberCustomVariable } from "./types/number";
import { TextCustomVariable } from "./types/text";
import { TimeCustomVariable } from "./types/time";
 
// NOTE - InvalidCustomVariable should be at the last of the list
// because it accepts any definition.
const VARIABLE_TYPES = [
    TextCustomVariable,
    NumberCustomVariable,
    BooleanCustomVariable,
    DateCustomVariable,
    TimeCustomVariable,
    EnumCustomVariable,
    InvalidCustomVariable
];
 
export const getVariableFromDefinition = (name: string, definition: unknown): CustomVariable => {
    for (const variableType of VARIABLE_TYPES) {
        try {
            const variable = variableType.createFromDefinition(name, definition);
            return variable;
        } catch (err) {
            Eif (err instanceof InvalidDefinitionError) {
                continue;
            }
 
            throw err;
        }
    }
 
    // This ideally should never happen. "InvalidCustomVariable" accepts
    // all definitions.
    throw Error("No valid definition for variable: " + name);
}