mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +00:00
refactor: locale types
This commit is contained in:
parent
2314e82341
commit
2517686f8c
5 changed files with 36 additions and 24 deletions
1
deps.ts
1
deps.ts
|
|
@ -1,5 +1,6 @@
|
||||||
export {
|
export {
|
||||||
assert,
|
assert,
|
||||||
|
assertEquals,
|
||||||
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";
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
import { assertStrictEquals } from "../../deps.ts";
|
import { assertEquals } from "../../deps.ts";
|
||||||
import { getLocaleById } from "./locales.ts";
|
import { getLocaleById, Locale } from "./locales.ts";
|
||||||
|
|
||||||
Deno.test("Get locale by id", () => {
|
Deno.test("Get locale by id", () => {
|
||||||
const id = 25;
|
const id = 25;
|
||||||
const actual = getLocaleById(id);
|
const actual = getLocaleById(id);
|
||||||
const expected = "Salut le Monde!";
|
const expected: Locale = {
|
||||||
|
locale: "French",
|
||||||
|
message: "Salut le Monde!",
|
||||||
|
};
|
||||||
|
|
||||||
assertStrictEquals(actual, expected, `Expected ${expected}; got ${actual}`);
|
assertEquals(actual, expected, `Expected ${expected}; got ${actual}`);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,17 @@
|
||||||
import { locales } from "./locales.data.ts";
|
import { locales } from "./locales.data.ts";
|
||||||
|
|
||||||
|
export type Locale = {
|
||||||
|
locale: string;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
|
||||||
export const localesSize = Object.keys(locales).length;
|
export const localesSize = Object.keys(locales).length;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a given locale identified by its id
|
* Return a given locale identified by its id
|
||||||
* @param id unique identifier
|
* @param id unique identifier
|
||||||
*/
|
*/
|
||||||
export const getLocaleById = (id: number) => Object.values(locales)[id - 1];
|
export const getLocaleById = (id: number): Locale => ({
|
||||||
|
locale: Object.keys(locales)[id - 1],
|
||||||
|
message: Object.values(locales)[id - 1],
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { assertStrictEquals } from "../../deps.ts";
|
import { assertEquals, assertStrictEquals } from "../../deps.ts";
|
||||||
import { ID } from "../types/id.ts";
|
import { ID } from "../types/id.ts";
|
||||||
import { sayHello, sayRandomHello } from "./hello.ts";
|
import { HelloResponse, sayHello, sayRandomHello } from "./hello.ts";
|
||||||
|
|
||||||
Deno.test("Random Hello", () => {
|
Deno.test("Random Hello", () => {
|
||||||
const result = sayRandomHello();
|
const result = sayRandomHello();
|
||||||
|
|
@ -10,17 +10,23 @@ Deno.test("Random Hello", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("Specific Hello", () => {
|
Deno.test("Specific Hello", () => {
|
||||||
type TestCase = { in: ID; want: string };
|
type TestCase = { in: ID; want: HelloResponse };
|
||||||
|
|
||||||
const testCases: TestCase[] = [
|
const testCases: TestCase[] = [
|
||||||
{ in: new ID(25), want: "Salut le Monde!" },
|
{
|
||||||
{ in: new ID(21), want: "Hello World!" },
|
in: new ID(25),
|
||||||
|
want: { id: 25, locale: "French", message: "Salut le Monde!" },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: new ID(21),
|
||||||
|
want: { id: 21, locale: "English", message: "Hello World!" },
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const test of testCases) {
|
for (const test of testCases) {
|
||||||
const actual = sayHello(test.in);
|
const actual = sayHello(test.in);
|
||||||
const expected = test.want;
|
const expected = test.want;
|
||||||
|
|
||||||
assertStrictEquals(actual, expected);
|
assertEquals(actual, expected);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
import { getLocaleById, localesSize } from "../repositories/locales.ts";
|
import { getLocaleById, Locale, localesSize } from "../repositories/locales.ts";
|
||||||
import { ID } from "../types/id.ts";
|
import { ID } from "../types/id.ts";
|
||||||
|
|
||||||
type HelloResponse = {
|
export type HelloResponse = Locale & { id: number };
|
||||||
id: number;
|
|
||||||
locale: string;
|
|
||||||
message: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the iconic Hello, World in a random locale
|
* Display the iconic Hello, World in a random locale
|
||||||
|
|
@ -20,10 +16,8 @@ export const sayRandomHello = () => {
|
||||||
* Display the iconic Hello, World in a locale identified by id
|
* Display the iconic Hello, World in a locale identified by id
|
||||||
* @param id Unique identifier
|
* @param id Unique identifier
|
||||||
*/
|
*/
|
||||||
export const sayHello = (id: ID): HelloResponse => {
|
export const sayHello = (id: ID): HelloResponse => ({
|
||||||
return {
|
id: id.value,
|
||||||
id: id.value,
|
locale: getLocaleById(id.value).locale,
|
||||||
locale: "",
|
message: getLocaleById(id.value).message,
|
||||||
message: getLocaleById(id.value),
|
});
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue