diff --git a/README.md b/README.md index 776f797..4d06019 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,6 @@ Web app which returns the iconic Hello, World in various locales ## Features -- [ ] Print hello world in a random locale -- [ ] Select the locale by id +- [x] Print hello world in a random locale +- [x] Select the locale by id - [ ] Serve via internet diff --git a/src/index.test.ts b/src/index.test.ts index 341d2e9..c7348f7 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,12 +1,8 @@ -import { - assertStrictEquals, - assertThrows, -} from "https://deno.land/std@0.87.0/testing/asserts.ts"; +import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts"; import { sayHello, sayRandomHello } from "./index.ts"; -import { ValidationError } from "./validation.ts"; -import { localesSize } from "./repository/locales.ts"; +import { ID } from "./types.ts"; -Deno.test("Hello test", () => { +Deno.test("Random Hello", () => { const result = sayRandomHello(); const actual = typeof result; const expected = "string"; @@ -14,11 +10,11 @@ Deno.test("Hello test", () => { }); Deno.test("Specific Hello", () => { - type TestCase = { in: number; want: string }; + type TestCase = { in: ID; want: string }; const testCases: TestCase[] = [ - { in: 25, want: "Salut le Monde!" }, - { in: 21, want: "Hello World!" }, + { in: new ID(25), want: "Salut le Monde!" }, + { in: new ID(21), want: "Hello World!" }, ]; for (const test of testCases) { @@ -28,17 +24,3 @@ Deno.test("Specific Hello", () => { assertStrictEquals(actual, expected); } }); - -Deno.test("Hello fails for non strictly positive values", () => { - const id = -1; - assertThrows(() => sayHello(id), ValidationError, "Invalid index: -1"); -}); - -Deno.test("Hello fails for too large values", () => { - const id = localesSize; - assertThrows( - () => sayHello(id), - ValidationError, - `Invalid index: ${localesSize}`, - ); -}); diff --git a/src/index.ts b/src/index.ts index c4f9613..61fad20 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,21 +1,19 @@ -import { idIsValid, ValidationError } from "./validation.ts"; import { getLocaleById, localesSize } from "./repository/locales.ts"; +import { ID } from "./types.ts"; /** * Display the iconic Hello, World in a random locale */ export const sayRandomHello = () => { const id = Math.ceil(Math.random() * localesSize); - return sayHello(id); + const val = new ID(id); + return sayHello(val); }; /** * Display the iconic Hello, World in a locale identified by id * @param id Unique identifier */ -export const sayHello = (id: number): string => { - if (!idIsValid(id)) { - throw new ValidationError(`Invalid index: ${id}`); - } - return getLocaleById(id); +export const sayHello = (id: ID): string => { + return getLocaleById(id.id); }; diff --git a/src/types.test.ts b/src/types.test.ts new file mode 100644 index 0000000..395ff89 --- /dev/null +++ b/src/types.test.ts @@ -0,0 +1,16 @@ +import { ID } from "./types.ts"; +import { ValidationError } from "./validation.ts"; +import { assertThrows } from "https://deno.land/std@0.87.0/testing/asserts.ts"; +import { localesSize } from "./repository/locales.ts"; + +Deno.test("ID validation fails for non strictly positive values", () => { + assertThrows(() => new ID(-1), ValidationError, "Invalid index: -1"); +}); + +Deno.test("Hello fails for too large values", () => { + assertThrows( + () => new ID(localesSize), + ValidationError, + `Invalid index: ${localesSize}`, + ); +}); diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..e548fe9 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,16 @@ +import { ValidationError } from "./validation.ts"; +import { localesSize } from "./repository/locales.ts"; + +export class ID { + constructor(public id: number) { + this.#isValid(id); + } + + #idIsValid = (id: number) => id > 0 && id < localesSize; + + #isValid = (id: number) => { + if (!this.#idIsValid(id)) { + throw new ValidationError(`Invalid index: ${id}`); + } + }; +} diff --git a/src/validation.ts b/src/validation.ts index f3a52c9..89bdf8b 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -4,8 +4,5 @@ * @example * `throw new ValidationError("Bad id")` */ -import { localesSize } from "./repository/locales.ts"; export class ValidationError extends Error {} - -export const idIsValid = (id: number) => id > 0 && id < localesSize;