<commit_before>export const BEGIN = '@@optimist/BEGIN';
export const COMMIT = '@@optimist/COMMIT';
export const REVERT = '@@optimist/REVERT';

export const ensureState = state => {
  if (state instanceof Object) {
    if (Array.isArray(state.history)) {
      return state.current;
    }
  }
  return state;
};

const createState = state => ({
  beforeState: undefined,
  history: [],
  current: state
});

const shift = arr => {
    const newArr = arr.slice();
    newArr.shift();
    return newArr;
}

const findIndex = (arr, fn) => {
  const l = arr.length;
  for (let i = 0; i < l; i++) {
    if (fn(arr[i])) {
      return i;
    }
  }
  return -1;
}

const applyCommit = (state, commitId, reducer) => {
  const { history } = state;
  // If the action to commit is the first in the queue (most common scenario)
  if (history[0].meta.optimistic.id === commitId) {
    const historyWithoutCommit = shift(history);
    const nextOptimisticIndex = findIndex(historyWithoutCommit, action => action.meta && action.meta.optimistic && !action.meta.optimistic.isNotOptimistic && action.meta.optimistic.id);
    // If this is the only optimistic item in the queue, we're done!
    if (nextOptimisticIndex === -1) {
      return {
        ...state,
        history: [],
        beforeState: undefined
      };
    }
    // Create a new history starting with the next one
    const newHistory = historyWithoutCommit.slice(nextOptimisticIndex);
    // And run every action up until that next one to get the new beforeState
    const newBeforeState = history.reduce((mutState, action, index) => {
      return index <= nextOptimisticIndex ? reducer(mutState, action) : mutState;
    }, state.beforeState);

    return {
      ...state,
      history: newHistory,
      beforeState: newBeforeState
    };
  } else {
    // If the committed action isn't the first in the queue, find out where it is
    const actionToCommit = history.find(action => action.meta && action.meta.optimistic && action.meta.optimistic.id === commitId);
    if (!actionToCommit) {
      console.error(`@@optimist: Failed commit. Transaction #${commitId} does not exist!`);
    }
    // Make it a regular non-optimistic action
    const newAction = Object.assign({}, actionToCommit[1], {
      meta: Object.assign({}, actionToCommit[1].meta,
        {optimistic: null})
    });
    return {
      ...state,
      history: [newAction].concat(state.history.slice(1))
    };
  }
};

const applyRevert = (state, revertId, reducer) => {
  const { beforeState, history } = state;
  let newHistory;
  // If the action to revert is the first in the queue (most common scenario)
  if (history[0].meta.optimistic.id === revertId) {
    const historyWithoutRevert = shift(history);
    const nextOptimisticIndex = findIndex(historyWithoutRevert, action => action.meta && action.meta.optimistic && !action.meta.optimistic.isNotOptimistic && action.meta.optimistic.id);
    // If this is the only optimistic action in the queue, we're done!
    if (nextOptimisticIndex === -1) {
      return {
        ...state,
        history: [],
        current: historyWithoutRevert.reduce((s, action) => reducer(s, action), beforeState),
        beforeState: undefined
      };
    }<commit_msg>remove need for array  function<commit_after>export const BEGIN = '@@optimist/BEGIN';
export const COMMIT = '@@optimist/COMMIT';
export const REVERT = '@@optimist/REVERT';

export const ensureState = state => {
  if (state instanceof Object) {
    if (Array.isArray(state.history)) {
      return state.current;
    }
  }
  return state;
};

const createState = state => ({
  beforeState: undefined,
  history: [],
  current: state
});

const findIndex = (arr, fn) => {
  const l = arr.length;
  for (let i = 0; i < l; i++) {
    if (fn(arr[i])) {
      return i;
    }
  }
  return -1;
}

const applyCommit = (state, commitId, reducer) => {
  const { history } = state;
  // If the action to commit is the first in the queue (most common scenario)
  if (history[0].meta.optimistic.id === commitId) {
    const historyWithoutCommit = history.slice(1);
    const nextOptimisticIndex = findIndex(historyWithoutCommit, action => action.meta && action.meta.optimistic && !action.meta.optimistic.isNotOptimistic && action.meta.optimistic.id);
    // If this is the only optimistic item in the queue, we're done!
    if (nextOptimisticIndex === -1) {
      return {
        ...state,
        history: [],
        beforeState: undefined
      };
    }
    // Create a new history starting with the next one
    const newHistory = historyWithoutCommit.slice(nextOptimisticIndex);
    // And run every action up until that next one to get the new beforeState
    const newBeforeState = history.reduce((mutState, action, index) => {
      return index <= nextOptimisticIndex ? reducer(mutState, action) : mutState;
    }, state.beforeState);

    return {
      ...state,
      history: newHistory,
      beforeState: newBeforeState
    };
  } else {
    // If the committed action isn't the first in the queue, find out where it is
    const actionToCommit = history.find(action => action.meta && action.meta.optimistic && action.meta.optimistic.id === commitId);
    if (!actionToCommit) {
      console.error(`@@optimist: Failed commit. Transaction #${commitId} does not exist!`);
    }
    // Make it a regular non-optimistic action
    const newAction = Object.assign({}, actionToCommit[1], {
      meta: Object.assign({}, actionToCommit[1].meta,
        {optimistic: null})
    });
    return {
      ...state,
      history: [newAction].concat(state.history.slice(1))
    };
  }
};

const applyRevert = (state, revertId, reducer) => {
  const { beforeState, history } = state;
  let newHistory;
  // If the action to revert is the first in the queue (most common scenario)
  if (history[0].meta.optimistic.id === revertId) {
    const historyWithoutRevert = history.slice(1);
    const nextOptimisticIndex = findIndex(historyWithoutRevert, action => action.meta && action.meta.optimistic && !action.meta.optimistic.isNotOptimistic && action.meta.optimistic.id);
    // If this is the only optimistic action in the queue, we're done!
    if (nextOptimisticIndex === -1) {
      return {
        ...state,
        history: [],
        current: historyWithoutRevert.reduce((s, action) => reducer(s, action), beforeState),
        beforeState: undefined
      };
    }
{'fraction_non_alphanumeric': 0.12896670314209785, 'fraction_numerical': 0.003126465530717524, 'mean_word_length': 3.0777565328234546, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, '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': '449547', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1909, 'n_words': 555}