From 1a7fa33e3131ecd786dd16668bfe165756a50955 Mon Sep 17 00:00:00 2001 From: Dennis Bauer Date: Sun, 3 May 2026 21:47:25 +0200 Subject: [PATCH] Fixed some typing --- Problems/Bingo-Check/bingo-check.test.ts | 2 +- Problems/Bingo-Check/solver.ts | 2 +- .../itinerary-in-alphabetical-order.test.ts | 56 +++++++++---------- .../Itinerary-In-Alphabetical-Order/solver.ts | 4 +- .../Keyword-Cipher/keyword-cipher.test.ts | 22 +++----- Problems/Keyword-Cipher/solver.ts | 6 +- Problems/Missing-Number/solver.ts | 4 +- .../Nom-Nom-Numbers/nom-nom-numbers.test.ts | 6 -- utilities/types/fixedArray.ts | 4 +- utilities/types/intRange.ts | 5 +- utilities/types/nonEmptyArray.ts | 7 +++ utilities/types/nonEmptyString.ts | 7 +++ 12 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 utilities/types/nonEmptyArray.ts create mode 100644 utilities/types/nonEmptyString.ts diff --git a/Problems/Bingo-Check/bingo-check.test.ts b/Problems/Bingo-Check/bingo-check.test.ts index 68474c0..761c985 100644 --- a/Problems/Bingo-Check/bingo-check.test.ts +++ b/Problems/Bingo-Check/bingo-check.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import type { FixedArray } from "../../utilities/types/fixedArray"; +import type FixedArray from "../../utilities/types/fixedArray"; import bingoCheck from "./solver"; type CardType = FixedArray, 5>; diff --git a/Problems/Bingo-Check/solver.ts b/Problems/Bingo-Check/solver.ts index bd91055..d0f4eba 100644 --- a/Problems/Bingo-Check/solver.ts +++ b/Problems/Bingo-Check/solver.ts @@ -1,4 +1,4 @@ -import type { FixedArray } from "../../utilities/types/fixedArray"; +import type FixedArray from "../../utilities/types/fixedArray"; export default function bingoCheck(card: FixedArray, 5>): boolean { if ( diff --git a/Problems/Itinerary-In-Alphabetical-Order/itinerary-in-alphabetical-order.test.ts b/Problems/Itinerary-In-Alphabetical-Order/itinerary-in-alphabetical-order.test.ts index 0675be2..83971a2 100644 --- a/Problems/Itinerary-In-Alphabetical-Order/itinerary-in-alphabetical-order.test.ts +++ b/Problems/Itinerary-In-Alphabetical-Order/itinerary-in-alphabetical-order.test.ts @@ -2,39 +2,39 @@ import { describe, expect, it } from "vitest"; import findPath from "./solver"; describe("Finds the correct path for given tickets", () => { - const input1 = [ - { start: "C", end: "F" }, - { start: "A", end: "C" }, - { start: "I", end: "Z" }, - { start: "F", end: "I" }, - ]; - it("should return the correct path for 4 tickets", () => { - expect(findPath(input1)).toEqual(["A", "C", "F", "I", "Z"]); + expect( + findPath([ + { start: "C", end: "F" }, + { start: "A", end: "C" }, + { start: "I", end: "Z" }, + { start: "F", end: "I" }, + ]) + ).toEqual(["A", "C", "F", "I", "Z"]); }); - const input2 = [ - { start: "A", end: "C" }, - { start: "A", end: "B" }, - { start: "C", end: "B" }, - { start: "B", end: "A" }, - { start: "B", end: "C" }, - ]; - it("should return the correct path for 5 tickets with same start destinations", () => { - expect(findPath(input2)).toEqual(["A", "B", "A", "C", "B", "C"]); + expect( + findPath([ + { start: "A", end: "C" }, + { start: "A", end: "B" }, + { start: "C", end: "B" }, + { start: "B", end: "A" }, + { start: "B", end: "C" }, + ]) + ).toEqual(["A", "B", "A", "C", "B", "C"]); }); - const input3 = [ - { start: "Y", end: "L" }, - { start: "D", end: "A" }, - { start: "A", end: "D" }, - { start: "R", end: "Y" }, - { start: "A", end: "R" }, - ]; - it("should return the correct path for 5 tickets", () => { - expect(findPath(input3)).toEqual(["A", "D", "A", "R", "Y", "L"]); + expect( + findPath([ + { start: "Y", end: "L" }, + { start: "D", end: "A" }, + { start: "A", end: "D" }, + { start: "R", end: "Y" }, + { start: "A", end: "R" }, + ]) + ).toEqual(["A", "D", "A", "R", "Y", "L"]); }); it("should return the correct path for 6 tickets which have the same start and end", () => { @@ -52,10 +52,6 @@ describe("Finds the correct path for given tickets", () => { }); describe("Throws the correct error when handling wrong input", () => { - it("should throw the correct error for an empty array", () => { - expect(() => findPath([])).toThrowError("It's not possible to find a path when no tickets are given!"); - }); - it("should throw the correct error when no start ticket (Ticket with start = 'A') is given", () => { expect(() => findPath([ diff --git a/Problems/Itinerary-In-Alphabetical-Order/solver.ts b/Problems/Itinerary-In-Alphabetical-Order/solver.ts index 696499c..e90d41f 100644 --- a/Problems/Itinerary-In-Alphabetical-Order/solver.ts +++ b/Problems/Itinerary-In-Alphabetical-Order/solver.ts @@ -1,9 +1,11 @@ +import type NonEmptyArray from "../../utilities/types/nonEmptyArray"; + interface Ticket { start: string; end: string; } -export default function findPath(tickets: Ticket[]): string[] { +export default function findPath(tickets: NonEmptyArray): string[] { if (tickets.length <= 0) throw new Error("It's not possible to find a path when no tickets are given!"); const path = findTicketPath("A", tickets, ["A"]); diff --git a/Problems/Keyword-Cipher/keyword-cipher.test.ts b/Problems/Keyword-Cipher/keyword-cipher.test.ts index a26622f..b94eae6 100644 --- a/Problems/Keyword-Cipher/keyword-cipher.test.ts +++ b/Problems/Keyword-Cipher/keyword-cipher.test.ts @@ -1,38 +1,32 @@ import { describe, expect, it } from "vitest"; -import keyword_cipher from "./solver"; +import keywordCipher from "./solver"; describe("Encrypt the word correct", () => { it("should return keyabc for abchij", () => { - expect(keyword_cipher("keyword", "abchij")).toEqual("keyabc"); + expect(keywordCipher("keyword", "abchij")).toEqual("keyabc"); }); it("should return pur for abc", () => { - expect(keyword_cipher("purplepineapple", "abc")).toEqual("pur"); + expect(keywordCipher("purplepineapple", "abc")).toEqual("pur"); }); it("should return samucq for edabit", () => { - expect(keyword_cipher("mubashir", "edabit")).toEqual("samucq"); + expect(keywordCipher("mubashir", "edabit")).toEqual("samucq"); }); it("should return eta for abc", () => { - expect(keyword_cipher("etaoinshrdlucmfwypvbgkjqxz", "abc")).toEqual("eta"); + expect(keywordCipher("etaoinshrdlucmfwypvbgkjqxz", "abc")).toEqual("eta"); }); it("should return qxz for xyz", () => { - expect(keyword_cipher("etaoinshrdlucmfwypvbgkjqxz", "xyz")).toEqual("qxz"); + expect(keywordCipher("etaoinshrdlucmfwypvbgkjqxz", "xyz")).toEqual("qxz"); }); it("should return eirfg for aeiou", () => { - expect(keyword_cipher("etaoinshrdlucmfwypvbgkjqxz", "aeiou")).toEqual("eirfg"); + expect(keywordCipher("etaoinshrdlucmfwypvbgkjqxz", "aeiou")).toEqual("eirfg"); }); it("should return EiRfg for AeIou", () => { - expect(keyword_cipher("etaoinshrdlucmfwypvbgkjqxz", "AeIou.")).toEqual("EiRfg."); - }); -}); - -describe("While trying to encrypt a word throws the correct error", () => { - it("should throw the correct error for an equation with no result", () => { - expect(() => keyword_cipher("", "")).toThrowError("An empty word cannot be encrypted"); + expect(keywordCipher("etaoinshrdlucmfwypvbgkjqxz", "AeIou.")).toEqual("EiRfg."); }); }); diff --git a/Problems/Keyword-Cipher/solver.ts b/Problems/Keyword-Cipher/solver.ts index 2473c41..5311bed 100644 --- a/Problems/Keyword-Cipher/solver.ts +++ b/Problems/Keyword-Cipher/solver.ts @@ -1,4 +1,6 @@ -export default function keyword_cipher(keyword: string, word: string): string { +import type NonEmptyString from "../../utilities/types/nonEmptyString"; + +export default function keywordCipher(keyword: NonEmptyString, word: string): string { // biome-ignore format: the array should not be formatted const alphabetA = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; // biome-ignore format: the array should not be formatted @@ -6,7 +8,7 @@ export default function keyword_cipher(keyword: string, word: string): string { if (word.length <= 0) throw new Error("An empty word cannot be encrypted"); - let extraAlphabet = keyword; + let extraAlphabet: NonEmptyString | string = keyword; extraAlphabet.split("").forEach((e) => { if (alphabetA.some((value) => value === e)) alphabetA.splice(alphabetA.indexOf(e), 1); diff --git a/Problems/Missing-Number/solver.ts b/Problems/Missing-Number/solver.ts index 26280f8..6ef205a 100644 --- a/Problems/Missing-Number/solver.ts +++ b/Problems/Missing-Number/solver.ts @@ -1,5 +1,5 @@ -import type { FixedArray } from "../../utilities/types/fixedArray"; -import type { IntRange } from "../../utilities/types/intRange"; +import type FixedArray from "../../utilities/types/fixedArray"; +import type IntRange from "../../utilities/types/intRange"; export default function missingNumber(array: FixedArray, 9>): number { return 55 - array.reduce((preVal, val) => preVal + val, 0); diff --git a/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts b/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts index 9d8574e..82a15bb 100644 --- a/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts +++ b/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts @@ -42,9 +42,3 @@ describe("Returns the correct remaining numbers", () => { expect(nom_nom([2])).toEqual([2]); }); }); - -// describe("Another description for the tests. Maybe those which are throwing errors", () => { -// it("should return something for this input", () => { -// expect(true).toEqual(true); -// }); -// }); diff --git a/utilities/types/fixedArray.ts b/utilities/types/fixedArray.ts index 119ea4b..cad8d9d 100644 --- a/utilities/types/fixedArray.ts +++ b/utilities/types/fixedArray.ts @@ -8,4 +8,6 @@ type GrowToSize, N extends number> = { 1: GrowToSize, N>; }[A["length"] extends N ? 0 : 1]; -export type FixedArray = GrowToSize; +type FixedArray = GrowToSize; + +export default FixedArray; diff --git a/utilities/types/intRange.ts b/utilities/types/intRange.ts index ed5b8df..0542dd4 100644 --- a/utilities/types/intRange.ts +++ b/utilities/types/intRange.ts @@ -6,4 +6,7 @@ type Enumerate = Acc["length"] exte ? Acc[number] : Enumerate; -export type IntRange = Exclude, Enumerate>; +// A number from F to T +type IntRange = Exclude, Enumerate>; + +export default IntRange; diff --git a/utilities/types/nonEmptyArray.ts b/utilities/types/nonEmptyArray.ts new file mode 100644 index 0000000..e2dea24 --- /dev/null +++ b/utilities/types/nonEmptyArray.ts @@ -0,0 +1,7 @@ +// Source - https://stackoverflow.com/a/56006703 +// Posted by jcalz, modified by community. See post 'Timeline' for change history +// Retrieved 2026-05-03, License - CC BY-SA 4.0 + +type NonEmptyArray = [T, ...T[]]; + +export default NonEmptyArray; diff --git a/utilities/types/nonEmptyString.ts b/utilities/types/nonEmptyString.ts new file mode 100644 index 0000000..7c11dab --- /dev/null +++ b/utilities/types/nonEmptyString.ts @@ -0,0 +1,7 @@ +// Source - https://stackoverflow.com/a/69252250 +// Posted by Grochni +// Retrieved 2026-05-03, License - CC BY-SA 4.0 + +type NonEmptyString = "" extends T ? never : T; + +export default NonEmptyString;