<commit_before>import { Directions } from '../actions';
import Immutable, { Map, List } from 'immutable';
import { Suits, Ranks } from '../constants';
import range from 'lodash/utility/range';
import shuffle from 'lodash/collection/shuffle';
import first from 'lodash/array/first';
import flatten from 'lodash/array/flatten';

const orderedDeck = flatten(
    Object.keys(Suits).map(suit => Ranks.map(rank => ({ rank, suit })))
);

function getInitialState() {
    const cards = shuffle(orderedDeck);
    return Map({
        FOUNDATION: Map({
            HEARTS: List(),
            SPADES: List(),
            DIAMONDS: List(),
            CLUBS: List()
        }),

        PILE: getPiles(cards),
        DECK: Map({
            upturned: List(cards.slice(-1)),
            downturned: List(cards.slice(21, -1))
        })
    });
}

function getPiles (cards) {
    const deck = cards.slice();
    return List(range(0, 6).map(index => {
        const pile = deck.splice(0, index + 1);
        return List(pile.slice(0, -1).concat([{ ...pile.pop(), upturned: true}]))
    }));
}

function upturnFirstCard(cards) {
    return cards.map((card, index, pile) => {
        if (index === pile.size - 1) { return { ...card, upturned: true }; }
        else { return card; }
    });
}

function moveCards(state, action) {
    let { cards, where } = action.payload;
    const target = state.getIn(where.to).concat(cards);
    let source = state.getIn(where.from)
    if (first(where.from) === 'PILE' && first(where.to) === 'PILE' && !first(cards).isLast) {
        const index = source.findIndex(c =>
            c.suit === first(cards).suit && c.rank === first(cards).rank
        );
        cards = source.slice(index);
    }
    source = source.slice(0, -cards.length);

    if (first(where.from) === 'PILE') source = upturnFirstCard(source);
<commit_msg>Fixed bug on card move.<commit_after>import { Directions } from '../actions';
import Immutable, { Map, List } from 'immutable';
import { Suits, Ranks } from '../constants';
import range from 'lodash/utility/range';
import shuffle from 'lodash/collection/shuffle';
import first from 'lodash/array/first';
import flatten from 'lodash/array/flatten';

const orderedDeck = flatten(
    Object.keys(Suits).map(suit => Ranks.map(rank => ({ rank, suit })))
);

function getInitialState() {
    const cards = shuffle(orderedDeck);
    return Map({
        FOUNDATION: Map({
            HEARTS: List(),
            SPADES: List(),
            DIAMONDS: List(),
            CLUBS: List()
        }),

        PILE: getPiles(cards),
        DECK: Map({
            upturned: List(cards.slice(-1)),
            downturned: List(cards.slice(21, -1))
        })
    });
}

function getPiles (cards) {
    const deck = cards.slice();
    return List(range(0, 6).map(index => {
        const pile = deck.splice(0, index + 1);
        return List(pile.slice(0, -1).concat([{ ...pile.pop(), upturned: true}]))
    }));
}

function upturnFirstCard(cards) {
    return cards.map((card, index, pile) => {
        if (index === pile.size - 1) { return { ...card, upturned: true }; }
        else { return card; }
    });
}

function moveCards(state, action) {
    let { cards, where } = action.payload;
    let source = state.getIn(where.from)
    if (first(where.from) === 'PILE' && first(where.to) === 'PILE' && !first(cards).isLast) {
        const index = source.findIndex(c =>
            c.suit === first(cards).suit && c.rank === first(cards).rank
        );
        cards = source.slice(index);
    }
    const target = state.getIn(where.to).concat(cards);
    source = source.slice(0, -cards.length);

    if (first(where.from) === 'PILE') source = upturnFirstCard(source);

{'fraction_non_alphanumeric': 0.1710668103448276, 'fraction_numerical': 0.00646551724137931, 'mean_word_length': 2.654527559055118, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5188251', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1190, 'n_words': 278}