1- // from here https://stackoverflow.com/a/36566052/6942210
2-
3- export const similarity = ( s1 , s2 ) => {
1+ /**
2+ * Compares two strings and returns the Levenshtein distance
3+ * @see https://stackoverflow.com/a/36566052/6942210
4+ * @param s1 Text string
5+ * @param s2 text string
6+ */
7+ export const similarity = ( s1 : string , s2 : string ) => {
48 let longer = s1 ;
59 let shorter = s2 ;
610 if ( s1 . length < s2 . length ) {
@@ -11,32 +15,33 @@ export const similarity = (s1, s2) => {
1115 if ( longerLength === 0 ) {
1216 return 1.0 ;
1317 }
14- return ( longerLength - editDistance ( longer , shorter ) ) / parseFloat ( longerLength ) ;
18+ return ( longerLength - editDistance ( longer , shorter ) ) / longerLength ;
1519} ;
1620
17- const editDistance = ( s1 , s2 ) => {
21+ const editDistance = ( s1 : string , s2 : string ) => {
1822 s1 = s1 . toLowerCase ( ) ;
1923 s2 = s2 . toLowerCase ( ) ;
2024
21- const costs = new Array ( ) ;
25+ const costs = new Array < number > ( ) ;
2226 for ( let i = 0 ; i <= s1 . length ; i ++ ) {
2327 let lastValue = i ;
2428 for ( let j = 0 ; j <= s2 . length ; j ++ ) {
25- if ( i === 0 )
29+ if ( i === 0 ) {
2630 costs [ j ] = j ;
27- else {
31+ } else {
2832 if ( j > 0 ) {
2933 let newValue = costs [ j - 1 ] ;
30- if ( s1 . charAt ( i - 1 ) !== s2 . charAt ( j - 1 ) )
31- newValue = Math . min ( Math . min ( newValue , lastValue ) ,
32- costs [ j ] ) + 1 ;
34+ if ( s1 . charAt ( i - 1 ) !== s2 . charAt ( j - 1 ) ) {
35+ newValue = Math . min ( Math . min ( newValue , lastValue ) , costs [ j ] ) + 1 ;
36+ }
3337 costs [ j - 1 ] = lastValue ;
3438 lastValue = newValue ;
3539 }
3640 }
3741 }
38- if ( i > 0 )
42+ if ( i > 0 ) {
3943 costs [ s2 . length ] = lastValue ;
44+ }
4045 }
4146 return costs [ s2 . length ] ;
4247} ;
0 commit comments