<commit_before>import {
  ADD_TO_QUEUE,
  REMOVE_FROM_QUEUE,
  CLEAR_QUEUE,
  ADD_STREAMS_TO_QUEUE_ITEM,
  REPLACE_STREAMS_IN_QUEUE_ITEM,
  NEXT_SONG,
  PREVIOUS_SONG,
  SELECT_SONG,
  SWAP_SONGS
} from '../actions/queue';

var _ = require('lodash');

const initialState = {
  queueItems: [],
  currentSong: 0
};

function findQueueItemIndex(queueItems, item) {
  return _.findIndex(queueItems, i => i.uuid===item.uuid);
}

export default function QueueReducer(state=initialState, action) {
  let replaceIx, removeIx, newQueue;
  switch (action.type) {
  case ADD_TO_QUEUE:
    return Object.assign({}, state, {
      queueItems: _.union(state.queueItems, [action.payload])
    });
  case REMOVE_FROM_QUEUE:
    let newCurrent = state.currentSong;
    removeIx = findQueueItemIndex(state.queueItems, action.payload);
    newQueue = _.cloneDeep(state.queueItems);
    newQueue = _.filter(newQueue, item => action.payload.uuid!==item.uuid);
    if (removeIx < state.currentSong) {
      newCurrent--;
    }
    return Object.assign({}, state, {
      queueItems: newQueue,
      currentSong: newCurrent
    });
  case CLEAR_QUEUE:
    return Object.assign({}, state, {
      queueItems: []
    });
  case ADD_STREAMS_TO_QUEUE_ITEM:
    replaceIx = findQueueItemIndex(state.queueItems, action.payload);
    newQueue = _.cloneDeep(state.queueItems);
    newQueue[replaceIx] = Object.assign({}, newQueue[replaceIx], action.payload);

    return Object.assign({}, state, {
      queueItems: newQueue
    });
  case REPLACE_STREAMS_IN_QUEUE_ITEM:
    replaceIx = findQueueItemIndex(state.queueItems, action.payload);
    newQueue = _.cloneDeep(state.queueItems);
    newQueue[replaceIx] = action.payload;
    return Object.assign({}, state, {
      queueItems: newQueue
    });
  case NEXT_SONG:
    return Object.assign({}, state, {
      currentSong: (state.currentSong+1) % state.queueItems.length
    });
  case PREVIOUS_SONG:
    return Object.assign({}, state, {
      currentSong: ((state.currentSong-1) % state.queueItems.length + state.queueItems.length) % state.queueItems.length
    });
  case SELECT_SONG:
    return Object.assign({}, state, {
      currentSong: action.payload
    });
  case SWAP_SONGS:
    newQueue = _.cloneDeep(state.queueItems);
    let temp = newQueue[action.payload.itemFrom];
    newQueue[action.payload.itemFrom] = newQueue[action.payload.itemTo];
    newQueue[action.payload.itemTo] = temp;
    return Object.assign({}, state, {
      queueItems: newQueue
    });
  default:
    return state;<commit_msg>Simplified Queue reducer for maintainability<commit_after>import {
  ADD_TO_QUEUE,
  REMOVE_FROM_QUEUE,
  CLEAR_QUEUE,
  ADD_STREAMS_TO_QUEUE_ITEM,
  REPLACE_STREAMS_IN_QUEUE_ITEM,
  NEXT_SONG,
  PREVIOUS_SONG,
  SELECT_SONG,
  SWAP_SONGS,
} from '../actions/queue';

var _ = require('lodash');

const initialState = {
  queueItems: [],
  currentSong: 0,
};

function findQueueItemIndex(queueItems, item) {
  return _.findIndex(queueItems, i => i.uuid === item.uuid);
}

function reduceAddToQueue(state, action) {
  return Object.assign({}, state, {
    queueItems: _.union(state.queueItems, [action.payload]),
  });
}

function reduceRemoveFromQueue(state, action) {
  let removeIx, newQueue;
  let newCurrent = state.currentSong;
  removeIx = findQueueItemIndex(state.queueItems, action.payload);
  newQueue = _.cloneDeep(state.queueItems);
  newQueue = _.filter(newQueue, item => action.payload.uuid !== item.uuid);
  if (removeIx < state.currentSong) {
    newCurrent--;
  }
  return Object.assign({}, state, {
    queueItems: newQueue,
    currentSong: newCurrent,
  });
}

function reduceClearQueue(state, action) {
  return Object.assign({}, state, {
    queueItems: [],
  });
}

function reduceAddStreamsToQueueItem(state, action) {
  let replaceIx, newQueue;
  replaceIx = findQueueItemIndex(state.queueItems, action.payload);
  newQueue = _.cloneDeep(state.queueItems);
  newQueue[replaceIx] = Object.assign({}, newQueue[replaceIx], action.payload);

  return Object.assign({}, state, {
    queueItems: newQueue,
  });
}

function reduceReplaceStreamsInQueueItem(state, action) {
  let replaceIx, newQueue;
  replaceIx = findQueueItemIndex(state.queueItems, action.payload);
  newQueue = _.cloneDeep(state.queueItems);
  newQueue[replaceIx] = action.payload;
  return Object.assign({}, state, {
    queueItems: newQueue,
  });
}

function reduceSelectSong(state, action) {
  return Object.assign({}, state, {
    currentSong: action.payload,
  });
}

function reduceSwapSongs(state, action) {
  let newQueue;
  newQueue = _.cloneDeep(state.queueItems);
  let temp = newQueue[action.payload.itemFrom];
  newQueue[action.payload.itemFrom] = newQueue[action.payload.itemTo];
  newQueue[action.payload.itemTo] = temp;
  return Object.assign({}, state, {
    queueItems: newQueue,
  });
}

function reduceNextSong(state, action) {
  return Object.assign({}, state, {
    currentSong: (state.currentSong + 1) % state.queueItems.length,
  });
}

function reducePreviousSong(state, action) {
  return Object.assign({}, state, {
    currentSong:
      (((state.currentSong - 1) % state.queueItems.length) +
        state.queueItems.length) %
      state.queueItems.length,
  });
}

export default function QueueReducer(state = initialState, action) {
  switch (action.type) {
    case ADD_TO_QUEUE:
      return reduceAddToQueue(state, action);
    case REMOVE_FROM_QUEUE:
      return reduceRemoveFromQueue(state, action);
    case CLEAR_QUEUE:
      return reduceClearQueue(state, action);
    case ADD_STREAMS_TO_QUEUE_ITEM:
      return reduceAddStreamsToQueueItem(state, action);
    case REPLACE_STREAMS_IN_QUEUE_ITEM:
      return reduceReplaceStreamsInQueueItem(state, action);
    case NEXT_SONG:
      return reduceNextSong(state, action);
    case PREVIOUS_SONG:
      return reducePreviousSong(state, action);
    case SELECT_SONG:
      return reduceSelectSong(state, action);
    case SWAP_SONGS:
      return reduceSwapSongs(state, action);
    default:
      return state;
{'fraction_non_alphanumeric': 0.12282157676348547, 'fraction_numerical': 0.000995850622406639, 'mean_word_length': 4.154833190761335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7447331', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1970, 'n_words': 369}