<commit_before>var co = require('co'),
    isGeneratorFn = require('is-generator').fn;

var DEFAULT_METHODS = [
    'afterAll',
    'afterEach',
    'beforeAll',
    'beforeEach',
    'it', 'fit', //'xit',
];
var originalMethods = {},
    overrideMethods, installed;

module.exports = function jasmineCo(userFn) {
    return wrapFn(userFn);
};
module.exports.install = function install() {
    (overrideMethods || DEFAULT_METHODS).forEach(function(fname) {
        coifyJasmineFn(fname);
    });
    installed = true;
};
module.exports.uninstall = function uninstall() {
    Object.keys(originalMethods).forEach(function(key) {
        global[key] = originalMethods[key];
    });
    originalMethods = {};
    installed = false;
};
module.exports.isInstalled = function isInstalled() {
    return installed;
};
module.exports.setOverrideMethods = function setOverrideMethods(methods) {
    overrideMethods = Array.isArray(methods) ? methods : DEFAULT_METHODS;
};

function coifyJasmineFn(fname) {
    // don't process methods that don't exist globally or have already been overridden
    if (!global[fname] || originalMethods[fname]) { return; }

    var origFn = originalMethods[fname] = global[fname];
    global[fname] = wrapFn(origFn);
}

function wrapFn(origFn) {
    return function() {
        var expectsName = arguments.length > 1; // `it('does stuff', fn)` (length 2) vs `beforeEach(fn)` (length 1)
        var userFn = expectsName ? arguments[1] : arguments[0];
        if (isGeneratorFn(userFn)) {
            // if the user method is a generator:
            //   1. call it with the correct `this` context object
            //   2. wrap it in a co function which fails the spec if an exception is
            //      encountered and notifies jasmine that the spec is done when the co
            //      promise settles
            var args = [function(done) {
                return co(userFn.bind(this)).then(done, done.fail);
            }];
            if (expectsName) { args.unshift(arguments[0]); }
            return origFn.apply(null, args);
        } else {
            // if the user method is a standard function, just call the standard jasmine method
            return origFn.apply(null, arguments);
        }
    };
}<commit_msg>Add support for standard (non-generator) functions that return promises<commit_after>var co = require('co'),
    isGeneratorFn = require('is-generator').fn;

var DEFAULT_METHODS = [
    'afterAll',
    'afterEach',
    'beforeAll',
    'beforeEach',
    'it', 'fit', //'xit',
];
var originalMethods = {},
    overrideMethods, installed;

module.exports = function jasmineCo(userFn) {
    return wrapFn(userFn);
};
module.exports.install = function install() {
    (overrideMethods || DEFAULT_METHODS).forEach(function(fname) {
        coifyJasmineFn(fname);
    });
    installed = true;
};
module.exports.uninstall = function uninstall() {
    Object.keys(originalMethods).forEach(function(key) {
        global[key] = originalMethods[key];
    });
    originalMethods = {};
    installed = false;
};
module.exports.isInstalled = function isInstalled() {
    return installed;
};
module.exports.setOverrideMethods = function setOverrideMethods(methods) {
    overrideMethods = Array.isArray(methods) ? methods : DEFAULT_METHODS;
};

function coifyJasmineFn(fname) {
    // don't process methods that don't exist globally or have already been overridden
    if (!global[fname] || originalMethods[fname]) { return; }

    var origFn = originalMethods[fname] = global[fname];
    global[fname] = wrapFn(origFn);
}

function wrapFn(origFn) {
    return function() {
        var expectsName = arguments.length > 1; // `it('does stuff', fn)` (length 2) vs `beforeEach(fn)` (length 1)
        var userFn = expectsName ? arguments[1] : arguments[0];
        var args;
        if (isGeneratorFn(userFn)) {
            // if the user method is a generator:
            //   1. call it with the correct `this` context object
            //   2. wrap it in a co function which fails the spec if an exception is
            //      encountered and notifies jasmine that the spec is done when the co
            //      promise settles
            args = [function(done) {
                return co(userFn.bind(this)).then(done, done.fail);
            }];
            if (expectsName) { args.unshift(arguments[0]); }
            return origFn.apply(null, args);
        } else if (!userFn.length) {
            // if the user method is a standard function that doesn't expect to be asynchronous
            // (i.e. it doesn't take `done` as a parameter), wrap it with a function that *is*
            // asynchronous and retrofit it to support returning a promise from the function
            args = [function(done) {
                let result = userFn.call(this);
                if (!(result && typeof result.then === 'function')) {
                    done();
                    return result;
                } else {
                    result.then(done, done.fail);
                }
            }];
            if (expectsName) { args.unshift(arguments[0]); }
            return origFn.apply(null, args);
        } else {
            // if the user method is already asynchronous, just call the standard jasmine method
            // and let the user method take care of itself
            return origFn.apply(null, arguments);
        }
    };
}
{'fraction_non_alphanumeric': 0.11769059955588453, 'fraction_numerical': 0.003145817912657291, 'mean_word_length': 2.473650385604113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5031640', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1481, 'n_words': 433}