All files / sportident/src/storage SiModified.ts

100% Statements 31/31
100% Branches 7/7
100% Functions 6/6
100% Lines 31/31

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 5926x 26x   26x   131x 131x 131x 131x 131x 131x   131x       33x 3x   30x       4x 1x   3x       7x 1x   6x       283x 1x   282x 282x 3x   279x       16x 1x   15x 15x 4x   11x      
import {ISiDataType, ISiStorageData, ValueToStringError, ValueFromStringError} from './interfaces';
import {SiDataType} from './SiDataType';
 
export class SiModified<T, U> extends SiDataType<U> implements ISiDataType<U> {
    constructor(
                public readonly dataType: ISiDataType<T>,
                public readonly modifyExtracted?: (value: T) => U|undefined,
                public readonly modifyForUpdate?: (value: U) => T|undefined,
                public readonly modifiedToString?: (value: U) => string|ValueToStringError,
                public readonly modifiedFromString?: (value: string) => U|ValueFromStringError,
                public readonly modifiedIsValid?: (value: U) => boolean,
    ) {
        super();
    }
 
    typeSpecificIsValueValid(value: U): boolean {
        if (!this.modifiedIsValid) {
            return true;
        }
        return this.modifiedIsValid(value);
    }
 
    typeSpecificValueToString(value: U): string|ValueToStringError {
        if (!this.modifiedToString) {
            return new ValueToStringError('modifiedToString was not provided');
        }
        return this.modifiedToString(value);
    }
 
    typeSpecificValueFromString(string: string): U|ValueFromStringError {
        if (!this.modifiedFromString) {
            return new ValueFromStringError('modifiedFromString was not provided');
        }
        return this.modifiedFromString(string);
    }
 
    typeSpecificExtractFromData(data: ISiStorageData): U|undefined {
        if (!this.modifyExtracted) {
            return undefined;
        }
        const internalData = this.dataType.typeSpecificExtractFromData(data);
        if (internalData === undefined) {
            return undefined;
        }
        return this.modifyExtracted(internalData);
    }
 
    typeSpecificUpdateData(data: ISiStorageData, newValue: U): ISiStorageData {
        if (!this.modifyForUpdate) {
            return data;
        }
        const internalData = this.modifyForUpdate(newValue);
        if (internalData === undefined) {
            return data;
        }
        return this.dataType.typeSpecificUpdateData(data, internalData);
    }
}