<commit_before>(function (scope) {

    var _arrayContains = function (array, val) {
        for (var i = 0; i < array.length; i++) {
            if (array[i] == val) {
                return true;
            }
        }
        return false;
    };

    var Discuss = function (path, headers, options) {
        this.methods = [ 'get', 'post', 'put', 'delete', 'head' ];
        this.path = path;
        this._initHeaders(headers);
        this._initOptions(options);
        this._initMethods();
    };

    Discuss.prototype._initHeaders = function (headers) {
        this.headers = headers;
    };

    Discuss.prototype._initOptions = function (options) {
        this.options = {
            autoParse: true,
            timeout: 30000,
            cors: false
        };
        for (var o in options) {
            if (options.hasOwnProperty(o) && this.options.hasOwnProperty(o)) {
                this.options[o] = options[o];
            }
        }
    };

    Discuss.prototype._initMethods = function () {
        var self = this;
        for (var i in this.methods) {
            (function(method) {
                Discuss.prototype['method'] = function () {
                    arguments.unshift(method);
                    Discuss.prototype.request(self, arguments);
                };
            }(this.methods[i]));
        }
    };

    /*
     *  Discuss.request(method, [[[[headers], ] query, ] body], callback);
     */
    Discuss.prototype.request = function () {
        if (typeof arguments[0] !== 'string' || !_arrayContains(this.methods, arguments[0])) {
            throw 'Invalid method type';
        }

        if (typeof arguments[1] === 'function') {
            arguments[4] = arguments[1];
            arguments[3] = null;
            arguments[2] = null;
            arguments[1] = null;
        }
        else if ((typeof arguments[1] === 'Object' || typeof arguments[1] === 'string')
            && typeof arguments[2] === 'function') {
            
            arguments[4] = arguments[2];
            arguments[3] = arguments[1];
            arguments[2] = null;
            arguments[1] = null;
        }
        else if ((typeof arguments[1] === 'Object' || typeof arguments[1] === 'string')
            && (typeof arguments[2] === 'Object' || typeof arguments[2] === 'string')
            && typeof arguments[3] === 'function') {
            
            arguments[4] = arguments[3];
            arguments[3] = arguments[2];
            arguments[2] = arguments[1];
            arguments[1] = null;
        }

        Discuss.prototype.send.apply(this, arguments);
    };

    Discuss.prototype.send = function (method, headers, query, body, callback) {
        var url = this.path;

        if (query && Object.keys(query).length > 0) {
            url = url + this._buildQueryString(query);
        }

        var xhr = this._buildXHR();
        xhr.open(method, this.path, true);

        var compiledHeaders = this._buildHeaders(headers);
        for (var p in compiledHeaders) {
            if (compiledHeaders.hasOwnProperty(p)) {
                xhr.setRequestHeader(p, compiledHeaders[p]);
            }
        }

        var timer = setTimer(function() {
            xhr.abort();
            callback(new Error('Request has timed out'));
        }, self.options.timeout);

        xhr.readystatechange = function () {
            clearTimeout(timeout);
            if (xhr.readyState === 4) {
                this._handleResponse(xhr.status, xhr.responseText, xhr.getAllResponseHeaders(), callback);
            }
        };
    };

    Discuss.prototype._handleResponse = function (status, responseText, responseHeaderText, callback) {
        var responseBody = responseText, responseHeaders;
        if (this.options.autoParse) {
            try {
                responseHeaders = this._parseResponseHeaders(responseHeaderText);
            }
            catch {
                responseHeaders = new Error('Unable to parse response headers');<commit_msg>Improving method signature logic<commit_after>(function (scope) {

    var Discuss = function (path, headers, options) {
        this.methods = {
            'get': {
                query: true
            },
            'post': {
                body: true
            },
            'put': {
                body: true
            },
            'delete': {
            },
            'head': {
                query: true
            } };

        this.path = path;
        this._initHeaders(headers);
        this._initOptions(options);
        this._initMethods();
    };

    Discuss.prototype._initHeaders = function (headers) {
        this.headers = headers;
    };

    Discuss.prototype._initOptions = function (options) {
        this.options = {
            autoParse: true,
            timeout: 30000,
            cors: false
        };
        for (var o in options) {
            if (options.hasOwnProperty(o) && this.options.hasOwnProperty(o)) {
                this.options[o] = options[o];
            }
        }
    };

    Discuss.prototype._initMethods = function () {
        var self = this;
        for (var m in this.methods) {
            (function(method) {
                Discuss.prototype[method] = function () {
                    arguments.unshift(method);
                    Discuss.prototype.request(self, arguments);
                };
            }(this.methods[m]));
        }
    };

    /*
     *  Discuss.request(method [, query | body], callback [, headers]);
     */
    Discuss.prototype.request = function () {
        if (typeof arguments[0] !== 'string' || !(arguments[0] in this.methods)) {
            throw 'Invalid method type';
        }

        var method = this.methods[arguments[0]];

        if ((method.body || method.query)
            && arguments.length === 2 {
            arguments.splice(1, 0, null, null);
        }
        else if (!method.body && !method.query
            && arguments.length === 2) {
            arguments.splice(1, 0, null, null);
        }

        Discuss.prototype.send.apply(this, arguments);
    };

    Discuss.prototype.send = function (method, query, body, callback, headers) {
        var url = this.path;

        if (query && Object.keys(query).length > 0) {
            url = url + this._buildQueryString(query);
        }

        var xhr = this._buildXHR();
        xhr.open(method, this.path, true);

        var compiledHeaders = this._buildHeaders(headers);
        for (var p in compiledHeaders) {
            if (compiledHeaders.hasOwnProperty(p)) {
                xhr.setRequestHeader(p, compiledHeaders[p]);
            }
        }

        var timer = setTimer(function() {
            xhr.abort();
            callback(new Error('Request has timed out'));
        }, self.options.timeout);

        xhr.readystatechange = function () {
            clearTimeout(timeout);
            if (xhr.readyState === 4) {
                this._handleResponse(xhr.status, xhr.responseText, xhr.getAllResponseHeaders(), callback);
            }
        };

        if (body) {
            xhr.send(body);
        }
    };

    Discuss.prototype._handleResponse = function (status, responseText, responseHeaderText, callback) {
        var responseBody = responseText, responseHeaders;
        if (this.options.autoParse) {
            try {
                responseHeaders = this._parseResponseHeaders(responseHeaderText);
            }
            catch {
                responseHeaders = new Error('Unable to parse response headers');
{'fraction_non_alphanumeric': 0.12849014159057828, 'fraction_numerical': 0.007013365091967712, 'mean_word_length': 1.6791917759659696, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3037048', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1978, 'n_words': 427}