Skip to content

Commit

Permalink
Extract out duplicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
richrace committed Sep 6, 2024
1 parent a8aa4ef commit 95c8448
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useCourtCase } from "context/CourtCaseContext"
import { useCurrentUser } from "context/CurrentUserContext"
import { Exception } from "types/exceptions"
import getExceptionMessage from "utils/offenceMatcher/getExceptionMessage"
import { getOffenceMatchingException } from "utils/offenceMatcher/getOffenceMatchingException"
import getOffenceMatchingException from "utils/offenceMatcher/getOffenceMatchingException"
import isEnabled from "utils/offenceMatcher/isEnabled"
import offenceMatchingExceptions from "utils/offenceMatcher/offenceMatchingExceptions"
import findCandidates from "../../../../../../utils/offenceMatcher/findCandidates"
Expand Down
37 changes: 3 additions & 34 deletions src/utils/offenceMatcher/getOffenceMatchingException.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import { Exception } from "types/exceptions"
import {
getOffenceMatchingException,
GetOffenceMatchingExceptionResult,
getOffenceMatchingExceptions
} from "./getOffenceMatchingException"
import ExceptionCode from "@moj-bichard7-developers/bichard7-next-data/dist/types/ExceptionCode"
import type { Exception } from "types/exceptions"
import { ExceptionBadgeType } from "utils/exceptions/exceptionBadgeType"

describe("getOffenceMatchingExceptions", () => {
it("returns an empty array when given no exceptions", () => {
const result = getOffenceMatchingExceptions([])
expect(result).toEqual([])
})

it("returns an empty array when given exception that is not in array of valid values", () => {
const exception: Exception = { code: ExceptionCode.HO100304, path: ["test"] }
const result = getOffenceMatchingExceptions([exception])
expect(result).toEqual([])
})

it("returns exception when given exception that is in array of valid values", () => {
const exception: Exception = { code: ExceptionCode.HO100310, path: ["test"] }
const result = getOffenceMatchingExceptions([exception])
expect(result).toEqual([exception])
})

it("returns exception when given multiple exceptions and exception is in array of valid values", () => {
const exceptions: Exception[] = [
{ code: ExceptionCode.HO100304, path: ["test"] },
{ code: ExceptionCode.HO100310, path: ["test"] }
]
const result = getOffenceMatchingExceptions(exceptions)
expect(result).toEqual([exceptions[1]])
})
})
import type { GetOffenceMatchingExceptionResult } from "./getOffenceMatchingException"
import getOffenceMatchingException from "./getOffenceMatchingException"

