{"size": 1153, "ext": "js", "max_stars_repo_path": "lib/createUpdater.js", "max_stars_repo_name": "doniyor2109/redux-lightweight", "max_stars_repo_stars_event_min_datetime": "2018-11-14T18:12:56.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-15T06:05:19.000Z", "max_issues_repo_path": "lib/createUpdater.js", "max_issues_repo_name": "doniyor2109/redux-lightweight", "max_issues_repo_issues_event_min_datetime": "2018-12-20T13:10:27.000Z", "max_issues_repo_issues_event_max_datetime": "2020-03-30T16:25:03.000Z", "max_forks_repo_path": "lib/createUpdater.js", "max_forks_repo_name": "doniyor2109/redux-lightweight", "max_forks_repo_forks_event_min_datetime": "2021-04-06T20:00:08.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-06T20:00:08.000Z"}
{"max_stars_count": 41, "max_issues_count": 18, "max_forks_count": 1, "avg_line_length": 27.4523809524, "max_line_length": 87, "alphanum_fraction": 0.6782307025}
import { getAllProperties } from './helpers';

export function createUpdater(Updater) {
  const updater = new Updater();
  const updaterName = Updater.name;
  const initialState = updater.state;

  function reducer(state = initialState, { type, action, payload }) {
    const parts = type && type.split('/');
    if (parts && parts.length === 2 && parts[0]===updaterName && parts[1] in updater) {
      const actionName = parts[1];
      updater.state = state;
      return updater[actionName].apply(updater, payload);
    }
    return state;
  }

  const actions = getAllProperties(updater).reduce((actions, actionName) => {
    const actionType = buildActionType(updaterName, actionName)
    const action = createUpdaterAction(actionType)
    return Object.assign(actions, { [actionName]: action })
  }, {})

  return [reducer, actions, initialState]
}

export function buildActionType(reducerName, actionName) {
  return `${reducerName}/${actionName}`
}

export function createUpdaterAction(actionType) {
  function action(...args) {
    return {
      type: actionType,
      payload: args
    };
  }
  action.type = actionType;

  return action;
}
