# All files / src fuzzy-search.js

100% Statements 37/37
78.57% Branches 11/14
100% Functions 6/6
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    7117x
    17x
    17x
    17x
    17x
    17x

    17x
    90x

    90x

    90x

    5x

    5x
    50x

    50x
    50x
    60x
    60x
    60x
    11x

    60x
    49x

    50x

    60x
    60x

    60x
    11x

    49x

    90x

    2x
    2x

    90x
    3x

     var classes = require('./utils/classes'),
      events = require('./utils/events'),
      extend = require('./utils/extend'),
      toString = require('./utils/to-string'),
      getByClass = require('./utils/get-by-class'),
      fuzzy = require('./utils/fuzzy')

    module.exports = function (list, options) {
      options = options || {}

      options = extend(
        {
          location: 0,
          distance: 100,
          threshold: 0.4,
          multiSearch: true,
          searchClass: 'fuzzy-search',
        },
        options
      )

      var fuzzySearch = {
        search: function (searchString, columns) {
          // Substract arguments from the searchString or put searchString as only argument
          var searchArguments = options.multiSearch ? searchString.replace(/ +$/, '').split(/ +/) : [searchString]

          for (var k = 0, kl = list.items.length; k < kl; k++) {
            fuzzySearch.item(list.items[k], columns, searchArguments)
          }
        },
        item: function (item, columns, searchArguments) {
          var found = true
          for (var i = 0; i < searchArguments.length; i++) {
            var foundArgument = false
            for (var j = 0, jl = columns.length; j < jl; j++) {
              if (fuzzySearch.values(item.values(), columns[j], searchArguments[i])) {
                foundArgument = true
              }
            }
            if (!foundArgument) {
              found = false
            }
          }
          item.found = found
        },
        values: function (values, value, searchArgument) {
          Eif (values.hasOwnProperty(value)) {
            var text = toString(values[value]).toLowerCase()

            if (fuzzy(text, searchArgument, options)) {
              return true
            }
          }
          return false
        },
      }

      events.bind(
        getByClass(list.listContainer, options.searchClass),
        'keyup',
        list.utils.events.debounce(function (e) {
          var target = e.target || e.srcElement // IE have srcElement
          list.search(target.value, fuzzySearch.search)
        }, list.searchDelay)
      )

      return function (str, columns) {
        list.search(str, columns, fuzzySearch.search)
      }
    }