Nodejs String Score score(keyword)

Here you can find the source of score(keyword)

Method Source Code

String.prototype.score = function(keyword) {

    if (string == keyword) { return 1.0; }

    if (keyword == "") { return 0; }

    var total_character_score = 0,
        keyword_length = keyword.length,
        string = this,// w ww . j  a va  2s  .co m
        string_length = string.length,
        compare_score = 0,
        final_score;

    for (var i = 0; i < string_length; ++i) {

        var c = string[i];
        var index_c_lowercase = keyword.indexOf(c.toLowerCase());
        var index_c_uppercase = keyword.indexOf(c.toUpperCase());
        var min_index = Math.min(index_c_lowercase, index_c_uppercase);
        var index_in_string = (min_index > -1) ? min_index : Math.max(index_c_lowercase, index_c_uppercase);

        if (keyword[index_in_string] === c) {
            compare_score++;
        }
    }

    final_score = compare_score / string_length;

    return final_score;
};

Related

  1. score()
    String.prototype.score = function () {
        return this.replace(/ /g, '_');
    };
    
  2. score(search)
    String.prototype.score = function(search) {
      if (search.length == 0 || this.length == 0) { return 0.0; }
      for (var i = search.length; i > 0; i--) {
        var
          subSearch = search.substring(0, i),
          index = this.search(new RegExp("\\b" + subSearch)),
          score = subSearch.length;
        if (index >= 0) {
          score += 1;
    ...