polish source API

This commit is contained in:
Ruidy 2021-02-19 15:58:13 +01:00
parent 914ab13e74
commit 328eef7876
3 changed files with 35 additions and 31 deletions

View file

@ -6,4 +6,5 @@ Web app which returns the iconic Hello, World in various locales
- [x] Print hello world in a random locale
- [x] Select the locale by id
- [x] Use with CLI
- [ ] Serve via internet

33
cli.ts Normal file
View file

@ -0,0 +1,33 @@
import { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
import { ID, sayHello, sayRandomHello, ValidationError } from "./src";
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)) {
const value = line.trim();
if (value === "") {
console.log(sayRandomHello());
} else if (value === "quit") {
return;
} else {
try {
const id = new ID(parseInt(value, 10));
console.log(sayHello(id));
} catch (error) {
if (error instanceof ValidationError) {
console.error(error.message);
} else {
throw error;
}
}
}
}
};
await start();

View file

@ -1,35 +1,5 @@
import { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
import { ValidationError } from "./errors/exceptions.ts";
import { ID } from "./types/id.ts";
import { sayHello, sayRandomHello } from "./usecases/hello.ts";
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)) {
const value = line.trim();
if (value === "") {
console.log(sayRandomHello());
} else if (value === "quit") {
return;
} else {
try {
const id = new ID(parseInt(value, 10));
console.log(sayHello(id));
} catch (error) {
if (error instanceof ValidationError) {
console.error(error.message);
} else {
throw error;
}
}
}
}
};
await start();
export { ID, sayHello, sayRandomHello, ValidationError };