All files / src/utils DataItemMappingTools.ts

100% Statements 11/11
90% Branches 9/10
100% Functions 6/6
100% Lines 11/11

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 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          6x                   9x                   6x                         3x   3x 9x     3x                         2x   2x 6x         2x                                    
import { DataItem } from '../types/DataConnectorTypes';
 
/**
 * Utility tools for mapping between `EngineDataItem` and `DataItem`.
 */
export class DataItemMappingTools {
    /**
     * Checks if the given value is a `DatePropertyWrapper`.
     *
     * @param value The value to check.
     * @returns `true` if the value is a `DatePropertyWrapper`, otherwise `false`.
     */
    private isDatePropertyWrapper(
        value: string | number | boolean | DatePropertyWrapper | null,
    ): value is DatePropertyWrapper {
        return typeof value === 'object' && value?.type === 'date';
    }
 
    /**
     * Checks if the given value is a `Date` object instance.
     *
     * @param value The value to check.
     * @returns `true` if the value is a `Date` object, otherwise `false`.
     */
    private isDateObject(value: string | number | boolean | Date | null): value is Date {
        return value instanceof Date;
    }
 
    /**
     * Transforms an `EngineDataItem` into a `DataItem`.
     *
     * Converts `DatePropertyWrapper` values to JavaScript `Date` objects
     * while keeping other values unchanged.
     *
     * @param dataItem the EngineDataItem to transform.
     * @returns the resulting `DataItem` with parsed date properties.
     */
    mapEngineToDataItem(dataItem: EngineDataItem): DataItem {
        const parsedItem: DataItem = {};
 
        Object.entries(dataItem).forEach(([key, value]) => {
            parsedItem[key] = this.isDatePropertyWrapper(value) ? new Date(value.value) : value;
        });
 
        return parsedItem;
    }
 
    /**
     * Transforms a `DataItem` into an `EngineDataItem`.
     *
     * Converts JavaScript `Date` objects to `DatePropertyWrapper` objects
     * while keeping other values unchanged.
     *
     * @param dataItem the `DataItem` to transform.
     * @returns the resulting `EngineDataItem` with parsed date properties.
     */
    mapDataItemToEngine(dataItem: DataItem): EngineDataItem {
        const parsedItem: EngineDataItem = {};
 
        Object.entries(dataItem).forEach(([key, value]) => {
            parsedItem[key] = this.isDateObject(value)
                ? ({ value: value.getTime(), type: 'date' } as DatePropertyWrapper)
                : value;
        });
 
        return parsedItem;
    }
}
 
/**
 * Internal type used to wrap date objects on the engine side.
 */
export type DatePropertyWrapper = {
    value: number;
    type: 'date';
};
 
/**
 * Engine data item type.
 */
export type EngineDataItem = {
    [key: string]: string | number | boolean | DatePropertyWrapper | null;
};