Skip to content

Commit

Permalink
fix & refactor checkKeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
djobbo committed Apr 10, 2024
1 parent 3e6cf0c commit 3cc0b4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 4 additions & 0 deletions util/checkKeyword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ describe("Check Keyword", () => {

describe("should return the index of the error if the keyword is wrong", () => {
test("wrong character", () => {
expect(checkKeyword("ai", [["i"], ["a"]])).toBe(0)
expect(checkKeyword("ai", [["i"], ["i"]])).toBe(0)
expect(checkKeyword("ia", [["i"], ["i"]])).toBe(1)

expect(checkKeyword("aoi", [["a"], ["o"], ["i"]])).toBe(-1)
expect(checkKeyword("eoi", [["a"], ["o"], ["i"]])).toBe(0)
expect(checkKeyword("aei", [["a"], ["o"], ["i"]])).toBe(1)
Expand Down
22 changes: 8 additions & 14 deletions util/checkKeyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ export const checkKeyword = (
pronounciation: string[][],
): number => {
let checkIndex = 0
let ok = true
for (let i = 0; i < pronounciation.length; i++) {
let ok = pronounciation.every((p) => {
const rest = inputValue.slice(checkIndex)
for (let j = 0; j < pronounciation[i].length; j++) {
if (rest.length !== 0 && rest.startsWith(pronounciation[i][j])) {
checkIndex += pronounciation[i][j].length
ok = true
break
}
const found = p.find((p) => rest.startsWith(p))

ok = false
if (!found) {
return false
}
}

if (ok && checkIndex === inputValue.length) {
return -1
}
checkIndex += found.length
return true
})

return checkIndex
return ok && checkIndex === inputValue.length ? -1 : checkIndex
}

0 comments on commit 3cc0b4c

Please sign in to comment.