mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
basic cli
This commit is contained in:
parent
40fd25bfaf
commit
25e8826d3c
8 changed files with 46 additions and 19 deletions
38
src/index.ts
38
src/index.ts
|
|
@ -1,19 +1,27 @@
|
|||
import { getLocaleById, localesSize } from "./repository/locales.ts";
|
||||
import { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
|
||||
import { ID } from "./types/id.ts";
|
||||
import { sayHello, sayRandomHello } from "./usecases/hello.ts";
|
||||
|
||||
/**
|
||||
* Display the iconic Hello, World in a random locale
|
||||
*/
|
||||
export const sayRandomHello = () => {
|
||||
const id = Math.ceil(Math.random() * localesSize);
|
||||
const val = new ID(id);
|
||||
return sayHello(val);
|
||||
const start = async () => {
|
||||
console.log("Hello, World! (International version)\n");
|
||||
console.log(
|
||||
"🌍 Type a number (between 1 and 78), or leave blank for random language. Type 'quit' to leave.",
|
||||
);
|
||||
console.log("👉 ");
|
||||
|
||||
for await (const line of readLines(Deno.stdin)) {
|
||||
switch (line.trim()) {
|
||||
case "": {
|
||||
console.log(sayRandomHello());
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const id = new ID(parseInt(line.trim(), 10));
|
||||
console.log(sayHello(id));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Display the iconic Hello, World in a locale identified by id
|
||||
* @param id Unique identifier
|
||||
*/
|
||||
export const sayHello = (id: ID): string => {
|
||||
return getLocaleById(id.value);
|
||||
};
|
||||
await start();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { assertThrows } from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
||||
import { localesSize } from "../repository/locales.ts";
|
||||
import { localesSize } from "../repositories/locales.ts";
|
||||
import { ID } from "./id.ts";
|
||||
import { ValidationError } from "../errors/exceptions.ts";
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { localesSize } from "../repository/locales.ts";
|
||||
import { localesSize } from "../repositories/locales.ts";
|
||||
import { ValidationError } from "../errors/exceptions.ts";
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
||||
import { sayHello, sayRandomHello } from "./index.ts";
|
||||
import { ID } from "./types/id.ts";
|
||||
import { sayHello, sayRandomHello } from "./hello.ts";
|
||||
import { ID } from "../types/id.ts";
|
||||
|
||||
Deno.test("Random Hello", () => {
|
||||
const result = sayRandomHello();
|
||||
19
src/usecases/hello.ts
Normal file
19
src/usecases/hello.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { getLocaleById, localesSize } from "../repositories/locales.ts";
|
||||
import { ID } from "../types/id.ts";
|
||||
|
||||
/**
|
||||
* Display the iconic Hello, World in a random locale
|
||||
*/
|
||||
export const sayRandomHello = () => {
|
||||
const id = Math.ceil(Math.random() * localesSize);
|
||||
const val = new ID(id);
|
||||
return sayHello(val);
|
||||
};
|
||||
|
||||
/**
|
||||
* Display the iconic Hello, World in a locale identified by id
|
||||
* @param id Unique identifier
|
||||
*/
|
||||
export const sayHello = (id: ID): string => {
|
||||
return getLocaleById(id.value);
|
||||
};
|
||||
Loading…
Reference in a new issue