describe("getOffenceMatchingException", () => {
it("returns undefined when given an empty array", () => {
Expand Down
6 changes: 1 addition & 5 deletions src/utils/offenceMatcher/getOffenceMatchingException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ type GetOffenceMatchingExceptionResult =
}
| undefined

const getOffenceMatchingExceptions = (exceptions: Exception[]): Exception[] => {
return exceptions.filter((exception) => offenceMatchingExceptions.offenceNotMatched.includes(exception.code))
}

const getOffenceMatchingException = (
exceptions: Exception[],
offenceIndex: number
Expand Down Expand Up @@ -48,5 +44,5 @@ const getOffenceMatchingException = (
}
}

export { getOffenceMatchingException, getOffenceMatchingExceptions }
export default getOffenceMatchingException
export type { GetOffenceMatchingExceptionResult }
31 changes: 31 additions & 0 deletions src/utils/offenceMatcher/getOffenceMatchingExceptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ExceptionCode from "@moj-bichard7-developers/bichard7-next-data/dist/types/ExceptionCode"
import type { Exception } from "types/exceptions"
import getOffenceMatchingExceptions from "./getOffenceMatchingExceptions"

describe("getOffenceMatchingExceptions", () => {
it("returns an empty array when given no exceptions", () => {
const result = getOffenceMatchingExceptions([])
expect(result).toEqual([])
})

it("returns an empty array when given exception that is not in array of valid values", () => {
const exception: Exception = { code: ExceptionCode.HO100312, path: ["test"] }
const result = getOffenceMatchingExceptions([exception])
expect(result).toEqual([])
})

it("returns exception when given exception that is in array of valid values", () => {
const exception: Exception = { code: ExceptionCode.HO100310, path: ["test"] }
const result = getOffenceMatchingExceptions([exception])
expect(result).toEqual([exception])
})

it("returns exception when given multiple exceptions and exception is in array of valid values", () => {
const exceptions: Exception[] = [
{ code: ExceptionCode.HO100310, path: ["test"] },
{ code: ExceptionCode.HO100312, path: ["test"] }
]
const result = getOffenceMatchingExceptions(exceptions)
expect(result).toEqual([exceptions[0]])
})
})
7 changes: 7 additions & 0 deletions src/utils/offenceMatcher/getOffenceMatchingExceptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Exception } from "types/exceptions"
import offenceMatchingExceptions from "./offenceMatchingExceptions"

const getOffenceMatchingExceptions = (exceptions: Exception[]): Exception[] =>
exceptions.filter((exception) => offenceMatchingExceptions.offenceNotMatched.includes(exception.code))

export default getOffenceMatchingExceptions
30 changes: 1 addition & 29 deletions src/utils/offenceMatcher/hasOffenceMatchingExceptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ExceptionCode from "@moj-bichard7-developers/bichard7-next-data/dist/types/ExceptionCode"
import hasOffenceMatchingExceptions, { filterOffenceMatchingException } from "./hasOffenceMatchingExceptions"
import { Exception } from "types/exceptions"
import hasOffenceMatchingExceptions from "./hasOffenceMatchingExceptions"

describe("hasOffenceMatchingExceptions", () => {
it("returns false when given empty array", () => {
Expand Down Expand Up @@ -29,31 +29,3 @@ describe("hasOffenceMatchingExceptions", () => {
expect(result).toBe(true)
})
})

describe("filterOffenceMatchingException", () => {
it("returns an empty array when given no exceptions", () => {
const result = filterOffenceMatchingException([])
expect(result).toEqual([])
})

it("returns an empty array when given exception that is not in array of valid values", () => {
const exception: Exception = { code: ExceptionCode.HO100312, path: ["test"] }
const result = filterOffenceMatchingException([exception])
expect(result).toEqual([])
})

it("returns exception when given exception that is in array of valid values", () => {
const exception: Exception = { code: ExceptionCode.HO100310, path: ["test"] }
const result = filterOffenceMatchingException([exception])
expect(result).toEqual([exception])
})

it("returns exception when given multiple exceptions and exception is in array of valid values", () => {
const exceptions: Exception[] = [
{ code: ExceptionCode.HO100310, path: ["test"] },
{ code: ExceptionCode.HO100312, path: ["test"] }
]
const result = filterOffenceMatchingException(exceptions)
expect(result).toEqual([exceptions[0]])
})
})
8 changes: 2 additions & 6 deletions src/utils/offenceMatcher/hasOffenceMatchingExceptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Exception } from "types/exceptions"
import offenceMatchingExceptions from "./offenceMatchingExceptions"
import getOffenceMatchingExceptions from "./getOffenceMatchingExceptions"

const filterOffenceMatchingException = (exceptions: Exception[]): Exception[] =>
exceptions.filter((exception) => offenceMatchingExceptions.offenceNotMatched.includes(exception.code))
const hasOffenceMatchingExceptions = (exceptions: Exception[]) => getOffenceMatchingExceptions(exceptions).length > 0

const hasOffenceMatchingExceptions = (exceptions: Exception[]) => filterOffenceMatchingException(exceptions).length > 0

export { filterOffenceMatchingException }
export default hasOffenceMatchingExceptions

0 comments on commit 95c8448

Please sign in to comment.