<commit_before>import _ from 'lodash'

export const UPDATE_FILTER = 'VIEW_UPDATE_FILTER'
export const UPDATE_SORTER = 'VIEW_UPDATE_SORTER'
export const ADD = 'VIEW_ADD'
export const UPDATE = 'VIEW_UPDATE'
export const REMOVE = 'VIEW_REMOVE'
export const RECEIVE = 'VIEW_RECEIVE'

const defaultState = {
    data: [],
    indexOf: {},
}

export default function reduce(state = defaultState, action) {
    switch (action.type) {

        case UPDATE_FILTER:
            const data = action.list.data.filter(action.filter).sort(action.sorter)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }

        case UPDATE_SORTER:
            const data = state.data.slice().sort(action.sorter)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index]))
            }

        case ADD:
            if (state.indexOf[action.item.id] != null || !action.filter(action.item)) {
                return state
            }
            return {
                ...state,
                ...sortedInsert(state, action.item, action.sorter),
            }

        case REMOVE:
            if (state.indexOf[action.item.id] == null) {
                return state
            }
            return {
                ...state,
                ...sortedRemove(state, action.id),
            }

        case UPDATE:
            if (state.indexOf[action.item.id] == null) {
                return
            }
            const nextState = {
                ...state,
                ...sortedRemove(state, action.id),
            }
            if (!action.filter(action.item)) {
                return nextState
            }
            return {
                ...nextState,
                ...sortedInsert(nextState, action.item, action.sorter)
            }

        case RECEIVE:
            const data = action.list.data.filter(action.filter).sort(action.sorter)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }

        default:
            return state
    }
}

export function updateFilter(list, filter = defaultFilter, sorter = defaultSorter) {<commit_msg>[web] fix view data structure<commit_after>import _ from 'lodash'

export const UPDATE_FILTER = 'VIEW_UPDATE_FILTER'
export const UPDATE_SORTER = 'VIEW_UPDATE_SORTER'
export const ADD = 'VIEW_ADD'
export const UPDATE = 'VIEW_UPDATE'
export const REMOVE = 'VIEW_REMOVE'
export const RECEIVE = 'VIEW_RECEIVE'

const defaultState = {
    data: [],
    indexOf: {},
}

export default function reduce(state = defaultState, action) {
    switch (action.type) {

        case UPDATE_FILTER:
            const data = _.values(action.list.data).filter(action.filter).sort(action.sorter)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }

        case UPDATE_SORTER:
            const data = state.data.slice().sort(action.sorter)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index]))
            }

        case ADD:
            if (state.indexOf[action.item.id] != null || !action.filter(action.item)) {
                return state
            }
            return {
                ...state,
                ...sortedInsert(state, action.item, action.sorter),
            }

        case REMOVE:
            if (state.indexOf[action.item.id] == null) {
                return state
            }
            return {
                ...state,
                ...sortedRemove(state, action.id),
            }

        case UPDATE:
            if (state.indexOf[action.item.id] == null) {
                return
            }
            const nextState = {
                ...state,
                ...sortedRemove(state, action.id),
            }
            if (!action.filter(action.item)) {
                return nextState
            }
            return {
                ...nextState,
                ...sortedInsert(nextState, action.item, action.sorter)
            }

        case RECEIVE:
            const data = _.values(action.list.data).filter(action.filter).sort(action.sorter)
            return {
                ...state,
                data,
                indexOf: _.fromPairs(data.map((item, index) => [item.id, index])),
            }

        default:
            return state
    }
}

export function updateFilter(list, filter = defaultFilter, sorter = defaultSorter) {
{'fraction_non_alphanumeric': 0.1148846960167715, 'fraction_numerical': 0.0, 'mean_word_length': 1.393878575012544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3493452', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1253, 'n_words': 268}