<commit_before>import { v4 as uuid } from 'uuid';
import { Action } from '@ngrx/store';

export const TODOS_KEY = 'EXAMPLES.TODOS';

export enum TodosActionTypes {
  ADD = '[Todos] Add',
  TOGGLE = '[Todos] Toggle',
  REMOVE_DONE = '[Todos] Remove Done',
  FILTER = '[Todos] Filter',
  PERSIST = '[Todos] Persist'
}

export class ActionTodosAdd implements Action {
  readonly type = TodosActionTypes.ADD;
  constructor(public payload: { name: string }) {}
}

export class ActionTodosToggle implements Action {
  readonly type = TodosActionTypes.TOGGLE;
  constructor(public payload: { id: string }) {}
}

export class ActionTodosRemoveDone implements Action {
  readonly type = TodosActionTypes.REMOVE_DONE;
}

export class ActionTodosFilter implements Action {
  readonly type = TodosActionTypes.FILTER;
  constructor(public payload: { filter: TodosFilter }) {}
}

export class ActionTodosPersist implements Action {
  readonly type = TodosActionTypes.PERSIST;
  constructor(public payload: { todos: Todo[] }) {}
}

export type TodosActions =
  | ActionTodosAdd
  | ActionTodosToggle
  | ActionTodosRemoveDone
  | ActionTodosFilter
  | ActionTodosPersist;

export type TodosFilter = 'ALL' | 'DONE' | 'ACTIVE';

export const initialState = {
  items: [
    { id: uuid(), name: 'Open Todo list example', done: true },
    { id: uuid(), name: 'Check the other examples', done: false },
    {
      id: uuid(),
      name: 'Use Angular ngRx Material Starter in your project',
      done: false
    }
  ],
  filter: 'ALL'
};

export const selectorTodos = state => state.examples.todos;

export function todosReducer(state = initialState, action: TodosActions) {
  switch (action.type) {
    case TodosActionTypes.ADD:
      return Object.assign({}, state, {
        items: state.items.concat({
          id: uuid(),
          name: action.payload.name,
          done: false
        })
      });

    case TodosActionTypes.TOGGLE:
      state.items.some((item: Todo) => {
        if (item.id === action.payload.id) {
          item.done = !item.done;
          return true;
        }
      });
      return Object.assign({}, state, {
        items: [...state.items]
      });

    case TodosActionTypes.REMOVE_DONE:
      return Object.assign({}, state, {
        items: state.items.filter((item: Todo) => !item.done)
      });

    case TodosActionTypes.FILTER:
      return Object.assign({}, state, { filter: action.payload.filter });

    default:
      return state;
  }
}

export interface Todo {
  id: string;
  name: string;
  done: boolean;
}<commit_msg>fix(todos): eliminate input mutation from the reducer<commit_after>import { v4 as uuid } from 'uuid';
import { Action } from '@ngrx/store';

export const TODOS_KEY = 'EXAMPLES.TODOS';

export enum TodosActionTypes {
  ADD = '[Todos] Add',
  TOGGLE = '[Todos] Toggle',
  REMOVE_DONE = '[Todos] Remove Done',
  FILTER = '[Todos] Filter',
  PERSIST = '[Todos] Persist'
}

export class ActionTodosAdd implements Action {
  readonly type = TodosActionTypes.ADD;
  constructor(public payload: { name: string }) {}
}

export class ActionTodosToggle implements Action {
  readonly type = TodosActionTypes.TOGGLE;
  constructor(public payload: { id: string }) {}
}

export class ActionTodosRemoveDone implements Action {
  readonly type = TodosActionTypes.REMOVE_DONE;
}

export class ActionTodosFilter implements Action {
  readonly type = TodosActionTypes.FILTER;
  constructor(public payload: { filter: TodosFilter }) {}
}

export class ActionTodosPersist implements Action {
  readonly type = TodosActionTypes.PERSIST;
  constructor(public payload: { todos: Todo[] }) {}
}

export type TodosActions =
  | ActionTodosAdd
  | ActionTodosToggle
  | ActionTodosRemoveDone
  | ActionTodosFilter
  | ActionTodosPersist;

export type TodosFilter = 'ALL' | 'DONE' | 'ACTIVE';

export const initialState = {
  items: [
    { id: uuid(), name: 'Open Todo list example', done: true },
    { id: uuid(), name: 'Check the other examples', done: false },
    {
      id: uuid(),
      name: 'Use Angular ngRx Material Starter in your project',
      done: false
    }
  ],
  filter: 'ALL'
};

export const selectorTodos = state => state.examples.todos;

export function todosReducer(state = initialState, action: TodosActions) {
  switch (action.type) {
    case TodosActionTypes.ADD:
      return Object.assign({}, state, {
        items: state.items.concat({
          id: uuid(),
          name: action.payload.name,
          done: false
        })
      });

    case TodosActionTypes.TOGGLE:
      return Object.assign({}, state, {
        items: state.items.map(
          (item: Todo) =>
            item.id === action.payload.id ? { ...item, done: !item.done } : item
        )
      });

    case TodosActionTypes.REMOVE_DONE:
      return Object.assign({}, state, {
        items: state.items.filter((item: Todo) => !item.done)
      });

    case TodosActionTypes.FILTER:
      return Object.assign({}, state, { filter: action.payload.filter });

    default:
      return state;
  }
}

export interface Todo {
  id: string;
  name: string;
  done: boolean;
}
{'fraction_non_alphanumeric': 0.1223091976516634, 'fraction_numerical': 0.0003913894324853229, 'mean_word_length': 3.203125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6425290', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1616, 'n_words': 425}