2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2023-11-05 12:49:00 +00:00
|
|
|
import { closestString } from "./closest_string.ts";
|
|
|
|
|
2023-12-20 09:58:55 +00:00
|
|
|
Deno.test("closestString() handles basic example", function () {
|
2023-11-05 12:49:00 +00:00
|
|
|
const words = ["hi", "hello", "help"];
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
JSON.stringify(closestString("hep", words)),
|
2023-12-12 10:34:42 +00:00
|
|
|
'"help"',
|
2023-11-05 12:49:00 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-12-20 09:58:55 +00:00
|
|
|
Deno.test("closestString() handles case sensitive example 1", function () {
|
2023-11-05 12:49:00 +00:00
|
|
|
const words = ["hi", "hello", "help"];
|
|
|
|
|
|
|
|
// this is why caseSensitive is OFF by default; very unintuitive until something better than levenshtein_distance is used
|
|
|
|
assertEquals(
|
|
|
|
JSON.stringify(closestString("HELP", words, { caseSensitive: true })),
|
|
|
|
'"hi"',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-12-20 09:58:55 +00:00
|
|
|
Deno.test("closestString() handles case sensitive example 2", function () {
|
2023-11-05 12:49:00 +00:00
|
|
|
const words = ["HI", "HELLO", "HELP"];
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
JSON.stringify(closestString("he", words, { caseSensitive: true })),
|
|
|
|
'"HI"',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-12-20 09:58:55 +00:00
|
|
|
Deno.test("closestString() handles empty input", function () {
|
2023-11-05 12:49:00 +00:00
|
|
|
assertThrows(
|
|
|
|
() => closestString("he", []),
|
|
|
|
Error,
|
|
|
|
"When using closestString(), the possibleWords array must contain at least one word",
|
|
|
|
);
|
|
|
|
});
|