@logtape/logtape 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/level.test.ts DELETED
@@ -1,71 +0,0 @@
1
- import { suite } from "@alinea/suite";
2
- import { assert } from "@std/assert/assert";
3
- import { assertEquals } from "@std/assert/equals";
4
- import { assertFalse } from "@std/assert/false";
5
- import { assertThrows } from "@std/assert/throws";
6
- import {
7
- compareLogLevel,
8
- getLogLevels,
9
- isLogLevel,
10
- type LogLevel,
11
- parseLogLevel,
12
- } from "./level.ts";
13
-
14
- const test = suite(import.meta);
15
-
16
- test("getLogLevels()", () => {
17
- const levels: readonly LogLevel[] = getLogLevels();
18
- assertEquals(levels, [
19
- "trace",
20
- "debug",
21
- "info",
22
- "warning",
23
- "error",
24
- "fatal",
25
- ]);
26
- const levels2 = levels as LogLevel[];
27
- levels2.push("trace");
28
- const levels3 = getLogLevels();
29
- assertEquals(levels3, [
30
- "trace",
31
- "debug",
32
- "info",
33
- "warning",
34
- "error",
35
- "fatal",
36
- ]);
37
- });
38
-
39
- test("parseLogLevel()", () => {
40
- assertEquals(parseLogLevel("debug"), "debug");
41
- assertEquals(parseLogLevel("info"), "info");
42
- assertEquals(parseLogLevel("warning"), "warning");
43
- assertEquals(parseLogLevel("error"), "error");
44
- assertEquals(parseLogLevel("fatal"), "fatal");
45
- assertEquals(parseLogLevel("DEBUG"), "debug");
46
- assertEquals(parseLogLevel("INFO"), "info");
47
- assertEquals(parseLogLevel("WARNING"), "warning");
48
- assertEquals(parseLogLevel("ERROR"), "error");
49
- assertEquals(parseLogLevel("FATAL"), "fatal");
50
- assertThrows(
51
- () => parseLogLevel("invalid"),
52
- TypeError,
53
- "Invalid log level: invalid.",
54
- );
55
- });
56
-
57
- test("isLogLevel()", () => {
58
- assert(isLogLevel("debug"));
59
- assert(isLogLevel("info"));
60
- assert(isLogLevel("warning"));
61
- assert(isLogLevel("error"));
62
- assert(isLogLevel("fatal"));
63
- assertFalse(isLogLevel("DEBUG"));
64
- assertFalse(isLogLevel("invalid"));
65
- });
66
-
67
- test("compareLogLevel()", () => {
68
- const levels: LogLevel[] = ["info", "debug", "error", "warning", "fatal"];
69
- levels.sort(compareLogLevel);
70
- assertEquals(levels, ["debug", "info", "warning", "error", "fatal"]);
71
- });
package/src/level.ts DELETED
@@ -1,86 +0,0 @@
1
- const logLevels = [
2
- "trace",
3
- "debug",
4
- "info",
5
- "warning",
6
- "error",
7
- "fatal",
8
- ] as const;
9
-
10
- /**
11
- * The severity level of a {@link LogRecord}.
12
- */
13
- export type LogLevel = typeof logLevels[number];
14
-
15
- /**
16
- * Lists all available log levels with the order of their severity.
17
- * The `"trace"` level goes first, and the `"fatal"` level goes last.
18
- * @returns A new copy of the array of log levels.
19
- * @since 1.0.0
20
- */
21
- export function getLogLevels(): readonly LogLevel[] {
22
- return [...logLevels];
23
- }
24
-
25
- /**
26
- * Parses a log level from a string.
27
- *
28
- * @param level The log level as a string. This is case-insensitive.
29
- * @returns The log level.
30
- * @throws {TypeError} If the log level is invalid.
31
- */
32
- export function parseLogLevel(level: string): LogLevel {
33
- level = level.toLowerCase();
34
- switch (level) {
35
- case "trace":
36
- case "debug":
37
- case "info":
38
- case "warning":
39
- case "error":
40
- case "fatal":
41
- return level;
42
- default:
43
- throw new TypeError(`Invalid log level: ${level}.`);
44
- }
45
- }
46
-
47
- /**
48
- * Checks if a string is a valid log level. This function can be used as
49
- * as a type guard to narrow the type of a string to a {@link LogLevel}.
50
- *
51
- * @param level The log level as a string. This is case-sensitive.
52
- * @returns `true` if the string is a valid log level.
53
- */
54
- export function isLogLevel(level: string): level is LogLevel {
55
- switch (level) {
56
- case "trace":
57
- case "debug":
58
- case "info":
59
- case "warning":
60
- case "error":
61
- case "fatal":
62
- return true;
63
- default:
64
- return false;
65
- }
66
- }
67
-
68
- /**
69
- * Compares two log levels.
70
- * @param a The first log level.
71
- * @param b The second log level.
72
- * @returns A negative number if `a` is less than `b`, a positive number if `a`
73
- * is greater than `b`, or zero if they are equal.
74
- * @since 0.8.0
75
- */
76
- export function compareLogLevel(a: LogLevel, b: LogLevel): number {
77
- const aIndex = logLevels.indexOf(a);
78
- if (aIndex < 0) {
79
- throw new TypeError(`Invalid log level: ${JSON.stringify(a)}.`);
80
- }
81
- const bIndex = logLevels.indexOf(b);
82
- if (bIndex < 0) {
83
- throw new TypeError(`Invalid log level: ${JSON.stringify(b)}.`);
84
- }
85
- return aIndex - bIndex;
86
- }