<commit_before>const ExamplesHandler = function () {
    const config = {
        username: process.env.ZULIP_USERNAME,
        apiKey: process.env.ZULIP_API_KEY,
        realm: process.env.ZULIP_REALM,
    };
    const examples = [];

    const make_result_object = (example, result, count = false) => {
        const name = count !== false ? `${example.name}_${count}` : example.name;
        return {
            name,
            endpoint: example.endpoint.split(':')[0],
            method: example.endpoint.split(':')[1],
            status_code: example.status_code.toString(),
            result,
        };
    };

    const generate_validation_data = async (client) => {
        const response_data = [];
        for (const example of examples) {
            const result = await example.func(client);
            if (Array.isArray(result)) {
                // Handle special cases where some examples make
                // more than 1 API requests.
                result.forEach((r, index) => {
                    response_data.push(make_result_object(example, r, index));
                });
            } else {
                response_data.push(make_result_object(example, result));
            }
        }
        return response_data;
    };

    const format_example_code = (example) => {
        let body = example.func.toString();
        body = body.slice(body.indexOf("{") + 2, body.lastIndexOf("}") - 1);
        body = body.replace(/\n\s*\n\s*\n/g, '\n\n');
        const wrapper = [
            "Zulip(config).then(async (client) => {",
            `${body}`,
            "}).then(console.log).catch(console.err);",
        ].join('\n');
        return wrapper;
    };

    const main = async () => {
        const command = process.argv[2];
        if (command === 'generate-responses') {
            const Zulip = require('zulip-js');
            const client = await Zulip(config);
            const response_data = await generate_validation_data(client);
            console.log(JSON.stringify(response_data));
            return;
        }
        if (command === 'generate-example') {
            const endpoint = process.argv[3];
            if (!endpoint) {
                console.error("js-examples: Please specify an endpoint.");
                process.exit(1);
            }
            const example = examples.find(x => x.endpoint.toString() === endpoint);
            if (!example) {
                console.error(`js-examples: Endpoint ${endpoint} not found.`);
                process.exit(1);
            }
            console.log(format_example_code(example));
            return;
        }
        console.error("js-examples: Invalid command.");
        process.exit(1);
    };

    const add_example = (name, endpoint, status_code, func) => {
        examples.push({
            name,
            endpoint,
            status_code,
            func,
        });
    };

    return {
        main,
        add_example,
    };
};

const {main} = ExamplesHandler();

// Declare all the examples below.

main();<commit_msg>js-api: Refactor ExamplesHandler to avoid running examples in a loop.<commit_after>const ExamplesHandler = function () {
    const config = {
        username: process.env.ZULIP_USERNAME,
        apiKey: process.env.ZULIP_API_KEY,
        realm: process.env.ZULIP_REALM,
    };
    const examples = {};
    const response_data = [];

    const make_result_object = (example, result, count = false) => {
        const name = count !== false ? `${example.name}_${count}` : example.name;
        return {
            name,
            endpoint: example.endpoint.split(':')[0],
            method: example.endpoint.split(':')[1],
            status_code: example.status_code.toString(),
            result,
        };
    };

    const generate_validation_data = async (client, example) => {
        const result = await example.func(client);
        if (Array.isArray(result)) {
            // Handle special cases where some examples make
            // more than 1 API requests.
            result.forEach((r, index) => {
                response_data.push(make_result_object(example, r, index));
            });
        } else {
            response_data.push(make_result_object(example, result));
        }
    };

    const main = async () => {
        const Zulip = require('zulip-js');
        const client = await Zulip(config);

        await generate_validation_data(client, examples.send_message);
        await generate_validation_data(client, examples.create_user);

        console.log(JSON.stringify(response_data));
        return;
    };

    const add_example = (name, endpoint, status_code, func) => {
        const example = {
            name,
            endpoint,
            status_code,
            func,
        };
        examples[name] = example;
    };

    return {
        main,
        add_example,
    };
};

const {main} = ExamplesHandler();

// Declare all the examples below.

main();
{'fraction_non_alphanumeric': 0.12991178829190056, 'fraction_numerical': 0.0026062550120288693, 'mean_word_length': 1.8443557582668186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '6597787', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1352, 'n_words': 293}