<commit_before>var csv = require('ya-csv');

function documentsToCsv(writer, handlerName, documents) {
    var csvWriter = new csv.CsvWriter(writer);

    (documents || []).forEach(function (doc) {
        var entries = doc[handlerName];

        entries.forEach(function (entryObject) {
            var row = objectToRow(entryObject);

            row.unshift(doc._id);

            csvWriter.writeRecord(row);
        });
    });
}

function objectToRow(entryObject) {
    var row = [];

    Object.keys(entryObject).forEach(function (prop) {
        row.push(entryObject[prop]);
    });

    return row;
}

module.exports = documentsToCsv;<commit_msg>Split out an inner loop into a separate function.<commit_after>var csv = require('ya-csv');

function documentsToCsv(writer, handlerName, documents) {
    var csvWriter = new csv.CsvWriter(writer);

    (documents || []).forEach(function (doc) {
        var rows = documentToRows(handlerName, doc);

        rows.forEach(function (row) {
            csvWriter.writeRecord(row);
        });
    });
}

function documentToRows(handlerName, doc) {
    var rows = [];

    (doc[handlerName] || []).forEach(function (entryObject) {
        var row = objectToRow(entryObject);

        row.unshift(doc._id);

        rows.push(row);
    });

    return rows;
}

function objectToRow(entryObject) {
    var row = [];

    Object.keys(entryObject).forEach(function (prop) {
        row.push(entryObject[prop]);
    });

    return row;
}

module.exports = documentsToCsv;
{'fraction_non_alphanumeric': 0.13843111404087013, 'fraction_numerical': 0.0, 'mean_word_length': 2.9326424870466323, 'pattern_counts': {'":': 0, '<': 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': '1510557', 'n_tokens_mistral': 472, 'n_tokens_neox': 446, 'n_words': 89}