|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { Injectable } from "@angular/core"; |
| 4 | + |
| 5 | +// ----------------------------------------------------------------------------------- // |
| 6 | +// ----------------------------------------------------------------------------------- // |
| 7 | + |
| 8 | +export type FuzzyScore = number; |
| 9 | + |
| 10 | +export interface FuzzySegment { |
| 11 | + value: string; |
| 12 | + isMatch: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +@Injectable({ |
| 16 | + providedIn: "root" |
| 17 | +}) |
| 18 | +export class FuzzyMatcher { |
| 19 | + |
| 20 | + // I parse the value against the given input, dividing it up into a collection of |
| 21 | + // segments that either match or do not match sequences within the input. |
| 22 | + public parseValue( value: string, input: string ) : FuzzySegment[] { |
| 23 | + |
| 24 | + var valueLength = value.length; |
| 25 | + var inputLength = input.length; |
| 26 | + var valueIndex = 0; |
| 27 | + var inputIndex = 0; |
| 28 | + |
| 29 | + var segments: FuzzySegment[] = []; |
| 30 | + var segment: FuzzySegment; |
| 31 | + |
| 32 | + while ( valueIndex < valueLength ) { |
| 33 | + |
| 34 | + var valueChar = value.charAt( valueIndex++ ).toLowerCase(); |
| 35 | + var inputChar = input.charAt( inputIndex ).toLowerCase(); |
| 36 | + |
| 37 | + // If this character matches the input, add to a matching segment. |
| 38 | + if ( valueChar === inputChar ) { |
| 39 | + |
| 40 | + inputIndex++; |
| 41 | + |
| 42 | + if ( segment && segment.isMatch ) { |
| 43 | + |
| 44 | + segment.value += valueChar; |
| 45 | + |
| 46 | + } else { |
| 47 | + |
| 48 | + segment = { |
| 49 | + value: valueChar, |
| 50 | + isMatch: true |
| 51 | + }; |
| 52 | + segments.push( segment ); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + // If we've run out of input characters to match, we can short-circuit |
| 57 | + // the segmentation - we know that the rest of the value will contain |
| 58 | + // non-matching characters - we can add them to a final segment. |
| 59 | + if ( ( inputIndex === inputLength ) && ( valueIndex < valueLength ) ) { |
| 60 | + |
| 61 | + segments.push({ |
| 62 | + value: value.slice( valueIndex ), |
| 63 | + isMatch: false |
| 64 | + }); |
| 65 | + |
| 66 | + // Force the while-loop to end. |
| 67 | + break; |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + // If this character does NOT match the input, add to a non-matching segment. |
| 72 | + } else { |
| 73 | + |
| 74 | + if ( segment && ! segment.isMatch ) { |
| 75 | + |
| 76 | + segment.value += valueChar; |
| 77 | + |
| 78 | + } else { |
| 79 | + |
| 80 | + segment = { |
| 81 | + value: valueChar, |
| 82 | + isMatch: false |
| 83 | + }; |
| 84 | + segments.push( segment ); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + return( segments ); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + // I compare the input to the given value and return a score for the fuzzy match. |
| 98 | + public scoreValue( value: string, input: string ) : FuzzyScore { |
| 99 | + |
| 100 | + // For the scoring process, we don't need to maintain the case of the arguments. |
| 101 | + // As such, we can normalize them now so that we don't have to do it inside of |
| 102 | + // each loop iteration. |
| 103 | + var normalizedValue = value.toLowerCase(); |
| 104 | + var normalizedInput = input.toLowerCase(); |
| 105 | + |
| 106 | + var valueLength = normalizedValue.length; |
| 107 | + var inputLength = normalizedInput.length; |
| 108 | + var valueIndex = 0; |
| 109 | + var inputIndex = 0; |
| 110 | + |
| 111 | + // When several letters are matched in a row, we're going to give them extra |
| 112 | + // weight in the scoring since this more likely to provide a meaningful match. |
| 113 | + var previousIndexMatched = false; |
| 114 | + var score = 0; |
| 115 | + |
| 116 | + while ( valueIndex < valueLength ) { |
| 117 | + |
| 118 | + var valueChar = normalizedValue.charAt( valueIndex++ ); // Get and increment. |
| 119 | + var inputChar = normalizedInput.charAt( inputIndex ); |
| 120 | + |
| 121 | + // If the current character matches the next part of the sequential input, |
| 122 | + // we are going to increase the score of the match. |
| 123 | + if ( valueChar === inputChar ) { |
| 124 | + |
| 125 | + inputIndex++; |
| 126 | + |
| 127 | + // If the previous character was also a match, let's bump the score by |
| 128 | + // slightly more. |
| 129 | + score += ( previousIndexMatched ) |
| 130 | + ? 3 |
| 131 | + : 2 |
| 132 | + ; |
| 133 | + |
| 134 | + previousIndexMatched = true; |
| 135 | + |
| 136 | + // If we've run out of input characters to match, then we can short- |
| 137 | + // circuit the scoring based on the number of remaining characters in the |
| 138 | + // value (each remaining character will be detracted from the score). |
| 139 | + if ( inputIndex === inputLength ) { |
| 140 | + |
| 141 | + return( score -= ( valueLength - valueIndex ) ); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + // If the current character does NOT Match the next part of the sequential |
| 146 | + // input, we are going to decrease the score of the match. |
| 147 | + } else { |
| 148 | + |
| 149 | + score -= 1; |
| 150 | + previousIndexMatched = false; |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + return( score ); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments