FuzzySet Class — svelte Architecture
Architecture documentation for the FuzzySet class in fuzzymatch.js from the svelte codebase.
Entity Profile
Dependency Diagram
graph TD 79034f27_7e89_3edc_1524_ef7e1cf114bf["FuzzySet"] 4057eb45_ab28_d989_1209_dfae45d590c0["fuzzymatch.js"] 79034f27_7e89_3edc_1524_ef7e1cf114bf -->|defined in| 4057eb45_ab28_d989_1209_dfae45d590c0 d480b819_2e14_22e2_dcf0_dc68f6824af1["constructor()"] 79034f27_7e89_3edc_1524_ef7e1cf114bf -->|method| d480b819_2e14_22e2_dcf0_dc68f6824af1 e9e928cd_ecf3_b386_9b42_407f2fa12962["add()"] 79034f27_7e89_3edc_1524_ef7e1cf114bf -->|method| e9e928cd_ecf3_b386_9b42_407f2fa12962 ca5600fe_7b5d_97e9_53dc_da8efbfc4d4f["_add()"] 79034f27_7e89_3edc_1524_ef7e1cf114bf -->|method| ca5600fe_7b5d_97e9_53dc_da8efbfc4d4f 82c454e1_d40e_f288_e9f9_3476ef8e8419["get()"] 79034f27_7e89_3edc_1524_ef7e1cf114bf -->|method| 82c454e1_d40e_f288_e9f9_3476ef8e8419 cf7dc1ed_ad34_4633_c64c_6d82e53d4449["__get()"] 79034f27_7e89_3edc_1524_ef7e1cf114bf -->|method| cf7dc1ed_ad34_4633_c64c_6d82e53d4449
Relationship Graph
Source Code
packages/svelte/src/compiler/phases/1-parse/utils/fuzzymatch.js lines 124–279
class FuzzySet {
/** @type {Record<string, string>} */
exact_set = {};
/** @type {Record<string, [number, number][]>} */
match_dict = {};
/** @type {Record<string, number[]>} */
items = {};
/** @param {string[]} arr */
constructor(arr) {
// initialisation
for (let i = GRAM_SIZE_LOWER; i < GRAM_SIZE_UPPER + 1; ++i) {
this.items[i] = [];
}
// add all the items to the set
for (let i = 0; i < arr.length; ++i) {
this.add(arr[i]);
}
}
/** @param {string} value */
add(value) {
const normalized_value = value.toLowerCase();
if (normalized_value in this.exact_set) {
return false;
}
let i = GRAM_SIZE_LOWER;
for (i; i < GRAM_SIZE_UPPER + 1; ++i) {
this._add(value, i);
}
}
/**
* @param {string} value
* @param {number} gram_size
*/
_add(value, gram_size) {
const normalized_value = value.toLowerCase();
const items = this.items[gram_size] || [];
const index = items.length;
items.push(0);
const gram_counts = gram_counter(normalized_value, gram_size);
let sum_of_square_gram_counts = 0;
let gram;
let gram_count;
for (gram in gram_counts) {
gram_count = gram_counts[gram];
sum_of_square_gram_counts += Math.pow(gram_count, 2);
if (gram in this.match_dict) {
this.match_dict[gram].push([index, gram_count]);
} else {
this.match_dict[gram] = [[index, gram_count]];
}
}
const vector_normal = Math.sqrt(sum_of_square_gram_counts);
// @ts-ignore no idea what this code is doing
items[index] = [vector_normal, normalized_value];
this.items[gram_size] = items;
this.exact_set[normalized_value] = value;
}
/** @param {string} value */
get(value) {
const normalized_value = value.toLowerCase();
const result = this.exact_set[normalized_value];
if (result) {
return /** @type {MatchTuple[]} */ ([[1, result]]);
}
// start with high gram size and if there are no results, go to lower gram sizes
for (let gram_size = GRAM_SIZE_UPPER; gram_size >= GRAM_SIZE_LOWER; --gram_size) {
const results = this.__get(value, gram_size);
if (results.length > 0) return results;
}
Domain
Source
Frequently Asked Questions
What is the FuzzySet class?
FuzzySet is a class in the svelte codebase, defined in packages/svelte/src/compiler/phases/1-parse/utils/fuzzymatch.js.
Where is FuzzySet defined?
FuzzySet is defined in packages/svelte/src/compiler/phases/1-parse/utils/fuzzymatch.js at line 124.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free