github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/cmd/deck/static/fuzzy-search.js (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  (function(window) {
    18    var FuzzySearch = function(dict) {
    19      dict.sort();
    20      /** {Array<string>} **/
    21      this.dict = dict;
    22      /**
    23       * Returns true if the string contains all the pattern characters.
    24       * @param {string} pttn
    25       * @param {string} str
    26       * @return {boolean}
    27       */
    28      this.basicMatch = function(pttn, str) {
    29        var i = 0, j = 0;
    30        while (i < pttn.length && j < str.length) {
    31          if (pttn[i].toLowerCase() === str[j].toLowerCase()) i += 1;
    32          j += 1;
    33        }
    34        return i === pttn.length;
    35      };
    36  
    37      /**
    38       * Sorts dict function. The higher the score, the lower index the string is. If two
    39       * strings have the same score, sort by alphabetical order.
    40       * @param {string} a
    41       * @param {string} b
    42       * @return {number}
    43       */
    44      this.sortFn = function(a, b) {
    45        if (a.score === b.score) {
    46          return a.str < b.str ? -1 : (a.str > b.str ? 1 : 0);
    47        }
    48        return a.score > b.score ? -1 : 1;
    49      };
    50  
    51      /**
    52       * Calculates the score that a matching can get. The string is calculated based on 4
    53       * criteria:
    54       *  1. +3 score for the matching that occurs near the beginning of the string.
    55       *  2. +5 score for the matching that is not an alphabetical character.
    56       *  3. +3 score for the matching that the string character is upper case.
    57       *  4. +10 score for the matching that matches the uppercase which is just before a
    58       *  separator.
    59       * @param {number} i
    60       * @param {string} str
    61       * @return {number}
    62       */
    63      this.calcScore = function(i, str) {
    64        var score = 0;
    65        var isAlphabetical = function (c) {
    66          return (c > 64 && c < 91) || (c > 96 && c < 123);
    67        };
    68        // Bonus if the matching is near the start of the string
    69        if (i < 3) {
    70          score += 3;
    71        }
    72        // Bonus if the matching is not a alphabetical character
    73        if (!isAlphabetical(str.charCodeAt(i))) {
    74          score += 5;
    75        }
    76        // Bonus if the matching is an UpperCase character
    77        if (str[i].toUpperCase() === str[i]) {
    78          score += 3;
    79        }
    80  
    81        // Bonus if matching after a separator
    82        var separatorBehind = (i === 0 || !isAlphabetical(str.charCodeAt(i - 1)));
    83        if (separatorBehind && isAlphabetical(str.charCodeAt(i))) {
    84          score += 10;
    85          score += (str[i].toUpperCase() === str[i] ? 5 : 0);
    86        }
    87        return score;
    88      };
    89  
    90      /**
    91       * Get maximum score that a string can get against the pattern.
    92       * @param {string} pttn
    93       * @param {string} str
    94       * @return {number}
    95       */
    96      this.getMaxScore = function(pttn, str) {
    97        // Rewards perfect match a value of Number.MAX_SAFE_INTEGER
    98        if (pttn === str) {
    99          return Number.MAX_SAFE_INTEGER;
   100        }
   101        var i = 0;
   102        while (i < Math.min(pttn.length, str.length) && pttn[i] === str[i]) {
   103          i++;
   104        }
   105        var streak = i;
   106        var score = [];
   107        for (i = 0; i < 2; i++) {
   108          score[i] = [];
   109          for (var j = 0 ; j < str.length; j++) {
   110            score[i][j] = 0;
   111          }
   112        }
   113        for (i = 0; i < pttn.length; i++) {
   114          var t = i % 2;
   115          for (j = 0; j < str.length; j++) {
   116            var scoreVal = pttn[i].toLowerCase() === str[j].toLowerCase() ?
   117              this.calcScore(j, str) : Number.MIN_SAFE_INTEGER;
   118            if (streak > 4 && i === streak - 1 && j === streak - 1) {
   119              scoreVal += 10 * streak;
   120            }
   121            if (i === 0) {
   122              score[t][j] = scoreVal;
   123              if (j > 0) score[t][j] = Math.max(score[t][j], score[t][j-1]);
   124            } else {
   125              if (j > 0) {
   126                score[t][j] = Math.max(score[t][j], score[t][j-1]);
   127                score[t][j] = Math.max(score[t][j], score[Math.abs(t-1)][j-1] + scoreVal);
   128              }
   129            }
   130          }
   131        }
   132        return score[(pttn.length - 1) % 2][str.length - 1];
   133      };
   134    };
   135  
   136    /**
   137     * Returns a list of string from dictionary that matches against the pattern.
   138     * @param {string} pattern
   139     * @return {Array<string>}
   140     */
   141    FuzzySearch.prototype.search = function(pattern) {
   142      if (!this.dict || !this.dict.length === 0) {
   143            return [];
   144        }
   145      if (!pattern || pattern.length === 0) {
   146        return this.dict;
   147      }
   148      var dictScr = [];
   149      for (var i = 0; i < this.dict.length; i++) {
   150        if (this.basicMatch(pattern, this.dict[i])) {
   151          dictScr.push({
   152            str: this.dict[i],
   153            score: this.getMaxScore(pattern, this.dict[i])
   154          });
   155        }
   156      }
   157      dictScr.sort(this.sortFn);
   158  
   159      var result = [];
   160      for (i = 0; i < dictScr.length; i++) {
   161        if (dictScr[i].score === 0) continue;
   162        result.push(dictScr[i].str);
   163      }
   164      return result;
   165    };
   166  
   167    /**
   168     * Sets the dictionary for the fuzzy search.
   169     * @param {Array<string>} dict
   170     */
   171    FuzzySearch.prototype.setDict = function(dict) {
   172      dict.sort();
   173      this.dict = dict;
   174    };
   175  
   176    window.FuzzySearch = FuzzySearch;
   177  })(window);