mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +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,
|
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
18
src/index.ts
18
src/index.ts
|
|
@ -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}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue