<commit_before>import shortId from 'shortid';
import { load, store, remove, ids } from '../storage';
import {
  ADD_ITEM_TO_SHOPPING_CART, LOAD_SHOPPING_CART, ORDER_FINISHED, REMOVE_ITEM_FROM_SHOPPING_CART,
  SET_SHOPPING_CART_AMOUNT,
} from '../actions';

const initialState = {
  id: shortId.generate(),
  items: [],
};

function updateItem(item, id, amount) {
  if (item.product._id === id) {
    if (amount) {
      item.amount = amount;
    } else {
      item.amount += 1;
    }
  }

  return item;
}

function insertOrUpdateItem(items, product, amount) {
  if (amount === 0) {
    return items.filter(item => item.product._id !== product._id);
  }

  if (items.some(item => item.product._id === product._id)) {
    return items.map(item => updateItem(item, product._id, amount));
  }

  return items.concat({
    product,
    amount: 1,
  });
}

export default function auth(state = initialState, { type, data }) {
  switch (type) {
    case LOAD_SHOPPING_CART: {
      const cart = load(ids.SHOPPING_CART) || state;

      if (!shortId.isValid(cart.id)) {
        cart.id = shortId.generate();
        store(ids.SHOPPING_CART, cart);
      }

      return cart;
    }
    case ADD_ITEM_TO_SHOPPING_CART: {
      state.items = insertOrUpdateItem(state.items, data);
      const newState = Object.assign({}, state);

      store(ids.SHOPPING_CART, newState);

      return newState;
    }
    case REMOVE_ITEM_FROM_SHOPPING_CART: {
      const filteredItems = state.items.filter(item => item.product._id !== data._id);

      store(ids.SHOPPING_CART, filteredItems);

      return filteredItems;
    }
    case SET_SHOPPING_CART_AMOUNT: {
      state.items = insertOrUpdateItem(state.items, data.product, data.amount);
      const newState = Object.assign({}, state);

      store(ids.SHOPPING_CART, newState);

      return newState;
    }
    case ORDER_FINISHED: {
      remove(ids.SHOPPING_CART);
      return initialState;
    }
    default: {
      return state;
    }
  }
}<commit_msg>don't update initial state<commit_after>import shortId from 'shortid';
import { load, store, remove, ids } from '../storage';
import {
  ADD_ITEM_TO_SHOPPING_CART, LOAD_SHOPPING_CART, ORDER_FINISHED, REMOVE_ITEM_FROM_SHOPPING_CART,
  SET_SHOPPING_CART_AMOUNT,
} from '../actions';

const initialState = {
  id: shortId.generate(),
  items: [],
};

function updateItem(item, id, amount) {
  if (item.product._id === id) {
    if (amount) {
      item.amount = amount;
    } else {
      item.amount += 1;
    }
  }

  return item;
}

function insertOrUpdateItem(items, product, amount) {
  if (amount === 0) {
    return items.filter(item => item.product._id !== product._id);
  }

  if (items.some(item => item.product._id === product._id)) {
    return items.map(item => updateItem(item, product._id, amount));
  }

  return items.concat({
    product,
    amount: 1,
  });
}

export default function auth(state = initialState, { type, data }) {
  switch (type) {
    case LOAD_SHOPPING_CART: {
      const cart = load(ids.SHOPPING_CART) || state;

      if (!shortId.isValid(cart.id)) {
        cart.id = shortId.generate();
        store(ids.SHOPPING_CART, cart);
      }

      return cart;
    }
    case ADD_ITEM_TO_SHOPPING_CART: {
      const newState = Object.assign({}, state);
      newState.items = insertOrUpdateItem(newState.items, data);

      store(ids.SHOPPING_CART, newState);

      return newState;
    }
    case REMOVE_ITEM_FROM_SHOPPING_CART: {
      const filteredItems = state.items.filter(item => item.product._id !== data._id);

      store(ids.SHOPPING_CART, filteredItems);

      return filteredItems;
    }
    case SET_SHOPPING_CART_AMOUNT: {
      state.items = insertOrUpdateItem(state.items, data.product, data.amount);
      const newState = Object.assign({}, state);

      store(ids.SHOPPING_CART, newState);

      return newState;
    }
    case ORDER_FINISHED: {
      remove(ids.SHOPPING_CART);
      return Object.assign({}, initialState, {
        id: shortId.generate(),
      });
    }
    default: {
      return state;
    }
  }
}
{'fraction_non_alphanumeric': 0.13220505273485406, 'fraction_numerical': 0.0014716703458425313, 'mean_word_length': 2.9941234084231145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5993270', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1373, 'n_words': 274}