<commit_before>/*
 * @author: vivaxy
 * @date:   2015-04-02 20:46:03
 * @last modified by:   vivaxy
 * @last modified time: 2015-04-03 09:38:19
 */

'use strict';

/**
 * @constructor
 * @param {[type]} options [description]
 */
var Ajax = function (options) {
        this.events = {};

        this.url = options.url;
        this.type = options.type;

    },
    p = {};

Ajax.prototype = p;

/**
 * on `success`
 * @param  {[type]}   event    [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
p.on = function (event, callback) {
    if (!this.events[event]) {
        this.events[event] = [];
    }
    this.events[event].push(callback);
    return this;
};

/**
 * fire events
 * @param  {[type]} event [description]
 * @return {[type]}       [description]
 */
p.fire = function (event) {
    var callbacks = this.events[event],
        _this = this,
        _arguments = arguments;
    if (callbacks) {
        callbacks.forEach(function (callback) {
            callback.apply(null, Array.prototype.slice.call(_arguments, 1));
        });
    }
    return this;
};

/**
 * send data
 * @param  {[type]} data [description]
 * @return {[type]}      [description]
 */
p.send = function (data) {
    var _this = this,
        xhr = new XMLHttpRequest();
    xhr.addEventListener('readystatechange', function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
                // 2** is valid response status, and 304 means get from the cache
                _this.fire('success', xhr.responseText, xhr);
            }
        }
        _this.fire('readystatechange', xhr);
    });
    if (this.type && this.type.toUpperCase() === 'POST') {
        xhr.open('POST', this.url, true);
        xhr.send(data);
    } else {
        var a = document.createElement('a');
        a.href = this.url;
        var url = a.origin + a.pathname + this.getQueryString(data);
        xhr.open('GET', url, true);
        xhr.send();
    }
    return this;
};

/**
 * get query string from data
 * @param  {[type]} data [description]
 * @return {[type]}        [description]
 */
p.getQueryString = function (data) {
    if (typeof data === 'string') return data === '' ? '' : '?' + data;
    var querystring = '?';
    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            var value = data[key];
            if (typeof value !== 'string') {
                try {
                    value = JSON.stringify(value);
                } catch (e) {
                    throw 'data can not be parsed to querystring';
                }
            }
            querystring += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
        }
    }
    return querystring.slice(0, -1);
};<commit_msg>get add timestamp<commit_after>/*
 * @author: vivaxy
 * @date:   2015-04-02 20:46:03
 * @last modified by:   vivaxy
 * @last modified time: 2015-04-03 09:38:19
 */

'use strict';

/**
 *
 * @param options
 * @constructor
 */
var Ajax = function (options) {
        this.events = {};

        this.url = options.url;
        this.type = options.type;

    },
    p = {};

Ajax.prototype = p;

/**
 *
 * @param event
 * @param callback
 * @returns {p}
 */
p.on = function (event, callback) {
    if (!this.events[event]) {
        this.events[event] = [];
    }
    this.events[event].push(callback);
    return this;
};

/**
 *
 * @param event
 * @returns {p}
 */
p.fire = function (event) {
    var callbacks = this.events[event],
        _this = this,
        _arguments = arguments;
    if (callbacks) {
        callbacks.forEach(function (callback) {
            callback.apply(null, Array.prototype.slice.call(_arguments, 1));
        });
    }
    return this;
};

/**
 *
 * @param data
 * @returns {p}
 */
p.send = function (data) {
    var _this = this,
        xhr = new XMLHttpRequest();
    xhr.addEventListener('readystatechange', function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
                // 2** is valid response status, and 304 means get from the cache
                _this.fire('success', xhr.responseText, xhr);
            }
        }
        _this.fire('readystatechange', xhr);
    });
    if (this.type && this.type.toUpperCase() === 'POST') {
        xhr.open('POST', this.url, true);
        xhr.send(data);
    } else {
        var a = document.createElement('a');
        a.href = this.url;
        var url = a.origin + a.pathname + this.getQueryString(this.mixin(data, {_: new Date().getTime()}));
        xhr.open('GET', url, true);
        xhr.send();
    }
    return this;
};

/**
 *
 * @param data
 * @returns {*}
 */
p.getQueryString = function (data) {
    if (typeof data === 'string') return data === '' ? '' : '?' + data;
    var queryString = '?';
    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            var value = data[key];
            if (typeof value !== 'string') {
                try {
                    value = JSON.stringify(value);
                } catch (e) {
                    throw 'data can not be parsed to querystring';
                }
            }
            queryString += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
        }
    }
    return queryString.slice(0, -1);
};

p.mixin = function (a, b) {
    var r = {};
    r = this.copy(a, r);
    r = this.copy(b, r);
    return r;
};

/**
 *
 * @param a
 * @param r
 * @returns {*}
 */
p.copy = function (a, r) {
    for (var i in a) {
        if (a.hasOwnProperty(i)) {
            r[i] = a[i];
        }
    }
    return r;
};
{'fraction_non_alphanumeric': 0.16777583187390543, 'fraction_numerical': 0.01576182136602452, 'mean_word_length': 1.949896694214876, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6831602', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1824, 'n_words': 438}