<commit_before>/*
 * Vuex store for kanban lists
 */

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const KANBAN_TODO   = "_KANBAN_TODO"
const KANBAN_DOING  = "_KANBAN_DOING"
const KANBAN_DONE   = "_KANBAN_DONE" 

const add = (list, text, index) => {
    list.cards.splice(index, 0, text)
}

const remove = (list, index) => {
    list.cards.splice(index, 1)
}

const listForCard = (state, card) => {
    return state.board.find(l => l.id === card.listId)
}

const store = new Vuex.Store({
    state: {
        board: [
            {
                id: KANBAN_TODO,
                header: "Para executar",
                cards: [
                    "do thing 1", 
                    "do thing 2",
                    "thing a",
                    "thing b",
                    "thing c",
                    "thing d",
                    "thing e",
                    "thing things 1",
                    "thing things 2",
                    "thing things 3",
                    "thing test thing a",
                    "thing test thing b",
                    "thing test thing c",
                    "thing test thing d",
                    "thing test thing e",
                    "thing test thing f",
                    "thing test thing g",
                    "thing test thing h",
                    "thing test thing i",
                    "thing test thing j",
                    "thing things a",
                    "thing things b",
                    "thing things c",
                    "thing things d",
                    "thing things e",
                    "a longer thing to see how this thing draws in the thing"
                ]
            },
            {
                id: KANBAN_DOING,
                header: "Em andamento",
                cards: ["a longer description to test line breakage in this card"]
            },
            {
                id: KANBAN_DONE,
                header: "Finalizado",
                cards: ["e", "f"]
            }
        ]
    },
    mutations: {
        delete(state, payload) {
            let card = payload.card
            remove(listForCard(state, card), card.index)
        },
        move(state, payload) {
            let source = payload.from
            let destination = payload.to

            if (source.listId !== destination.listId) {
                //moving between differing lists
                remove(listForCard(state, source), source.index)
                add(listForCard(state, destination), source.text, destination.index)
            }
            else {
                //moving within a single list
                if (source.index !== destination.index) {
                    let destinationIndex = destination.index - 1 < 0 ? 0 : destination.index - 1
                    remove(listForCard(state, source), source.index)
                    add(listForCard(state, destination), source.text, destinationIndex)
                }
            }
        }
    }
})

export default store<commit_msg>Fix off-by-one error when moving source cards within the same list to destinations with smaller indexes<commit_after>/*
 * Vuex store for kanban lists
 */

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const KANBAN_TODO   = "_KANBAN_TODO"
const KANBAN_DOING  = "_KANBAN_DOING"
const KANBAN_DONE   = "_KANBAN_DONE" 

const add = (list, text, index) => {
    list.cards.splice(index, 0, text)
    console.log(`card added at position ${index}`)
}

const remove = (list, index) => {
    list.cards.splice(index, 1)
}

const listForCard = (state, card) => {
    return state.board.find(l => l.id === card.listId)
}

const store = new Vuex.Store({
    state: {
        board: [
            {
                id: KANBAN_TODO,
                header: "Para executar",
                cards: [
                    "do thing 1", 
                    "do thing 2",
                    "thing a",
                    "thing b",
                    "thing c",
                    "thing d",
                    "thing e",
                    "thing things 1",
                    "thing things 2",
                    "thing things 3",
                    "thing test thing a",
                    "thing test thing b",
                    "thing test thing c",
                    "thing test thing d",
                    "thing test thing e",
                    "thing test thing f",
                    "thing test thing g",
                    "thing test thing h",
                    "thing test thing i",
                    "thing test thing j",
                    "thing things a",
                    "thing things b",
                    "thing things c",
                    "thing things d",
                    "thing things e",
                    "a longer thing to see how this thing draws in the thing"
                ]
            },
            {
                id: KANBAN_DOING,
                header: "Em andamento",
                cards: ["a longer description to test line breakage in this card"]
            },
            {
                id: KANBAN_DONE,
                header: "Finalizado",
                cards: ["e", "f"]
            }
        ]
    },
    mutations: {
        delete(state, payload) {
            let card = payload.card
            remove(listForCard(state, card), card.index)
        },
        move(state, payload) {
            let source = payload.from
            let destination = payload.to

            if (source.listId !== destination.listId) {
                //moving between differing lists
                remove(listForCard(state, source), source.index)
                add(listForCard(state, destination), source.text, destination.index)
            }
            else {
                //moving within a single list
                if (source.index !== destination.index) {
                    let destinationIndex = destination.index - 1 < 0 ? 0 : destination.index - 1

                    if (source.index > destination.index && destination.index > 0) {
                        destinationIndex += 1
                    }

                    remove(listForCard(state, source), source.index)
                    add(listForCard(state, destination), source.text, destinationIndex)
                }
            }
        }
    }
})

export default store
{'fraction_non_alphanumeric': 0.0955743879472693, 'fraction_numerical': 0.003766478342749529, 'mean_word_length': 1.164741847826087, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1591217', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1609, 'n_words': 488}