<commit_before>const coverageVariable = '__start_istanbul__';

export function instrument(istanbul = require('istanbul'), options) {
    return function(input) {
        return function coverageInstrument(log) {
            return new Promise((resolve, reject) => {
                const instrumenter = new istanbul.Instrumenter({
                    embedSource: true,
                    noAutoWrap: true,
                    ...options,
                    coverageVariable
                });

                istanbul.hook.hookRequire(
                    // hook requires which matches input files
                    function(file) {
                        return input.indexOf(file) !== -1;
                    },
                    // and instrument that sources
                    function(source, file) {
                        const result = instrumenter.instrumentSync(source, file);

                        log(file);

                        return result;
                    }
                );

                resolve(input);
            });
        };
    };
}

export function report(reporters = [ 'lcovonly', 'text-summary' ], dir = 'coverage/') {
    return function(input) {
        return function coverageReport(/* log */) {
            const istanbul = require('istanbul');

            istanbul.hook.unhookRequire();

            return new Promise((resolve, reject) => {
                if (!global[coverageVariable]) {
                    return reject('no coverage information was collected');
                }

                const collector = new istanbul.Collector();
                const reporter = new istanbul.Reporter(null, dir);

                collector.add(global[coverageVariable]);
                reporter.addAll(reporters);
                reporter.write(collector, false /* async */, () => {
                    resolve(input);
                });
            });
        };
    };
}

export function thresholds(options = {}) {
    return function(input) {
        return function coverageoptions(/* log */) {
            const istanbul = require('istanbul');

            return new Promise((resolve, reject) => {
                const coverage = istanbul.utils.summarizeCoverage(global[coverageVariable]);
                const result = Object.keys(options).reduce((errors, key) => {
                    const threshold = options[key];
                    const summary = coverage[key];

                    // check percentage threshold
                    if (threshold > 0) {
                        if (summary.pct < threshold) {
                            return errors.concat(
                                `${key} percentage: ${summary.pct}% < ${threshold}%`
                            );
                        }
                    }

                    // check gap threshold
                    if (threshold < 0) {
                        if (summary.covered - summary.total < threshold) {
                            return errors.concat(
                                `${key} gap: ${summary.covered} - ${summary.total} < ${threshold}`
                            );
                        }
                    }
<commit_msg>:wrench: rename package to `start-istanbul`<commit_after>const coverageVariable = '__start_istanbul__';

export function instrument(istanbul = require('istanbul'), options) {
    return function(input) {
        return function istanbulInstrument(log) {
            return new Promise((resolve, reject) => {
                const instrumenter = new istanbul.Instrumenter({
                    embedSource: true,
                    noAutoWrap: true,
                    ...options,
                    coverageVariable
                });

                istanbul.hook.hookRequire(
                    // hook requires which matches input files
                    function(file) {
                        return input.indexOf(file) !== -1;
                    },
                    // and instrument that sources
                    function(source, file) {
                        const result = instrumenter.instrumentSync(source, file);

                        log(file);

                        return result;
                    }
                );

                resolve(input);
            });
        };
    };
}

export function report(reporters = [ 'lcovonly', 'text-summary' ], dir = 'coverage/') {
    return function(input) {
        return function istanbulReport(/* log */) {
            const istanbul = require('istanbul');

            istanbul.hook.unhookRequire();

            return new Promise((resolve, reject) => {
                if (!global[coverageVariable]) {
                    return reject('no coverage information was collected');
                }

                const collector = new istanbul.Collector();
                const reporter = new istanbul.Reporter(null, dir);

                collector.add(global[coverageVariable]);
                reporter.addAll(reporters);
                reporter.write(collector, false /* async */, () => {
                    resolve(input);
                });
            });
        };
    };
}

export function thresholds(options = {}) {
    return function(input) {
        return function istanbulThresholds(/* log */) {
            const istanbul = require('istanbul');

            return new Promise((resolve, reject) => {
                const coverage = istanbul.utils.summarizeCoverage(global[coverageVariable]);
                const result = Object.keys(options).reduce((errors, key) => {
                    const threshold = options[key];
                    const summary = coverage[key];

                    // check percentage threshold
                    if (threshold > 0) {
                        if (summary.pct < threshold) {
                            return errors.concat(
                                `${key} percentage: ${summary.pct}% < ${threshold}%`
                            );
                        }
                    }

                    // check gap threshold
                    if (threshold < 0) {
                        if (summary.covered - summary.total < threshold) {
                            return errors.concat(
                                `${key} gap: ${summary.covered} - ${summary.total} < ${threshold}`
                            );
                        }
                    }

{'fraction_non_alphanumeric': 0.10162916989914662, 'fraction_numerical': 0.0009309542280837859, 'mean_word_length': 1.1984993178717598, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '48384', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1364, 'n_words': 318}