Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/FoxxMD/string-sameness. Compare the sameness of two strings foxxmd.github.io/string-sameness
compare cosine-similarity dice-coefficient levenshtein-distance sameness string text typescript
Something went wrong. Try again.
2.4 kB · 77 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778export interface StringComparisonOptions { /** * An array of transformations to apply to each string before comparing similarity * */ transforms?: StringTransformFunc[] /** * An array of strategies used to score similarity. All strategies scores are combined for an average high score. * */ strategies?: ComparisonStrategy<ComparisonStrategyResultValue>[] /** * Reorder second string so its token match order of first string as closely as possible * * Useful when only the differences in content are important, but not the order of the content * */ reorder?: boolean
/** * When `reorder` is used this determines how to split each string into the tokens that will be reordered. * * The value of this property is used in String.split() -- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#separator * * @default " " * */ delimiter?: string | RegExp}
export interface StringSamenessResult { strategies: { [key: string]: ComparisonStrategyResult }, highScore: number highScoreWeighted: number}
export interface ComparisonStrategyResultObject { /** * The normalized (0 to 100) score of this comparison * */ score: number /** * The raw value returned by the comparison (not normalized) * */ rawScore?: number
[key: string]: any}
export interface ComparisonStrategyResult extends ComparisonStrategyResultObject { // TODO maybe add more things in the future //scoreFormatted: string}
export interface NamedComparisonStrategyObjectResult extends ComparisonStrategyResultObject { name: string}
export type ComparisonStrategyResultValue = number | ComparisonStrategyResultObject;
export type StrategyFunc<T extends ComparisonStrategyResultValue> = (strA: string, strB: string) => T;
export interface ComparisonStrategy<T extends ComparisonStrategyResultValue> { /** * The name of this strategy * */ name: string /** * A function that accepts two string arguments and returns a number * */ strategy: StrategyFunc<T>
/** * An optional function that accepts two string arguments and returns whether this strategy should be used * */ isValid?: (strA: string, strB: string) => boolean}
export type StringTransformFunc = (str: string) => string;