A search box suggests "Did you mean …?" only when the query is exactly one typo away from a known word. A typo is one inserted character, one deleted character, or one character replaced by a different one.
Write two functions that take strings a and b:
isOneEditAway(a, b) returns true if exactly one insert, delete or replace turns a into b, and false otherwise.
oneEditDiff(a, b) describes that edit as { type, index, char }, or returns null when the strings are not exactly one edit apart:
type is "insert" (b has one extra character), "delete" (a has one extra character) or "replace".
index is the position of the first character where a and b differ, which is the length of their common prefix.
char is the inserted character (from b), the deleted character (from a), or the new character (from b).
Rules:
A character is one Unicode code point, as produced by for…of, so an emoji counts as one character and index counts code points.
Equal strings are zero edits apart: both functions report "no" (false and null).
If a or b is not a string, throw a TypeError.
Strings can be 1,000,000 characters long. An edit distance table is far too large; work in O(n) time.