All files / src/plugins/indicator normalize.ts

91.66% Statements 22/24
88.57% Branches 31/35
100% Functions 5/5
91.3% Lines 21/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                    13x 9x   4x 4x               8x 8x 6x 2x 2x   4x   2x                           2x 2x                     7x 8x   8x 3x     5x                 8x 2x   5x    
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;
    }
    Iif (
        !('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};
}