<commit_before>import { getCookie as getBrowserCookie } from './cookieApi';

/**
 * return the node referenced by path in state.
 * @param {Object} path
 * @return {Object} node reference
 **/
function pathSlicer(path) {
    const getSubtree = (subtree, key) => {
        if (subtree && key.indexOf('.') > -1) {
            const remaining = key.split('.').slice(1).join('.');

            return getSubtree(subtree[key.split('.')[0]], remaining);
        }
        return subtree[key];
    };

    return (state) => getSubtree(state, path);
}

/**
 * read browser cookie into state
 * @param {Object} preloaded state
 * @param {Object} paths
 * @param {Object} get Cookie implementation
 * @return {Object} new state
 **/
const getStateFromCookies = (
    preloadedState,
    paths,
    getCookie = getBrowserCookie
) => {
    Object.keys(paths).forEach(pathToState => {
        const pathConf = paths[pathToState];

        // read cookies
        const storedState = getCookie(pathConf.name);

        // get a slice of state path where to put cookie value
        const stateTree = pathSlicer(pathToState)(preloadedState);

        if (storedState) {
            try {
                Object.assign(stateTree, JSON.parse(storedState));
            } catch (err) {
                console.error(`Error while parsing cookie ${pathConf.name}.`);
            }
        }
    });

    return preloadedState;
};

export default getStateFromCookies;<commit_msg>handle case where terminal key in store is not an object<commit_after>import { getCookie as getBrowserCookie } from './cookieApi';

/**
 * return the node referenced by path in state.
 * @param {Object} path
 * @return {Object} node reference
 **/
function pathSlicer(path) {
    const getSubtree = (subtree, key) => {
        if (key.indexOf('.') > -1) {
            const remaining = key.split('.').slice(1).join('.');

            return getSubtree(subtree[key.split('.')[0]], remaining);
        }
        return subtree[key];
    };

    return (state) => getSubtree(state, path);
}

/**
 * read browser cookie into state
 * @param {Object} preloaded state
 * @param {Object} paths
 * @param {Object} get Cookie implementation
 * @return {Object} new state
 **/
const getStateFromCookies = (
    preloadedState,
    paths,
    getCookie = getBrowserCookie
) => {
    Object.keys(paths).forEach(pathToState => {
        const pathConf = paths[pathToState];
        const pathSplit = pathToState.split('.');
        const terminalKey = pathSplit.slice(-1);

        // read cookies
        const storedState = getCookie(pathConf.name);

        // get a slice of state path where to put cookie value
        const stateTree = pathSplit.length > 1 ? (
            pathSlicer(pathSplit.slice(0, -1).join('.'))(preloadedState)
        ) : preloadedState;

        if (storedState) {
            try {
                stateTree[terminalKey] = JSON.parse(storedState);
            } catch (err) {
                console.error(`Unable to set state from cookie at ${pathConf.name}. Error: `, err);
            }
        }
    });

    return preloadedState;
};

export default getStateFromCookies;
{'fraction_non_alphanumeric': 0.13007614213197968, 'fraction_numerical': 0.0031725888324873096, 'mean_word_length': 2.4994450610432852, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '3192273', 'n_tokens_mistral': 918, 'n_tokens_neox': 899, 'n_words': 243}