import DataType from "../DataType";

export default class BoolType extends DataType<boolean>{
    public sanitize(value: any): boolean|null|undefined {
        if (value == null) return value;
        if (typeof value === "number") return value !== 0;
        if (typeof value === "boolean") return value;
        if (typeof value === "string") return this.getFromString(value);
        console.warn("Value is not a boolean");
        console.warn(value);
        throw new Error("Value is not a boolean");
    }

    protected getFromString(value: string): boolean {
        value = value.toLowerCase();
        return value === "true" ||
            value === "1" ||
            value === "on" ||
            value === "yes" ||
            value === "y" ||
            value === "ja";
    }
}