All files / src/libs/settings mergeSettingStrategy.ts

94.11% Statements 16/17
87.5% Branches 7/8
100% Functions 3/3
93.75% Lines 15/16

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              11x 3x 3x   3x 4x   4x 1x 1x   1x           3x       3x 2x     3x     8x       8x    
import isObject from 'lodash/isObject';
import mergeWith from 'lodash/mergeWith';
 
import {ChartKitPlugin} from 'src/types';
 
// @ts-ignore
export function mergeSettingStrategy(objValue: any, srcValue: any, key: string): any {
    if (key === 'plugins') {
        const currentPlugins: ChartKitPlugin[] = [...objValue];
        const newPlugins: ChartKitPlugin[] = [...srcValue];
        // modify existing plugins
        let newSettingsPlugins = currentPlugins.map((currentPlugin) => {
            const newPluginIndex = newPlugins.findIndex(({type}) => type === currentPlugin.type);
 
            if (newPluginIndex !== -1) {
                const newPlugin = newPlugins[newPluginIndex];
                newPlugins.splice(newPluginIndex, 1);
 
                return {
                    type: currentPlugin.type,
                    renderer: newPlugin.renderer,
                };
            }
 
            return currentPlugin;
        });
 
        // add new plugins if it exist after modified
        if (newPlugins.length > 0) {
            newSettingsPlugins = [...newSettingsPlugins, ...newPlugins];
        }
 
        return newSettingsPlugins;
    }
 
    Iif (isObject(objValue)) {
        return mergeWith(objValue, srcValue, mergeSettingStrategy);
    }
 
    return srcValue;
}