dist/js/space-table.js
// returns index of the beginning of the match or -1 if no match
returns index of the beginning of the match or -1 if no match
returns index of the beginning of the match or - 1 if no match
findMatch( a, b ) {
			const longest  = a.length > b.length ? a : b;
			const shortest = longest === a ? b : a;
			let shortestIndex = 0;
			let errorsSoFar   = 0;
			const errorsAllowed = 2;
			let lastError     = null;
			for ( let i = 0; i < longest.length; i++ ) { 
				const lettersMatch = ( longest[i] === shortest[shortestIndex] );
				if ( lettersMatch ) {
					shortestIndex++; 
					if ( shortestIndex === shortest.length ) {
						return i - ( shortest.length - 1 );
					}
				}
				else {
					if ( shortestIndex === 0 ) {
						if ( longest.length - i < shortest.length ) {
							return -1; 
						}
						continue; 
					}
					errorsSoFar++; 
					const twoLettersFlippedAround = ( shortest[shortestIndex] === longest[i - 1] && shortest[shortestIndex - 1] === longest[i] ); 
					if ( errorsSoFar > errorsAllowed || ( lastError === ( i - 1 ) && !twoLettersFlippedAround ) ) { 
						shortestIndex = 0;
						errorsSoFar   = 0;
						if ( longest.length - i < shortest.length ) {
							return -1; 
						}
						continue; 
					}
					shortestIndex++; 
					lastError = i;
					if ( shortestIndex === shortest.length ) {
						return i - ( shortest.length - 1 );
					}
				}
			}
			return -1;
		}
findMatch ( a , b ) { const longest = a . length > b . length ? a : b ; const shortest = longest === a ? b : a ; let shortestIndex = 0 ; let errorsSoFar = 0 ; const errorsAllowed = 2 ; let lastError = null ; for ( let i = 0 ; i < longest . length ; i ++ ) { const lettersMatch = ( longest [ i ] === shortest [ shortestIndex ] ) ; if ( lettersMatch ) { shortestIndex ++ ; if ( shortestIndex === shortest . length ) { return i - ( shortest . length - 1 ) ; } } else { if ( shortestIndex === 0 ) { if ( longest . length - i < shortest . length ) { return - 1 ; } continue ; } errorsSoFar ++ ; const twoLettersFlippedAround = ( shortest [ shortestIndex ] === longest [ i - 1 ] && shortest [ shortestIndex - 1 ] === longest [ i ] ) ; if ( errorsSoFar > errorsAllowed || ( lastError === ( i - 1 ) && ! twoLettersFlippedAround ) ) { shortestIndex = 0 ; errorsSoFar = 0 ; if ( longest . length - i < shortest . length ) { return - 1 ; } continue ; } shortestIndex ++ ; lastError = i ; if ( shortestIndex === shortest . length ) { return i - ( shortest . length - 1 ) ; } } } return - 1 ; }
returns index of the beginning of the match or -1 if no match
returns index of the beginning of the match or - 1 if no match
// eslint-disable-line // if these letters match, check the next set of letters // eslint-disable-line // unless of course, that was the last letter, in which // case, we found a match! // if they don't match // it's okay if we're still looking for // the start of the string // unless of course, the remainder of the long string is // shorter than the short string // in that case, we know this isn't a match // eslint-disable-line // if we're mid-string, record the error // eslint-disable-line // check if this and the last letter were flipped around // like this: something -> somtehing // eslint-disable-line // if that's too many errors or it's a consecutive error that // wasn't caused by two letters being switched around // eslint-disable-line // restart looking for the beginning of the short string // within the long string from this point // unless of course, the remainder of the long string is // shorter than the short string // in that case, we know this isn't a match // eslint-disable-line // if that wasn't too many errors, just move on // to the next letter, but record when our last // error was // eslint-disable-line // unless that was the last letter in which case, // that's a motherfucking match, cudi dude // if we made it here somehow, it's not a match