src/index.js
function getClosenessRanking(testString, stringToRank) {
  let charNumber = 0
  function findMatchingCharacter(matchChar, string, index) {
    for (let j = index; j < string.length; j++) {
      const stringChar = string[j]
      if (stringChar === matchChar) {
        return j + 1
      }
    }
    return -1
  }
  function getRanking(spread) {
    const matching = spread - stringToRank.length + 1
    const ranking = rankings.MATCHES + 1 / matching
    return ranking
  }
  const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0)
  if (firstIndex < 0) {
    return rankings.NO_MATCH
  }
  charNumber = firstIndex
  for (let i = 1; i < stringToRank.length; i++) {
    const matchChar = stringToRank[i]
    charNumber = findMatchingCharacter(matchChar, testString, charNumber)
    const found = charNumber > -1
    if (!found) {
      return rankings.NO_MATCH
    }
  }

  const spread = charNumber - firstIndex
  return getRanking(spread)
}
function getClosenessRanking(testString, stringToRank) {
  let charNumber = 0
  function findMatchingCharacter(matchChar, string, index) {
    for (let j = index; j < string.length; j++) {
      const stringChar = string[j]
      if (stringChar === matchChar) {
        return j + 1
      }
    }
    return -1
  }
  function getRanking(spread) {
    const matching = spread - stringToRank.length + 1
    const ranking = rankings.MATCHES + 1 / matching
    return ranking
  }
  const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0)
  if (firstIndex < 0) {
    return rankings.NO_MATCH
  }
  charNumber = firstIndex
  for (let i = 1; i < stringToRank.length; i++) {
    const matchChar = stringToRank[i]
    charNumber = findMatchingCharacter(matchChar, testString, charNumber)
    const found = charNumber > -1
    if (!found) {
      return rankings.NO_MATCH
    }
  }

  const spread = charNumber - firstIndex
  return getRanking(spread)
}
function getClosenessRanking ( testString , stringToRank ) { let charNumber = 0 function findMatchingCharacter ( matchChar , string , index ) { for ( let j = index ; j < string . length ; j ++ ) { const stringChar = string [ j ] if ( stringChar === matchChar ) { return j + 1 } } return - 1 } function getRanking ( spread ) { const matching = spread - stringToRank . length + 1 const ranking = rankings . MATCHES + 1 / matching return ranking } const firstIndex = findMatchingCharacter ( stringToRank [ 0 ] , testString , 0 ) if ( firstIndex < 0 ) { return rankings . NO_MATCH } charNumber = firstIndex for ( let i = 1 ; i < stringToRank . length ; i ++ ) { const matchChar = stringToRank [ i ] charNumber = findMatchingCharacter ( matchChar , testString , charNumber ) const found = charNumber > - 1 if ( ! found ) { return rankings . NO_MATCH } } const spread = charNumber - firstIndex return getRanking ( spread ) }
Returns a score based on how spread apart the
characters from the stringToRank are within the testString.
A number close to rankings.MATCHES represents a loose match. A number close
to rankings.MATCHES + 1 represents a loose match.
@param {String} testString - the string to test against
@param {String} stringToRank - the string to rank
@returns {Number} the number between rankings.MATCHES and
rankings.MATCHES + 1 for how well stringToRank matches testString
Returns a score based on how spread apart the characters from the stringToRank are within the testString . A number close to rankings . MATCHES represents a loose match . A number close to rankings . MATCHES + 1 represents a loose match .