mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
random hello
This commit is contained in:
parent
bd1af7b44a
commit
54ae738762
2 changed files with 16 additions and 9 deletions
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
|||
18
src/index.ts
18
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}`);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue