<commit_before>var utils = require('./utils');

module.exports = function (options, data, next) {

    /*  TODO create docs
        Config example
        {
            "data_key": {
                "value": "replace_value",
                "value": {
                    "other_keys": "replace_value"
                }
            }
        }
    */

    if (!options.translate) {
        return next(new Error('Flow-Tools: Translate: No config in options.'));
    }

    // create a flat structure of the data chunk
    data = utils.flat(data);

    // replace paths from the options object
    var value;
    var translate = options.translate;
    for (var path in translate) { 

        if (typeof (value = data[path]) === 'undefined') {
            continue;
        }

        if (typeof translate[path][value] !== 'undefined') {
            if (typeof translate[path][value] === 'object') {
                for (var flat in translate[path][value]) {
                    data[flat] = translate[path][value][flat];
                }
            } else {
                data[path] = translate[path][value];
            }
        }
    }

    // unflat data chunk 
    data = utils.deep(data);

    next(null, data);
}
<commit_msg>update translate method to use libobject<commit_after>var libob = require('libobject');

module.exports = function (options, data, next) {

    /*  TODO create docs
        Config example
        {
            "key": {
                "value": "replace_value",
                "value": {
                    "other.flat.key": "replace_value"
                }
            }
        }
    */

    if (!libob.isObject(options._) || !libob.isObject(data)) {
        return next(new Error('Flow-Tools.translate: Config or data is not an object.')); 
    }

    // create a flat structure of the data chunk
    data = libob.flat(data);

    // replace paths from the options object
    var value;
    var translate = options._;
    for (var path in translate) { 

        if (typeof (value = data[path]) === 'undefined') {
            continue;
        }

        if (typeof translate[path][value] !== 'undefined') {
            if (typeof translate[path][value] === 'object') {
                for (var flat in translate[path][value]) {
                    data[flat] = translate[path][value][flat];
                }
            } else {
                data[path] = translate[path][value];
            }
        }
    }

    // unflat data chunk 
    next(null, libob.deep(data));
}

{'fraction_non_alphanumeric': 0.12996820349761526, 'fraction_numerical': 0.0, 'mean_word_length': 1.600206611570248, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, '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': '2683413', 'n_tokens_mistral': 703, 'n_tokens_neox': 669, 'n_words': 178}