function removeMissingColumns()

in packages/tablesorter-powerbi/src/ConfigBuilder.ts [266:321]


function removeMissingColumns(config: ITableSorterConfiguration, columns: ITableSorterColumn[]) {
    "use strict";
    listDiff<ITableSorterColumn>(config.columns.slice(0), columns, {
        /**
         * Returns true if item one equals item two
         */
        equals: (one, two) => one.column === two.column,

        /**
         * Gets called when the given item was removed
         */
        onRemove: (item) => {
            for (let i = 0; i < config.columns.length; i++) {
                if (config.columns[i].column === item.column) {
                    config.columns.splice(i, 1);
                    break;
                }
            }
        },

        /**
         * Gets called when the given item was added
         */
        onAdd: (item) => {
            config.columns.push(item);
            if (config.layout && config.layout.primary) {
                // If it is a confidence column, then try to find the best spot
                let idx: number;
                if (item["isConfidence"]) {
                    config.layout.primary.some((c: ITableSorterLayoutColumn, i: number) => {
                        // The column may not have a 'column' property if it is a stacked column
                        if (c.column && c.column.indexOf(GENERATED_COLUMN_NAME_PREFIX) >= 0) {
                            const bucket = parseFloat(c.column.split(GENERATED_COLUMN_NAME_PREFIX)[1]);
                            if (bucket >= item["bucket"]) {
                                idx = i;
                                return true;
                            }
                        }
                    });
                }

                const newLayoutCol = {
                    // color: item.color,
                    width: item.width || 100,
                    column: item.column,
                    type: item.type,
                };
                if (idx) {
                    config.layout.primary.splice(idx, 0, newLayoutCol);
                } else {
                    config.layout.primary.push(newLayoutCol);
                }
            }
        },
    });
}