mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
introduce locales repository
This commit is contained in:
parent
68b73c29e4
commit
80788adc40
5 changed files with 30 additions and 17 deletions
|
|
@ -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 };
|
||||
|
||||
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", () => {
|
||||
|
|
|
|||
10
src/index.ts
10
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;
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
};
|
||||
|
|
@ -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;
|
||||
9
src/repository/locales.ts
Normal file
9
src/repository/locales.ts
Normal 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];
|
||||
Loading…
Reference in a new issue