Lint and format src/compiler/utils/fuzzymatch.js

pull/8569/head
Simon Holthausen 3 years ago
parent 546e43598b
commit 5b130d1b85

@ -21,15 +21,13 @@ function _distance(str1, str2) {
if (str1 === null && str2 === null) { if (str1 === null && str2 === null) {
throw 'Trying to compare two null values'; throw 'Trying to compare two null values';
} }
if (str1 === null || str2 === null) if (str1 === null || str2 === null) return 0;
return 0;
str1 = String(str1); str1 = String(str1);
str2 = String(str2); str2 = String(str2);
const distance = levenshtein(str1, str2); const distance = levenshtein(str1, str2);
if (str1.length > str2.length) { if (str1.length > str2.length) {
return 1 - distance / str1.length; return 1 - distance / str1.length;
} } else {
else {
return 1 - distance / str2.length; return 1 - distance / str2.length;
} }
} }
@ -40,7 +38,6 @@ function _distance(str1, str2) {
* @param {string} str2 * @param {string} str2
*/ */
function levenshtein(str1, str2) { function levenshtein(str1, str2) {
/** /**
* @type {number[]} * @type {number[]}
*/ */
@ -52,12 +49,10 @@ function levenshtein(str1, str2) {
if (i && j) { if (i && j) {
if (str1.charAt(j - 1) === str2.charAt(i - 1)) { if (str1.charAt(j - 1) === str2.charAt(i - 1)) {
value = prev; value = prev;
} } else {
else {
value = Math.min(current[j], current[j - 1], prev) + 1; value = Math.min(current[j], current[j - 1], prev) + 1;
} }
} } else {
else {
value = i + j; value = i + j;
} }
prev = current[j]; prev = current[j];
@ -99,8 +94,7 @@ function gram_counter(value, gram_size = 2) {
for (i; i < grams.length; ++i) { for (i; i < grams.length; ++i) {
if (grams[i] in result) { if (grams[i] in result) {
result[grams[i]] += 1; result[grams[i]] += 1;
} } else {
else {
result[grams[i]] = 1; result[grams[i]] = 1;
} }
} }
@ -116,7 +110,6 @@ function sort_descending(a, b) {
} }
/** */ /** */
class FuzzySet { class FuzzySet {
/** /**
* @default {} * @default {}
*/ */
@ -174,8 +167,7 @@ class FuzzySet {
sum_of_square_gram_counts += Math.pow(gram_count, 2); sum_of_square_gram_counts += Math.pow(gram_count, 2);
if (gram in this.match_dict) { if (gram in this.match_dict) {
this.match_dict[gram].push([index, gram_count]); this.match_dict[gram].push([index, gram_count]);
} } else {
else {
this.match_dict[gram] = [[index, gram_count]]; this.match_dict[gram] = [[index, gram_count]];
} }
} }
@ -229,8 +221,7 @@ class FuzzySet {
other_gram_count = this.match_dict[gram][i][1]; other_gram_count = this.match_dict[gram][i][1];
if (index in matches) { if (index in matches) {
matches[index] += gram_count * other_gram_count; matches[index] += gram_count * other_gram_count;
} } else {
else {
matches[index] = gram_count * other_gram_count; matches[index] = gram_count * other_gram_count;
} }
} }
@ -262,7 +253,3 @@ class FuzzySet {
return new_results; return new_results;
} }
} }

Loading…
Cancel
Save