<commit_before>'use strict';

const createElement = require('@cloudcmd/create-element');
const noop = () => {};

load.addLoadListener = (onload) => {
    load.onload = onload;
};

load.addErrorListener = (onerror) => {
    load.onerror = onerror;
};

const addListenerOnce = (el, name, fn) => {
    const listener = (e) => {
        fn(e);
        el.removeEventListener(name, fn);
    };
    
    el.addEventListener(name, listener);
};

load.onerror = noop;
load.onload = noop;

module.exports = load;

function load(src, callback) {
    const ext = getExt(src);
    
    switch (ext) {
    case '.js':
        return load.js(src, callback);
    
    case '.css':
        return load.css(src, callback);
    
    case '.json':
        return load.json(src, callback);
    
    default:
        return load.ajax(src, callback);
    }
}

load.js = (src, callback) => {
    const id = getIdBySrc(src);
    let el = document.getElementById(id);
    
    if (el)
        return callback();
    
    el = createElement('script', {
        id,
        src,
    });
    
    addListenerOnce(el, 'load', (event) => {
        callback(null, event);
        load.onload();
    });
    
    addListenerOnce(el, 'error', (e) => {
        callback(e);
        document.body.removeChild(el);
        load.onerror(e, src);
    });
};

load.css = (src, callback) => {
    const id = getIdBySrc(src);
    let el = document.getElementById(id);
    
    if (el)
        return callback();
    
    el = createElement('link', {
        id: getIdBySrc(src),
        rel: 'stylesheet',
        href: src,
        parent: document.head,
    });
    
    el.addEventListener('load', (event) => {
        callback(null, event);
        load.onload();
    });
    
    el.addEventListener('error', (e) => {
        callback(e);
        document.head.removeChild(el);
        load.onerror(e, src);
    });
};

load.ajax   = (url, callback) => {
    const request = new XMLHttpRequest();
    
    request.open('GET', url, true);
    
    request.addEventListener('load', () => {
        if (request.status >= 200 && request.status < 400){
            const data = request.responseText;
            callback(null, data);
            load.onload();
        }
    });
    
    request.addEventListener('error', (e) => {
        load.onerror(e);
    });
    
    request.send();
};

load.json = (url ,callback) => {
    load.ajax(url, (error, data) => {
        let json;
        <commit_msg>fix(load) ajax: no callback call on error<commit_after>'use strict';

const createElement = require('@cloudcmd/create-element');
const noop = () => {};

load.addLoadListener = (onload) => {
    load.onload = onload;
};

load.addErrorListener = (onerror) => {
    load.onerror = onerror;
};

const addListenerOnce = (el, name, fn) => {
    const listener = (e) => {
        fn(e);
        el.removeEventListener(name, fn);
    };
    
    el.addEventListener(name, listener);
};

load.onerror = noop;
load.onload = noop;

module.exports = load;

function load(src, callback) {
    const ext = getExt(src);
    
    switch (ext) {
    case '.js':
        return load.js(src, callback);
    
    case '.css':
        return load.css(src, callback);
    
    case '.json':
        return load.json(src, callback);
    
    default:
        return load.ajax(src, callback);
    }
}

load.js = (src, callback) => {
    const id = getIdBySrc(src);
    let el = document.getElementById(id);
    
    if (el)
        return callback();
    
    el = createElement('script', {
        id,
        src,
    });
    
    addListenerOnce(el, 'load', (event) => {
        callback(null, event);
        load.onload();
    });
    
    addListenerOnce(el, 'error', (e) => {
        callback(e);
        document.body.removeChild(el);
        load.onerror(e, src);
    });
};

load.css = (src, callback) => {
    const id = getIdBySrc(src);
    let el = document.getElementById(id);
    
    if (el)
        return callback();
    
    el = createElement('link', {
        id: getIdBySrc(src),
        rel: 'stylesheet',
        href: src,
        parent: document.head,
    });
    
    el.addEventListener('load', (event) => {
        callback(null, event);
        load.onload();
    });
    
    el.addEventListener('error', (e) => {
        callback(e);
        document.head.removeChild(el);
        load.onerror(e, src);
    });
};

load.ajax = (url, callback) => {
    const request = new XMLHttpRequest();
    
    request.open('GET', url, true);
    
    request.addEventListener('load', () => {
        if (request.status >= 200 && request.status < 400){
            const data = request.responseText;
            callback(null, data);
            load.onload();
        }
    });
    
    request.addEventListener('error', (e) => {
        load.onerror(e);
        callback(e);
    });
    
    request.send();
};

load.json = (url ,callback) => {
    load.ajax(url, (error, data) => {
        let json;
        
{'fraction_non_alphanumeric': 0.16381565707385792, 'fraction_numerical': 0.002414972831555645, 'mean_word_length': 2.060344827586207, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '1587213', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1587, 'n_words': 325}