Skip to content

Commit

Permalink
rename checkKeyword to checkProunounciation
Browse files Browse the repository at this point in the history
  • Loading branch information
djobbo committed Apr 10, 2024
1 parent 3cc0b4c commit 5e83330
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
4 changes: 2 additions & 2 deletions components/Steps/KeywordChallenge.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type KeywordChallengeStep } from "@/data/levels"
import { useMemo, useState } from "react"
import { Button } from "@/components/Button"
import { checkKeyword } from "@/util/checkKeyword"
import { checkPronounciation } from "@/util/checkPronounciation"

type KeywordChallengeStepContentProps = {
step: KeywordChallengeStep
Expand Down Expand Up @@ -60,7 +60,7 @@ export const KeywordChallenge = ({
<button
type="button"
onClick={() => {
const errorIndex = checkKeyword(
const errorIndex = checkPronounciation(
inputValue.toLowerCase(),
step.pronounciation,
)
Expand Down
54 changes: 0 additions & 54 deletions util/checkKeyword.test.ts

This file was deleted.

19 changes: 0 additions & 19 deletions util/checkKeyword.ts

This file was deleted.

58 changes: 58 additions & 0 deletions util/checkPronounciation.test.ts
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)
})
})
})
25 changes: 25 additions & 0 deletions util/checkPronounciation.ts
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
}

0 comments on commit 5e83330

Please sign in to comment.