random hello

This commit is contained in:
Ruidy 2021-02-19 13:45:06 +01:00
parent bd1af7b44a
commit 54ae738762
2 changed files with 16 additions and 9 deletions

View file

@ -2,13 +2,14 @@ import {
assertStrictEquals, assertStrictEquals,
assertThrows, assertThrows,
} from "https://deno.land/std@0.87.0/testing/asserts.ts"; } from "https://deno.land/std@0.87.0/testing/asserts.ts";
import { sayHello } from "./index.ts"; import { sayHello, sayRandomHello } from "./index.ts";
import { ValidationError } from "./validation.ts"; import { ValidationError } from "./validation.ts";
import { localesSize } from "./repository/locales.ts"; import { localesSize } from "./repository/locales.ts";
Deno.test("Hello test", () => { Deno.test("Hello test", () => {
const actual = sayHello(); const result = sayRandomHello();
const expected = "Hello, World!"; const actual = typeof result;
const expected = "string";
assertStrictEquals(actual, expected); assertStrictEquals(actual, expected);
}); });

View file

@ -1,13 +1,19 @@
import { idIsValid, ValidationError } from "./validation.ts"; import { idIsValid, ValidationError } from "./validation.ts";
import { getLocaleById } from "./repository/locales.ts"; import { getLocaleById, localesSize } from "./repository/locales.ts";
/** /**
* Display the iconic Hello, World * Display the iconic Hello, World in a random locale
*/ */
export const sayHello = (id?: number) => { export const sayRandomHello = () => {
if (!id) { const id = Math.ceil(Math.random() * localesSize);
return; return sayHello(id);
} };
/**
* Display the iconic Hello, World in a locale identified by id
* @param id Unique identifier
*/
export const sayHello = (id: number): string => {
if (!idIsValid(id)) { if (!idIsValid(id)) {
throw new ValidationError(`Invalid index: ${id}`); throw new ValidationError(`Invalid index: ${id}`);
} }