From 54ae7387620739e4dc9f8e720b6250d9e30005f1 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 19 Feb 2021 13:45:06 +0100 Subject: [PATCH] random hello --- src/index.test.ts | 7 ++++--- src/index.ts | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index ee939ba..341d2e9 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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); }); diff --git a/src/index.ts b/src/index.ts index 87281b8..c4f9613 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}`); }