param partialMatch export default function scoreMatch ( str1 , str2 , partialMatch = false ) { str1 = str1 . toLowerCase ( ) ; str2 = str2 . toLowerCase ( ) ; if ( str1 === str2 ) { return 1 ; } if ( ! str1 || ! str2 || str1 . charCodeAt ( 0 ) !== str2 . charCodeAt ( 0 ) ) { return 0 ; } const str1Len = str1 . length ; const str2Len = str2 . length ; if ( ! partialMatch && str1Len > str2Len ) { return 0 ; } const minLength = Math . min ( str1Len , str2Len ) ; const maxLength = Math . max ( str1Len , str2Len ) ; let i = 1 ; let j = 1 ; let score = maxLength ; let ch1 = 0 ; let ch2 = 0 ; let found = false ; let acronym = false ; while ( i < str1Len ) { ch1 = str1 . charCodeAt ( i ) ; found = false ; acronym = false ; while ( j < str2Len ) { ch2 = str2 . charCodeAt ( j ) ; if ( ch1 === ch2 ) { found = true ; score += maxLength - ( acronym ? i : j ) ; break ; } acronym = ch2 === 45 ; j ++ ; } if ( ! found ) { if ( ! partialMatch ) { return 0 ; } break ; } i ++ ; } const matchRatio = i / maxLength ; const delta = maxLength - minLength ; const maxScore = sum ( maxLength ) - sum ( delta ) ; return ( score * matchRatio ) / maxScore ; } /**
 * Calculates sum of first `n` numbers, e.g. 1+2+3+...n
 */ function sum ( n ) { return n * ( n + 1 ) / 2 ; }