lunr.TokenSet.fromFuzzyString = function()

in static/api/python/3.2.x/lunr.js [1437:1561]


    lunr.TokenSet.fromFuzzyString = function (str, editDistance) {
      var root = new lunr.TokenSet
    
      var stack = [{
        node: root,
        editsRemaining: editDistance,
        str: str
      }]
    
      while (stack.length) {
        var frame = stack.pop()
    
        // no edit
        if (frame.str.length > 0) {
          var char = frame.str.charAt(0),
              noEditNode
    
          if (char in frame.node.edges) {
            noEditNode = frame.node.edges[char]
          } else {
            noEditNode = new lunr.TokenSet
            frame.node.edges[char] = noEditNode
          }
    
          if (frame.str.length == 1) {
            noEditNode.final = true
          }
    
          stack.push({
            node: noEditNode,
            editsRemaining: frame.editsRemaining,
            str: frame.str.slice(1)
          })
        }
    
        if (frame.editsRemaining == 0) {
          continue
        }
    
        // insertion
        if ("*" in frame.node.edges) {
          var insertionNode = frame.node.edges["*"]
        } else {
          var insertionNode = new lunr.TokenSet
          frame.node.edges["*"] = insertionNode
        }
    
        if (frame.str.length == 0) {
          insertionNode.final = true
        }
    
        stack.push({
          node: insertionNode,
          editsRemaining: frame.editsRemaining - 1,
          str: frame.str
        })
    
        // deletion
        // can only do a deletion if we have enough edits remaining
        // and if there are characters left to delete in the string
        if (frame.str.length > 1) {
          stack.push({
            node: frame.node,
            editsRemaining: frame.editsRemaining - 1,
            str: frame.str.slice(1)
          })
        }
    
        // deletion
        // just removing the last character from the str
        if (frame.str.length == 1) {
          frame.node.final = true
        }
    
        // substitution
        // can only do a substitution if we have enough edits remaining
        // and if there are characters left to substitute
        if (frame.str.length >= 1) {
          if ("*" in frame.node.edges) {
            var substitutionNode = frame.node.edges["*"]
          } else {
            var substitutionNode = new lunr.TokenSet
            frame.node.edges["*"] = substitutionNode
          }
    
          if (frame.str.length == 1) {
            substitutionNode.final = true
          }
    
          stack.push({
            node: substitutionNode,
            editsRemaining: frame.editsRemaining - 1,
            str: frame.str.slice(1)
          })
        }
    
        // transposition
        // can only do a transposition if there are edits remaining
        // and there are enough characters to transpose
        if (frame.str.length > 1) {
          var charA = frame.str.charAt(0),
              charB = frame.str.charAt(1),
              transposeNode
    
          if (charB in frame.node.edges) {
            transposeNode = frame.node.edges[charB]
          } else {
            transposeNode = new lunr.TokenSet
            frame.node.edges[charB] = transposeNode
          }
    
          if (frame.str.length == 1) {
            transposeNode.final = true
          }
    
          stack.push({
            node: transposeNode,
            editsRemaining: frame.editsRemaining - 1,
            str: charA + frame.str.slice(2)
          })
        }
      }
    
      return root
    }