<commit_before>import * as Actions from '../actions/types';
import ActionType = Actions.ActionType;

export default function reduce(state: AppState = { containers: [], hosts: [] }, action: ActionType): AppState {
    switch (action.type) {
        case 'add-container':
            return Object.assign(
                {},
                state,
                { containers: [...state.containers, action.container] }
            )
        case 'add-host':
            return Object.assign(
                {},
                state,
                { hosts: [...state.hosts, action.host] }
            )
        case 'remove-host':
            return Object.assign(
                {},
                state,
                { hosts: state.hosts.filter(c => c.id !== action.id) }
            )
        case 'remove-container':
            return Object.assign(
                {},
                state,
                { containers: state.containers.filter(c => c.id !== action.id) }
            )
        default:
            return state;
    }
}<commit_msg>Update entity if already exists in store<commit_after>import * as Actions from '../actions/types';
import ActionType = Actions.ActionType;

export default function reduce(state: AppState = { containers: [], hosts: [] }, action: ActionType): AppState {
    switch (action.type) {
        case 'add-container':
            return Object.assign(
                {},
                state,
                { containers: addEntity(state.containers, action.container) }
            );

        case 'add-host':
            return Object.assign(
                {},
                state,
                { hosts: addEntity(state.hosts, action.host) }
            );

        case 'remove-host':
            return Object.assign(
                {},
                state,
                { hosts: state.hosts.filter(c => c.id !== action.id) }
            );

        case 'remove-container':
            return Object.assign(
                {},
                state,
                { containers: state.containers.filter(c => c.id !== action.id) }
            );

        default:
            return state;
    }
}

type Id = { id?: string | number }

/**
 * Destructive
 * Add or update
 */
function addEntity<T extends Id>(entities: Array<T>, entity: T) {
    const existing = entities.find(c => c.id === entity.id);
    if (existing) {
        Object.assign(existing, entity);
        return entities;
    }
    return entities.concat(entity);
}

function removeEntity<T extends Id>(state: AppState, entities: Array<T>, entityId: string | number) {
    const filtered = entities.filter(entity => entity.id !== entityId);
    return filtered;
}
{'fraction_non_alphanumeric': 0.1256931608133087, 'fraction_numerical': 0.0, 'mean_word_length': 1.5456255879586076, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1676294', 'n_tokens_mistral': 713, 'n_tokens_neox': 678, 'n_words': 166}