diff --git a/src/index.test.ts b/src/index.test.ts index 0704715..ee939ba 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -4,7 +4,7 @@ import { } from "https://deno.land/std@0.87.0/testing/asserts.ts"; import { sayHello } from "./index.ts"; import { ValidationError } from "./validation.ts"; -import { localesSize } from "./data.ts"; +import { localesSize } from "./repository/locales.ts"; Deno.test("Hello test", () => { const actual = sayHello(); @@ -13,11 +13,19 @@ Deno.test("Hello test", () => { }); Deno.test("Specific Hello", () => { - const id = 25; - const actual = sayHello(id); - const expected = "Salut le Monde!"; + type TestCase = { in: number; want: string }; - assertStrictEquals(actual, expected); + const testCases: TestCase[] = [ + { in: 25, want: "Salut le Monde!" }, + { in: 21, want: "Hello World!" }, + ]; + + for (const test of testCases) { + const actual = sayHello(test.in); + const expected = test.want; + + assertStrictEquals(actual, expected); + } }); Deno.test("Hello fails for non strictly positive values", () => { diff --git a/src/index.ts b/src/index.ts index 5a6c0d3..40098be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,17 @@ import { ValidationError } from "./validation.ts"; -import { localesSize } from "./data.ts"; +import { getLocaleById, localesSize } from "./repository/locales.ts"; /** * Display the iconic Hello, World */ export const sayHello = (id?: number) => { - if (id && idInValidRange(id)) { + if (!id) { + return; + } + if (idInValidRange(id)) { throw new ValidationError(`Invalid index: ${id}`); } - // const hello = getLocaleByNumber(id); - return (id === 5) ? "Bonjour le monde" : "Hello, World!"; + return getLocaleById(id); }; const idInValidRange = (id: number) => id < 1 || id >= localesSize; diff --git a/src/data.ts b/src/repository/data.ts similarity index 94% rename from src/data.ts rename to src/repository/data.ts index b20170c..d998330 100644 --- a/src/data.ts +++ b/src/repository/data.ts @@ -1,4 +1,4 @@ -const locales = { +export const locales = { Afrikaans: "Hallo, wêreld!", Albanian: "Pershëndetje Botë", Arabic: "أهلاً بالعالم (Ahlan bil 'Alam)", @@ -78,9 +78,3 @@ const locales = { Yiddish: "העלא וועלט (hela velt)", Zulu: "Sawubona Mhlaba", }; - -export const localesSize = Object.keys(locales).length; - -export const getLocaleById = (id: number) => { - return Object.values(locales)[id - 1]; -}; diff --git a/src/data.test.ts b/src/repository/locales.test.ts similarity index 86% rename from src/data.test.ts rename to src/repository/locales.test.ts index 7a76487..ea5e9d2 100644 --- a/src/data.test.ts +++ b/src/repository/locales.test.ts @@ -1,5 +1,5 @@ import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts"; -import { getLocaleById } from "./data.ts"; +import { getLocaleById } from "./locales.ts"; Deno.test("Get locale by id", () => { const id = 25; diff --git a/src/repository/locales.ts b/src/repository/locales.ts new file mode 100644 index 0000000..07c827d --- /dev/null +++ b/src/repository/locales.ts @@ -0,0 +1,9 @@ +import { locales } from "./data.ts"; + +export const localesSize = Object.keys(locales).length; + +/** + * Return a given locale identified by its id + * @param id unique identifier + */ +export const getLocaleById = (id: number) => Object.values(locales)[id - 1];