mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
polish source API
This commit is contained in:
parent
914ab13e74
commit
328eef7876
3 changed files with 35 additions and 31 deletions
|
|
@ -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
33
cli.ts
Normal 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();
|
||||
32
src/index.ts
32
src/index.ts
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Reference in a new issue