Skip to content

Commit

Permalink
isLogLevel() function
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 21, 2024
1 parent 3f2d6a2 commit 743a76b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Version 0.3.0
To be released.

- Added `parseLogLevel()` function.
- Added `isLogLevel()` function.


Version 0.2.2
Expand Down
33 changes: 33 additions & 0 deletions logtape/level.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { assert } from "@std/assert/assert";
import { assertEquals } from "@std/assert/assert-equals";
import { assertFalse } from "@std/assert/assert-false";
import { assertThrows } from "@std/assert/assert-throws";
import { isLogLevel, parseLogLevel } from "./level.ts";

Deno.test("parseLogLevel()", () => {
assertEquals(parseLogLevel("debug"), "debug");
assertEquals(parseLogLevel("info"), "info");
assertEquals(parseLogLevel("warning"), "warning");
assertEquals(parseLogLevel("error"), "error");
assertEquals(parseLogLevel("fatal"), "fatal");
assertEquals(parseLogLevel("DEBUG"), "debug");
assertEquals(parseLogLevel("INFO"), "info");
assertEquals(parseLogLevel("WARNING"), "warning");
assertEquals(parseLogLevel("ERROR"), "error");
assertEquals(parseLogLevel("FATAL"), "fatal");
assertThrows(
() => parseLogLevel("invalid"),
TypeError,
"Invalid log level: invalid.",
);
});

Deno.test("isLogLevel()", () => {
assert(isLogLevel("debug"));
assert(isLogLevel("info"));
assert(isLogLevel("warning"));
assert(isLogLevel("error"));
assert(isLogLevel("fatal"));
assertFalse(isLogLevel("DEBUG"));
assertFalse(isLogLevel("invalid"));
});
20 changes: 20 additions & 0 deletions logtape/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ export function parseLogLevel(level: string): LogLevel {
throw new TypeError(`Invalid log level: ${level}.`);
}
}

/**
* Checks if a string is a valid log level. This function can be used as
* as a type guard to narrow the type of a string to a {@link LogLevel}.
*
* @param level The log level as a string. This is case-sensitive.
* @returns `true` if the string is a valid log level.
*/
export function isLogLevel(level: string): level is LogLevel {
switch (level) {
case "debug":
case "info":
case "warning":
case "error":
case "fatal":
return true;
default:
return false;
}
}
2 changes: 1 addition & 1 deletion logtape/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export {
defaultTextFormatter,
type TextFormatter,
} from "./formatter.ts";
export { type LogLevel, parseLogLevel } from "./level.ts";
export { isLogLevel, type LogLevel, parseLogLevel } from "./level.ts";
export { getLogger, type Logger } from "./logger.ts";
export type { LogRecord } from "./record.ts";
export {
Expand Down

0 comments on commit 743a76b

Please sign in to comment.