<commit_before>import { isEmpty } from 'lodash'
export default class ReduxReducerBuilder {

	constructor() {
		this.initialState = {};
		this.actionTypeToReducer = {};
		this.nameToChild = {};
	}

	setInitialState(initialState) {
		this.initialState = initialState;
		return this;
	}

	addReducer(actionType, reducer) {
		this.actionTypeToReducer[actionType] = reducer;
		return this
	}

	combine(name, child) {
		this.nameToChild[name] = child;
		return this
	}

	build() {

		const self = this;


		if(isEmpty(self.nameToChild)) {

			return (state = self.initialState, action) => {
				const reducer = self.actionTypeToReducer[action.type];
				return reducer ? reducer(state, action) : state;
			}

		} else {

			return (state = self.initialState, action) => {

				const parentReducer = self.actionTypeToReducer[action.type];

				const applyChildren = (nextState, name) => {
					const reducer = self.nameToChild[name];
					nextState[name] = reducer(state[name], action);
					return nextState;
				};

				const children = Object.keys(self.nameToChild)
					.reduce(applyChildren, {});

				return {
					...(parentReducer ? parentReducer(state, action) : state),
					...children
				}

			}
		}

	}

}
<commit_msg>changed names, added hasChanged logic<commit_after>import { isEmpty } from 'lodash'
export default class ReduxReducerBuilder {

	constructor() {
		this.initialState = {};
		this.typeToReducer = {};
		this.keyToReducer = {};
	}

	setInitialState(initialState) {
		this.initialState = initialState;
		return this;
	}

  //should you be able to add multiple reducers for the same type?
	addReducer(type, reducer) {
		this.typeToReducer[type] = reducer;
		return this
	}

	combine(key, reducer) {
		this.keyToReducer[key] = reducer;
		return this
	}

	build() {

		const self = this;

		if(isEmpty(self.nameToChild)) {

			return (state = self.initialState, action) => {
				const reducer = self.typeToReducer[action.type];
				return reducer ? reducer(state, action) : state;
			}

		} else {

			return (state = self.initialState, action) => {

        const rootReducer = self.typeToReducer[action.type];

        const nextRootState = rootReducer ? rootReducer(state, action) : state;

        let hasChanged = state === nextRootState;

				const nextCombinedReducersState = Object.keys(self.keyToReducer)
					.reduce((nextState, key) => {

            const reducer = self.keyToReducer[key];
            const stateForKey = state[key];
            const nextStateForKey = reducer ? reducer(previousStateForKey, action) : stateForKey;

            nextState[key] = nextStateForKey;
            hasChanged = hasChanged || nextStateForKey !== stateForKey;

            return nextState;

          }, {});

        return hasChanged ? {...nextRootState,...nextCombinedReducersState} : state;

			}
		}

	}

}

{'fraction_non_alphanumeric': 0.12036710201200142, 'fraction_numerical': 0.0, 'mean_word_length': 3.1799410029498527, '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': '858926', 'n_tokens_mistral': 1057, 'n_tokens_neox': 891, 'n_words': 185}