-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename checkKeyword to checkProunounciation
- Loading branch information
Showing
5 changed files
with
85 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { checkPronounciation } from "./checkPronounciation" | ||
|
||
describe("Check Pronounciation", () => { | ||
describe("Should return -1 if the pronounciation is right", () => { | ||
test("single solution", () => { | ||
expect(checkPronounciation("ai", [["a"], ["i"]])).toBe(-1) | ||
}) | ||
|
||
test("multiple solutions", () => { | ||
expect(checkPronounciation("ichi", [["i"], ["chi", "ti"]])).toBe(-1) | ||
expect(checkPronounciation("iti", [["i"], ["chi", "ti"]])).toBe(-1) | ||
|
||
expect(checkPronounciation("jaa", [["ja", "zya"], ["a"]])).toBe(-1) | ||
expect(checkPronounciation("zyaa", [["ja", "zya"], ["a"]])).toBe(-1) | ||
}) | ||
}) | ||
|
||
describe("should return the index of the error if the pronounciation is wrong", () => { | ||
test("wrong character", () => { | ||
expect(checkPronounciation("ai", [["i"], ["a"]])).toBe(0) | ||
expect(checkPronounciation("ai", [["i"], ["i"]])).toBe(0) | ||
expect(checkPronounciation("ia", [["i"], ["i"]])).toBe(1) | ||
|
||
expect(checkPronounciation("aoi", [["a"], ["o"], ["i"]])).toBe(-1) | ||
expect(checkPronounciation("eoi", [["a"], ["o"], ["i"]])).toBe(0) | ||
expect(checkPronounciation("aei", [["a"], ["o"], ["i"]])).toBe(1) | ||
expect(checkPronounciation("aee", [["a"], ["o"], ["i"]])).toBe(1) | ||
expect(checkPronounciation("aoe", [["a"], ["o"], ["i"]])).toBe(2) | ||
}) | ||
|
||
test("wrong character (multiple solutions)", () => { | ||
expect(checkPronounciation("iti", [["i"], ["chi", "ti"]])).toBe(-1) | ||
expect(checkPronounciation("ici", [["i"], ["chi", "ti"]])).toBe(1) | ||
expect(checkPronounciation("tci", [["i"], ["chi", "ti"]])).toBe(0) | ||
}) | ||
|
||
test("missing character", () => { | ||
expect(checkPronounciation("ai", [["a"], ["i"], ["i"]])).toBe(2) | ||
expect(checkPronounciation("ai", [["a"], ["i"], ["a"]])).toBe(2) | ||
expect(checkPronounciation("ai", [["a"], ["i"], ["o"]])).toBe(2) | ||
}) | ||
|
||
test("missing character (multiple solutions)", () => { | ||
expect( | ||
checkPronounciation("iti", [["i"], ["chi", "ti"], ["i"]]), | ||
).toBe(3) | ||
expect( | ||
checkPronounciation("ichi", [["i"], ["chi", "ti"], ["i"]]), | ||
).toBe(4) | ||
}) | ||
|
||
test("extra character", () => { | ||
expect(checkPronounciation("aii", [["a"], ["i"]])).toBe(2) | ||
expect(checkPronounciation("aiii", [["a"], ["i"]])).toBe(2) | ||
expect(checkPronounciation("aiii", [["a"], ["i"], ["i"]])).toBe(3) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Check if the input value is a is a valid pronounciation of the keyword | ||
* @param inputValue - The input value to check | ||
* @param pronounciation - The pronounciation of the keyword | ||
* @returns -1 if the keyword is right, otherwise the index of the error | ||
*/ | ||
export const checkPronounciation = ( | ||
inputValue: string, | ||
pronounciation: string[][], | ||
): number => { | ||
let checkIndex = 0 | ||
let ok = pronounciation.every((p) => { | ||
const rest = inputValue.slice(checkIndex) | ||
const found = p.find((p) => rest.startsWith(p)) | ||
|
||
if (!found) return false | ||
|
||
checkIndex += found.length | ||
return true | ||
}) | ||
|
||
if (ok && checkIndex === inputValue.length) return -1 | ||
|
||
return checkIndex | ||
} |