diff --git a/lib/natural/distance/index.d.ts b/lib/natural/distance/index.d.ts index a0f36043e..fbac8c4dc 100644 --- a/lib/natural/distance/index.d.ts +++ b/lib/natural/distance/index.d.ts @@ -40,10 +40,7 @@ declare interface DamerauLevenshteinDistanceOptions { /** @default 1 */ transposition_cost?: number /** @default false */ - search?: boolean - /** @default false */ restricted?: boolean - damerau?: boolean } declare interface SubstringDistanceResult { @@ -66,23 +63,23 @@ export function LevenshteinDistance ( * Valid edit operations are: * - transposition, insertion, deletion, and substitution */ -export function DamerauLevenshteinDistance ( +export function LevenshteinDistanceSearch ( source: string, target: string, - options: DamerauLevenshteinDistanceOptions & { search: true } + options?: DamerauLevenshteinDistanceOptions ): SubstringDistanceResult export function DamerauLevenshteinDistance ( source: string, target: string, - options?: DamerauLevenshteinDistanceOptions & { search?: false } + options?: DamerauLevenshteinDistanceOptions ): number -export function DamerauLevenshteinDistance ( +export function DamerauLevenshteinDistanceSearch ( source: string, target: string, - options: DamerauLevenshteinDistanceOptions & { search: boolean } -): number | SubstringDistanceResult + options?: DamerauLevenshteinDistanceOptions +): SubstringDistanceResult export function DiceCoefficient (str1: string, str2: string): number diff --git a/ts_spec/damerau_levenshtein_spec.ts b/ts_spec/damerau_levenshtein_spec.ts new file mode 100644 index 000000000..9dcbca670 --- /dev/null +++ b/ts_spec/damerau_levenshtein_spec.ts @@ -0,0 +1,53 @@ +'use strict' + +import { DamerauLevenshteinDistance, DamerauLevenshteinDistanceSearch } from 'lib/natural' + +describe('DamerauLevenshtein', function () { + describe('default', function () { + it('should be 0 when given equal strings', function () { + expect(DamerauLevenshteinDistance('test', 'test')).toBe(0) + }) + + it('should calculate 1 for adjacent transposition', function () { + expect(DamerauLevenshteinDistance('za', 'az')).toBe(1) + expect(DamerauLevenshteinDistance('Tomato', 'oTmato')).toBe(1) + }) + + it('should handle custom transposition_cost', function () { + expect(DamerauLevenshteinDistance('za', 'az', { transposition_cost: 0 })).toBe(0) + }) + + it('should calculate 2 when there are 2 transpositions', function () { + expect(DamerauLevenshteinDistance('tomato', 'otmaot')).toBe(2) + }) + + it('should calculate 2 for 1 transposition and 1 insertion', function () { + expect(DamerauLevenshteinDistance('CA', 'ABC')).toBe(2) + expect(DamerauLevenshteinDistance('a cat', 'a abct')).toBe(2) + }) + }) + + describe('options.restricted = true', function () { + const restricted = { restricted: true } + it('should calculate 0 for equal strings', function () { + expect(DamerauLevenshteinDistance('identity', 'identity', restricted)).toBe(0) + }) + it('should calculate 1 for an adjacent transposition', function () { + expect(DamerauLevenshteinDistance('za', 'az', restricted)).toBe(1) + }) + it('should not count transposition more than 1 char away', function () { + expect(DamerauLevenshteinDistance('CA', 'ABC', restricted)).toBe(3) + }) + }) + + it('should combine search with Damerau', function () { + const source = 'The RainCoat BookStore' + const target = 'All the best books are here at the Rain Coats Book Store' + const result = DamerauLevenshteinDistanceSearch(source, target) + expect(result).toEqual({ + substring: 'the Rain Coats Book Store', + distance: 4, + offset: 31 + }) + }) +})