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,
assertThrows,
} 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 { localesSize } from "./repository/locales.ts";
Deno.test("Hello test", () => {
const actual = sayHello();
const expected = "Hello, World!";
const result = sayRandomHello();
const actual = typeof result;
const expected = "string";
assertStrictEquals(actual, expected);
});

View file

@ -1,13 +1,19 @@
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) => {
if (!id) {
return;
}
export const sayRandomHello = () => {
const id = Math.ceil(Math.random() * localesSize);
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)) {
throw new ValidationError(`Invalid index: ${id}`);
}