AbstractThe type of the raw result, defaulting to MetricRaw.
Constructor for the Metric class. Initializes the metric with two inputs (strings or arrays of strings) and options.
The type of the raw result, defaulting to MetricRaw.
The name of the metric (e.g. 'levenshtein')
First input string or array of strings
Second input string or array of strings
Optionalopt: MetricOptions = {}Options for the metric computation
Optionalsymmetric: boolean = falseWhether the metric is symmetric (same result for inputs in any order)
Private ReadonlyaInputs for the metric computation, transformed into arrays
Private ReadonlybPrivate ReadonlymetricMetric name for identification
Protected ReadonlyoptionsOptions for the metric computation, such as performance tracking
Protected ReadonlyoptPrivateorigStore original inputs for result mapping
PrivateorigPrivateresultsResult of the metric computation, which can be a single result or an array of results. This will be populated after running the metric.
Protected ReadonlysymmetricIndicates whether the metric is symmetric (same result for inputs in any order)
Private StaticcacheCache for metric computations to avoid redundant calculations
Clear the cached results of the metric.
This method resets the results property to undefined, effectively clearing
any previously computed results. It can be useful for re-running the metric
with new inputs or options.
ProtectedcomputeAbstract method to be implemented by subclasses to perform the metric computation. This method should contain the logic for computing the metric between two strings.
First string
Second string
Length of the first string
Length of the second string
Maximum length of the strings
Get the name of the metric.
Get the result of the metric computation.
Check if the inputs are in batch mode.
This method checks if either a or b contains more than one string,
indicating that the metric is being run in batch mode.
Check if the inputs are in pairwise mode.
This method checks if both a and b are arrays of the same length,
indicating that the metric is being run on corresponding pairs of strings.
Optionalsafe: boolean = falseIf true, does not throw an error if lengths are not equal
Check if the inputs are in single mode.
This method checks if both a and b are single strings (not arrays),
indicating that the metric is being run on a single pair of strings.
Check if the metric is symmetrical.
This method returns whether the metric is symmetric, meaning it produces the same result regardless of the order of inputs (e.g., Levenshtein distance).
ProtectedprePre-compute the metric for two strings. This method is called before the actual computation to handle trivial cases.
First string
Second string
Length of the first string
Length of the second string
Run the metric computation based on the specified mode.
Optionalmode: MetricModeThe mode to run the metric in (optional)
Optionalclear: boolean = trueWhether to clear previous results before running
Run the metric computation based on the specified mode asynchronously.
Optionalmode: MetricModeThe mode to run the metric in (optional)
Optionalclear: boolean = trueWhether to clear previous results before running
PrivaterunRun the metric computation for batch inputs (arrays of strings).
It iterates through each string in the first array and computes the metric against each string in the second array.
PrivaterunRun the metric computation for batch inputs (arrays of strings) asynchronously.
PrivaterunRun the metric computation for pairwise inputs (A[i] vs B[i]).
This method assumes that both a and b are arrays of equal length
and computes the metric only for corresponding index pairs.
PrivaterunRun the metric computation for pairwise inputs (A[i] vs B[i]) asynchronously.
PrivaterunRun the metric computation for single inputs (two strings). Applies preCompute for trivial cases before cache lookup and computation.
If the profiler is active, it will measure time and memory usage.
Pointer to the first string
Pointer to the second string
PrivaterunRun the metric computation for single inputs (two strings) asynchronously.
Pointer to the first string
Pointer to the second string
Set the original inputs to which the results of the metric calculation will refer.
Optionala: MetricInputoriginal input(s) for a
Optionalb: MetricInputoriginal input(s) for b
Determine which mode to run the metric in.
This method checks the provided mode or defaults to the mode specified in options. If no mode is specified, it defaults to 'default'.
Optionalmode: MetricModeThe mode to run the metric in (optional)
Protected StaticclampClamps the similarity result between 0 and 1.
The input similarity to clamp
StaticclearStatic method to clear the cache of metric computations.
Protected StaticswapSwaps two strings and their lengths if the first is longer than the second.
First string
Second string
Length of the first string
Length of the second string
Abstract class representing a generic string metric.