All files / src/parameter ParameterContainer.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 2/2
100% Lines 12/12

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    1x     9x         15x 3x 1x 2x     12x   12x 2x   10x 1x   9x      
import type { ValidParameterTypeName, RawParameterType } from './ValidParameterTypeName';
 
export class ParameterContainer {
 
    public constructor(
        private readonly data: Record<string, unknown>
    ) {}
 
    public parameter<TypeName extends ValidParameterTypeName>(key: PropertyKey, expectedTypes: Set<TypeName>): RawParameterType<TypeName> {
 
        if (!(key in this.data)) {
            if ((expectedTypes as Set<string>).has('undefined'))
                return null as RawParameterType<TypeName>;
            throw new Error(`No ${String(key)} in parameters.`);
        }
 
        const parameter = (this.data as Record<PropertyKey, unknown>)[key];
 
        if (!(expectedTypes as Set<string>).has(typeof parameter))
            throw new Error(`Parameter ${String(key)} has an invalid type: ${typeof parameter}`);
 
        if (parameter === undefined)
            return null as RawParameterType<TypeName>;
 
        return parameter as RawParameterType<TypeName>;
    }
}