mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +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 { ID } from "./types/id.ts";
|
||||||
|
import { sayHello, sayRandomHello } from "./usecases/hello.ts";
|
||||||
|
|
||||||
/**
|
const start = async () => {
|
||||||
* Display the iconic Hello, World in a random locale
|
console.log("Hello, World! (International version)\n");
|
||||||
*/
|
console.log(
|
||||||
export const sayRandomHello = () => {
|
"🌍 Type a number (between 1 and 78), or leave blank for random language. Type 'quit' to leave.",
|
||||||
const id = Math.ceil(Math.random() * localesSize);
|
);
|
||||||
const val = new ID(id);
|
console.log("👉 ");
|
||||||
return sayHello(val);
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
await start();
|
||||||
* 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);
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { assertThrows } from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
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 { ID } from "./id.ts";
|
||||||
import { ValidationError } from "../errors/exceptions.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";
|
import { ValidationError } from "../errors/exceptions.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
||||||
import { sayHello, sayRandomHello } from "./index.ts";
|
import { sayHello, sayRandomHello } from "./hello.ts";
|
||||||
import { ID } from "./types/id.ts";
|
import { ID } from "../types/id.ts";
|
||||||
|
|
||||||
Deno.test("Random Hello", () => {
|
Deno.test("Random Hello", () => {
|
||||||
const result = sayRandomHello();
|
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