-
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.
move chooseTechniques algorithm to core
- Loading branch information
Showing
13 changed files
with
216 additions
and
31 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 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 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 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,141 @@ | ||
import { chooseTechniques } from "$core/chooseTechniques/chooseTechniques.ts"; | ||
import { createExam } from "$core/model/Exam.test-helper.ts"; | ||
import { buildExamTable } from "$core/buildExamTable"; | ||
import { createTechnique, tqs } from "$core/model/Technique.test-helper.ts"; | ||
import { assertMock } from "$core/test-utils/assertMock.ts"; | ||
import { shuffleList } from "$core/utils/shuffleList.ts"; | ||
import type { Technique } from "$core/model"; | ||
|
||
const SUWARI_IKKYO_OMOTE = createTechnique("suwari waza", "ai hanmi katate dori", "ikkyo", "omote"); | ||
const SUWARI_IKKYO_URA = createTechnique("suwari waza", "ai hanmi katate dori", "ikkyo", "ura"); | ||
const HANMI_IKKYO_OMOTE = createTechnique("hanmi handachi waza", "ai hanmi katate dori", "ikkyo", "omote"); | ||
const HANMI_IKKYO_URA = createTechnique("hanmi handachi waza", "ai hanmi katate dori", "ikkyo", "ura"); | ||
const TACHI_IKKYO_OMOTE = createTechnique("tachi waza", "ai hanmi katate dori", "ikkyo", "omote"); | ||
const TACHI_IKKYO_URA = createTechnique("tachi waza", "ai hanmi katate dori", "ikkyo", "ura"); | ||
const exams = [ | ||
createExam({ | ||
id: "kyu5", | ||
techniques: buildExamTable([SUWARI_IKKYO_OMOTE, SUWARI_IKKYO_URA]), | ||
}), | ||
createExam({ | ||
id: "kyu4", | ||
techniques: buildExamTable([HANMI_IKKYO_OMOTE, HANMI_IKKYO_URA]), | ||
}), | ||
createExam({ | ||
id: "kyu3", | ||
techniques: buildExamTable([TACHI_IKKYO_OMOTE, TACHI_IKKYO_URA]), | ||
}), | ||
]; | ||
|
||
describe("chooseTechniques", () => { | ||
beforeEach(() => { | ||
assertMock(shuffleList); | ||
shuffleList.mockImplementation((list: Technique[]) => { | ||
return list.toReversed(); | ||
}); | ||
}); | ||
|
||
it("returns an empty array if called without any option", () => { | ||
expect(chooseTechniques(exams, {})).toHaveLength(0); | ||
}); | ||
|
||
it("returns all techniques from the selected exams", () => { | ||
expect(tqs(chooseTechniques(exams, { selectedExams: new Set(["kyu5", "kyu4"]) }))).toEqual( | ||
tqs([SUWARI_IKKYO_OMOTE, SUWARI_IKKYO_URA, HANMI_IKKYO_OMOTE, HANMI_IKKYO_URA]), | ||
); | ||
}); | ||
|
||
it("applies filters", () => { | ||
expect( | ||
tqs( | ||
chooseTechniques(exams, { | ||
selectedExams: new Set(["kyu5", "kyu4", "kyu3"]), | ||
filters: { | ||
kneeFriendly: true, | ||
}, | ||
}), | ||
), | ||
).toEqual(tqs([TACHI_IKKYO_OMOTE, TACHI_IKKYO_URA])); | ||
}); | ||
|
||
it("applies randomization", () => { | ||
expect( | ||
tqs( | ||
chooseTechniques(exams, { | ||
selectedExams: new Set(["kyu5", "kyu4", "kyu3"]), | ||
order: { | ||
randomize: true, | ||
}, | ||
}), | ||
), | ||
).toEqual( | ||
tqs([ | ||
SUWARI_IKKYO_OMOTE, | ||
SUWARI_IKKYO_URA, | ||
HANMI_IKKYO_OMOTE, | ||
HANMI_IKKYO_URA, | ||
TACHI_IKKYO_OMOTE, | ||
TACHI_IKKYO_URA, | ||
]).toReversed(), | ||
); | ||
}); | ||
|
||
it("applies cutoff after randomization", () => { | ||
expect( | ||
tqs( | ||
chooseTechniques(exams, { | ||
selectedExams: new Set(["kyu5", "kyu4", "kyu3"]), | ||
order: { | ||
randomize: true, | ||
includePercent: 50, | ||
}, | ||
}), | ||
), | ||
).toEqual(tqs([TACHI_IKKYO_URA, TACHI_IKKYO_OMOTE, HANMI_IKKYO_URA])); | ||
}); | ||
|
||
it("groups techniques by execution", () => { | ||
const exams = [ | ||
createExam({ | ||
id: "kyu5", | ||
techniques: buildExamTable([SUWARI_IKKYO_OMOTE, HANMI_IKKYO_OMOTE]), | ||
}), | ||
createExam({ | ||
id: "kyu4", | ||
techniques: buildExamTable([SUWARI_IKKYO_URA, HANMI_IKKYO_URA]), | ||
}), | ||
]; | ||
expect( | ||
tqs( | ||
chooseTechniques(exams, { | ||
selectedExams: new Set(["kyu5", "kyu4"]), | ||
}), | ||
), | ||
).toEqual(tqs([SUWARI_IKKYO_OMOTE, SUWARI_IKKYO_URA, HANMI_IKKYO_OMOTE, HANMI_IKKYO_URA])); | ||
}); | ||
|
||
it("groups techniques by execution when randomized", () => { | ||
const exams = [ | ||
createExam({ | ||
id: "kyu5", | ||
techniques: buildExamTable([SUWARI_IKKYO_OMOTE, HANMI_IKKYO_OMOTE]), | ||
}), | ||
createExam({ | ||
id: "kyu4", | ||
techniques: buildExamTable([SUWARI_IKKYO_URA, HANMI_IKKYO_URA]), | ||
}), | ||
]; | ||
expect( | ||
tqs( | ||
chooseTechniques(exams, { | ||
selectedExams: new Set(["kyu5", "kyu4"]), | ||
order: { | ||
randomize: true, | ||
includePercent: 100, | ||
orderExecutions: true, | ||
}, | ||
}), | ||
), | ||
).toEqual(tqs([SUWARI_IKKYO_URA, SUWARI_IKKYO_OMOTE, HANMI_IKKYO_URA, HANMI_IKKYO_OMOTE])); | ||
}); | ||
}); |
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,32 @@ | ||
import type { Exam, Technique } from "$core/model"; | ||
import { resolveExamTables } from "$core/resolveExamTables"; | ||
import { type TechniqueFilters, techniquePredicate } from "./techniqueFilter"; | ||
import { shuffleTechniques } from "$core/shuffleTechniques"; | ||
import { groupTechniques } from "$core/groupTechniques/groupTechniques.ts"; | ||
|
||
export interface ChooseTechniqueOptions { | ||
selectedExams?: Set<string>; | ||
filters?: TechniqueFilters; | ||
order?: OrderOptions; | ||
} | ||
|
||
export interface OrderOptions { | ||
randomize: boolean; | ||
includePercent?: number; | ||
orderExecutions?: boolean; | ||
} | ||
|
||
export function chooseTechniques(allExams: readonly Exam[], options: ChooseTechniqueOptions): Technique[] { | ||
const selectedExams = options.selectedExams ?? new Set(); | ||
const filters = options.filters ?? {}; | ||
const randomize = options.order?.randomize ?? false; | ||
const includePercent = options.order?.includePercent ?? 100; | ||
const orderExecutions = options.order?.orderExecutions ?? false; | ||
|
||
let result = resolveExamTables(allExams.filter((exam) => selectedExams.has(exam.id))); | ||
result = result.filter(techniquePredicate(filters)); | ||
if (randomize) { | ||
result = shuffleTechniques(result, { includePercent }); | ||
} | ||
return groupTechniques(result, { orderExecutions }); | ||
} |
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,2 @@ | ||
export * from "./chooseTechniques"; | ||
export type { TechniqueFilters } from "./techniqueFilter"; |
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 @@ | ||
export * from "./technique-filters"; |
File renamed without changes.
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 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 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 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { shuffleList } from "$core/utils/shuffleList.ts"; | ||
import { type Technique } from "$core/model"; | ||
|
||
export function shuffleTechniques(techniques: Technique[], { coverage = 0.8 } = {}): Technique[] { | ||
export function shuffleTechniques(techniques: Technique[], { includePercent = 100 } = {}): Technique[] { | ||
const shuffledTechniques: Technique[] = shuffleList(techniques); | ||
const sliceEnd = Math.ceil(coverage * techniques.length); | ||
const sliceEnd = Math.ceil((includePercent * techniques.length) / 100); | ||
return shuffledTechniques.slice(0, sliceEnd); | ||
} |
This file was deleted.
Oops, something went wrong.