<commit_before>/**
 * medloader - code4sa.org
 */

var log = function(obj) {
    if (window.console) {
        window.console.log(obj);
    }
};
var map = {
    // 0 was name
    0 : "sep",
    1 : "regno",
    2 : "schedule",
    3 : "dosage_form",
    4 : "pack_size",
    5 : "num_packs",
    6 : "is_generic"
};

var process_ingredient = function(result) {
    //log(result);
    $("#search-container").removeClass("js-results");
    $(".products .product").remove();

    var resultLength = result.length;
    if (resultLength) {
        $("#resultsheader span").html(resultLength);
        $("#search-container").addClass("js-results");

        var $templateRow = $(".products .template");

        for (var i=0; i<resultLength; i++){
            var $product = $templateRow.clone().removeClass('template').addClass('product');        
            var productItemData = result[i];

            // Set the product name
            $(".product-name", $product).html(productItemData.name);

            // Add the product details to each product item
            $(".details dd", $product).each(function(idx){
                var key = map[idx];
                $(this).html(productItemData[key]);
            });

            // Add ingredients to each product item
            $product.find(".ingredients dt, .ingredients dd").remove();
            var $ingredientsList = $(".ingredients dl", $product);
            var productIngredients = productItemData.ingredients;
            var productIngredientsLength = productIngredients.length;
            for (var j=0; j<productIngredientsLength; j++) {
                $ingredientsList.append("<dt>" + productIngredients[j].name + ":</dt>");
                $ingredientsList.append("<dd>" + productIngredients[j].strength + productIngredients[j].unit + "</dd>");
            }

            $('.products').append($product);
        }
        
    }
    $("#search-container").removeClass("js-loading");
}
var timer;
var searchTerm = '';
var entermedicine = function(e) {
    searchTerm = e.target.value;
    var load = function(){
        $("#search-container").addClass("js-loading");
        $.getJSON("/api/search?q=" + searchTerm, process_ingredient);
    }
    clearTimeout(timer);
    timer = setTimeout(load, 500);
};<commit_msg>Refactored js code a little<commit_after>/**
 * medloader - code4sa.org
 */

var log = function(obj) {
    if (window.console) {
        window.console.log(obj);
    }
};
var map = {
    // 0 was name
    0 : "sep",
    1 : "regno",
    2 : "schedule",
    3 : "dosage_form",
    4 : "pack_size",
    5 : "num_packs",
    6 : "is_generic"
};

var loading_data = function() {
    $("#search-container").addClass("js-loading");
}

var loaded_data = function() {
    $("#search-container").removeClass("js-results");
}

var process_request = function(result) {
    //log(result);
    loaded_data();
    $(".products .product").remove();

    var resultLength = result.length;
    if (resultLength) {
        $("#resultsheader span").html(resultLength);
        $("#search-container").addClass("js-results");

        var $templateRow = $(".products .template");

        for (var i = 0; i < resultLength; i++) {
            var $product = $templateRow.clone().removeClass('template').addClass('product');        
            var productItemData = result[i];

            // Set the product name
            $(".product-name", $product).html(productItemData.name);

            // Add the product details to each product item
            $(".details dd", $product).each(function(idx){
                var key = map[idx];
                $(this).html(productItemData[key]);
            });

            // Add ingredients to each product item
            $product.find(".ingredients dt, .ingredients dd").remove();
            var $ingredientsList = $(".ingredients dl", $product);
            var productIngredients = productItemData.ingredients;
            var productIngredientsLength = productIngredients.length;
            for (var j=0; j<productIngredientsLength; j++) {
                $ingredientsList.append("<dt>" + productIngredients[j].name + ":</dt>");
                $ingredientsList.append("<dd>" + productIngredients[j].strength + productIngredients[j].unit + "</dd>");
            }

            $('.products').append($product);
        }
    }
    $("#search-container").removeClass("js-loading");
}
var timer;
var searchTerm = '';
var delay = 500;
var api_url = "/api/search";

var entermedicine = function(e) {
    searchTerm = e.target.value;

    var load_data = function() {
        var query = api_url + "?q=" + searchTerm;
        loading_data();
        $.getJSON(query, process_request);
    }

    var reset_delay_before_requesting = function() {
        clearTimeout(timer);
        timer = setTimeout(load_data, delay);
    }

    reset_delay_before_requesting();
};
{'fraction_non_alphanumeric': 0.16199057956174484, 'fraction_numerical': 0.0057341798074953924, 'mean_word_length': 2.4034843205574914, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 11, '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': '908315', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1400, 'n_words': 296}