<commit_before>// TODO: Replace jquery with vanilla js.
'use strict';

var suggestions = suggestions || {};

/********************************************************************
* PROPERTIES
*********************************************************************/
suggestions.API = '//suggestqueries.google.com/complete/search';
suggestions.engine = 'google search';
suggestions.language = 'en';
suggestions.format = 'jsonp';
suggestions.delay = 300;
suggestions.selector = null;
suggestions.engines = {
  'google': 'google search',
  'youtube': 'yt'
};

suggestions.formats = {
  'json': 'firefox',
  'jsonp': 'youtube',
  'xml': 'toolbar'
};


/********************************************************************
* METHODS
*********************************************************************/
suggestions.delay = function(fn, ms, immediate) {
  var timeout;
  return function() {
    var _this = this;
    var args = arguments;

    var later = function() {
      timeout = null;
      if (!immediate) {
        fn.apply(_this, args);
      }
    };

    var callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, ms);
    if (callNow) {
      fn.apply(_this, args);
    }
  };
};


suggestions.setEngine = function(value) {
  suggestions.engine = suggestions.engines[value]
                    || suggestions.engines.google;
};


suggestions.update = function(data) {
  console.log(data);
};


suggestions.suggest = function(query) {
  $.ajax({
    url: suggestions.API,
    dataType: suggestions.format,
    data: {
      q: query,
      client: suggestions.formats[suggestions.format],
      jsonp: 'suggestions.update',
      ds: suggestions.engine,
      hl: suggestions.language
    }
  });
};


suggestions.bind = function(selector) {
  suggestions.selector = $(selector);
  suggestions.selector.keyup(function(e) {
    var query = e.target.value.trim();
    if (query) {
      suggestions.suggest(query);
    }
  });
};<commit_msg>#49 - Debounce delay for search suggestions<commit_after>// TODO: Replace jquery with vanilla js.
'use strict';

var suggestions = suggestions || {};

/********************************************************************
* PROPERTIES
*********************************************************************/
suggestions.API = '//suggestqueries.google.com/complete/search';
suggestions.engine = 'google search';
suggestions.language = 'en';
suggestions.format = 'jsonp';
suggestions.delay = 300;
suggestions.selector = null;
suggestions.query = '';
suggestions.engines = {
  'google': 'google search',
  'youtube': 'yt'
};

suggestions.formats = {
  'json': 'firefox',
  'jsonp': 'youtube',
  'xml': 'toolbar'
};


/********************************************************************
* METHODS
*********************************************************************/
suggestions.debounce = function(fn, ms) {
  var timeout;
  return function() {
    var _this = this;
    var _args = arguments;

    clearTimeout(timeout);
    timeout = setTimeout(function() {
      fn.apply(_this, _args);
    }, ms);
  };
};

suggestions.setEngine = function(value) {
  suggestions.engine = suggestions.engines[value]
                    || suggestions.engines.google;
};

suggestions.updateCallback = function(data) {
  console.log(data);
};

suggestions.suggest = function(query) {
  suggestions.query = query;

  $.ajax({
    url: suggestions.API,
    dataType: suggestions.format,
    data: {
      q: query,
      client: suggestions.formats[suggestions.format],
      jsonp: 'suggestions.updateCallback',
      ds: suggestions.engine,
      hl: suggestions.language
    }
  });
};

suggestions.bind = function(selector) {
  // Create debounced function to save requests.
  var suggestDebounced = suggestions.debounce(
    suggestions.suggest, suggestions.delay);

  // Store binded values in module.
  suggestions.selector = $(selector);
  suggestions.query = suggestions.selector.val();

  // Bind event listener on selector.
  suggestions.selector.keyup(function(e) {
    var query = e.target.value.trim();
    if (query && query !== suggestions.query) {
      suggestDebounced(query);
    }
  });
};
{'fraction_non_alphanumeric': 0.2583973128598848, 'fraction_numerical': 0.0019193857965451055, 'mean_word_length': 3.9278959810874703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3186156', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1137, 'n_words': 241}