feat: update deno-commons-mod.ts
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
// Reference:
|
// Reference:
|
||||||
// - https://docs.deno.com/runtime/fundamentals/testing/
|
// - https://docs.deno.com/runtime/fundamentals/testing/
|
||||||
|
|
||||||
import {assertEquals} from "jsr:@std/assert";
|
import { assertEquals } from "jsr:@std/assert";
|
||||||
|
|
||||||
export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 {
|
export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 {
|
||||||
if (ver1 === ver2) return 0;
|
if (ver1 === ver2) return 0;
|
||||||
@@ -136,6 +136,100 @@ export async function printLastLine(line: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Term {
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
blink(message: string): string {
|
||||||
|
return `\x1b[5m${message}\x1b[0m`;
|
||||||
|
}
|
||||||
|
|
||||||
|
bold(message: string): string {
|
||||||
|
return `\x1b[1m${message}\x1b[0m`;
|
||||||
|
}
|
||||||
|
|
||||||
|
red(message: string): string {
|
||||||
|
return `\x1b[31m${message}\x1b[0m`;
|
||||||
|
}
|
||||||
|
|
||||||
|
green(message: string): string {
|
||||||
|
return `\x1b[32m${message}\x1b[0m`;
|
||||||
|
}
|
||||||
|
|
||||||
|
yellow(message: string): string {
|
||||||
|
return `\x1b[33m${message}\x1b[0m`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const term = new Term();
|
||||||
|
|
||||||
|
function pad(message: string, length: number): string {
|
||||||
|
if (message.length >= length) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
return message + " ".repeat(length - message.length);
|
||||||
|
}
|
||||||
|
const LOGGER_PREFIX_LEN: number = 8;
|
||||||
|
class Logger {
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
success(...data: any[]) {
|
||||||
|
this.log(
|
||||||
|
term.bold(term.green(`[${pad("SUCCESS", LOGGER_PREFIX_LEN)}]`)),
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
error(...data: any[]) {
|
||||||
|
this.log(
|
||||||
|
term.bold(term.red(`[${pad("ERROR", LOGGER_PREFIX_LEN)}]`)),
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
warn(...data: any[]) {
|
||||||
|
this.log(
|
||||||
|
term.bold(term.yellow(`[${pad("WARN", LOGGER_PREFIX_LEN)}]`)),
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
warning(...data: any[]) {
|
||||||
|
this.log(
|
||||||
|
term.blink(
|
||||||
|
term.bold(term.yellow(`[${pad("WARN", LOGGER_PREFIX_LEN)}]`)),
|
||||||
|
),
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
info(...data: any[]) {
|
||||||
|
this.log(term.bold(`[${pad("INFO", LOGGER_PREFIX_LEN)}]`), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
debug(...data: any[]) {
|
||||||
|
this.log(`[${pad("DEBUG", LOGGER_PREFIX_LEN)}]`, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
log(prefix: string, data: any[]) {
|
||||||
|
const args = [prefix];
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
args.push(data[i]);
|
||||||
|
}
|
||||||
|
console.log.apply(console, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const log = new Logger();
|
||||||
|
|
||||||
Deno.test("isOn", () => {
|
Deno.test("isOn", () => {
|
||||||
assertEquals(false, isOn(undefined));
|
assertEquals(false, isOn(undefined));
|
||||||
assertEquals(false, isOn(""));
|
assertEquals(false, isOn(""));
|
||||||
|
|||||||
Reference in New Issue
Block a user