introduce locales repository

This commit is contained in:
Ruidy 2021-02-19 13:27:16 +01:00
parent 68b73c29e4
commit 80788adc40
5 changed files with 30 additions and 17 deletions

View file

@ -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", () => {

View file

@ -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;

View file

@ -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];
};

View file

@ -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;

View file

@ -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];