All files / src/plugins/indicator normalize.ts

95.83% Statements 23/24
97.14% Branches 34/35
100% Functions 5/5
95.65% Lines 22/23

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                    28x 18x   10x 10x               23x 23x 15x 2x 2x   13x   8x           1x               7x 7x                     18x 23x   23x 11x     12x                 23x 6x   12x    
import type {
    IndicatorFormatOptions,
    IndicatorWidgetConfig,
    IndicatorWidgetConfigItem,
    NormalizedIndicatorWidgetConfig,
    NormalizedIndicatorWidgetConfigItem,
} from './types';
 
/** Return the original formatNumber options, preserving application metadata and object identity. */
export function getValueFormatOptions(item: IndicatorWidgetConfigItem): IndicatorFormatOptions {
    if (item.value) {
        return item.value;
    }
    Eif (item.content) {
        return item.content.current;
    }
    throw new TypeError('An indicator item requires value or content.current.');
}
 
function normalizeValue(
    item: IndicatorWidgetConfigItem,
): NormalizedIndicatorWidgetConfigItem['value'] {
    const value = getValueFormatOptions(item);
    if (item.value) {
        if (value.color === undefined && item.color !== undefined) {
            const {value: rawValue, formatNumber, numberFormat, ...rest} = item.value;
            return {...rest, value: rawValue, formatNumber, numberFormat, color: item.color};
        }
        return value;
    }
    if (
        !('color' in value) &&
        !('formatNumber' in value) &&
        !('numberFormat' in value) &&
        item.color === undefined
    ) {
        return value;
    }
    const {
        value: rawValue,
        color: _color,
        formatNumber: _formatNumber,
        numberFormat: _numberFormat,
        ...rest
    } = value;
    return item.color === undefined
        ? {...rest, value: rawValue}
        : {...rest, value: rawValue, color: item.color};
}
 
/**
 * Normalize indicator configuration without mutating input, invoking callbacks or logging.
 */
export function normalizeIndicatorData(
    input: IndicatorWidgetConfig,
): NormalizedIndicatorWidgetConfig {
    const data = input.data?.map((item): NormalizedIndicatorWidgetConfigItem => {
        const value = normalizeValue(item);
 
        if (item.value === value && !('content' in item) && !('color' in item)) {
            return item as NormalizedIndicatorWidgetConfigItem;
        }
 
        return {
            value,
            ...(item.secondaryValue ? {secondaryValue: item.secondaryValue} : {}),
            ...(item.title === undefined ? {} : {title: item.title}),
            ...(item.size === undefined ? {} : {size: item.size}),
            ...(item.nowrap === undefined ? {} : {nowrap: item.nowrap}),
        };
    });
 
    if (data?.every((item, index) => item === input.data?.[index]) ?? true) {
        return input as NormalizedIndicatorWidgetConfig;
    }
    return {...input, data};
}