Skip to content

Commit

Permalink
Add levelToNum & numToLevel utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ymonb1291 committed Oct 14, 2020
1 parent 9ff2a44 commit 7afb64f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
6 changes: 3 additions & 3 deletions configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Formatter } from "./formatter.ts";
import type { Internals } from "./internals.interface.ts";
import type { Papyrus } from "./papyrus.ts";
import type { PapyrusOptions } from "./papyrus_options.interface.ts";
import type { KeyValuePair } from "./utils.ts";
import { KeyValuePair, levelToNum, numToLevel } from "./utils.ts";
import type { TransportOptions } from "./transport.ts";

export class Configuration {
Expand Down Expand Up @@ -43,10 +43,10 @@ export class Configuration {

/** Returns a valid level as number */
private validateLevel(level?: Level | keyof typeof Level): Level {
if(level && typeof level !== "number") {
if(level && typeof level !== "number" && levelToNum(level)) {
return Level[level];
} else if(
typeof level === "number" && level >= 0 && level <= Object.keys(Level).length/2-1
typeof level === "number" && numToLevel(level)
) {
return level;
}
Expand Down
4 changes: 2 additions & 2 deletions logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Configuration } from "./configuration.ts";
import { LOG_VERSION } from "./constants.ts";
import { PapyrusConsole } from "./console.transport.ts";
import { Level } from "./level.enum.ts";
import { filterKeys } from "./utils.ts";
import { filterKeys, numToLevel } from "./utils.ts";

import type { Formatter } from "./formatter.ts";
import type {
Expand Down Expand Up @@ -57,7 +57,7 @@ export abstract class Logger {
private buildBaseLog(level: Level): BaseLog {
// Initiate a BaseLog with the non-optional properties
const baseLog: BaseLog = {
level: this.configuration.useLabels ? Level[level] : level,
level: this.configuration.useLabels ? numToLevel(level) : level,
};

// Add name to BaseLog
Expand Down
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Papyrus } from "./papyrus.ts";
export default Papyrus;

// Named exports
export { PapyrusConsole } from "./console.transport.ts";
export { Level } from "./level.enum.ts";
export { Papyrus } from "./papyrus.ts";
export { PapyrusConsole } from "./console.transport.ts";
export { levelToNum, numToLevel } from "./utils.ts";

// Type exports
export type { ChildOptions } from "./child_options.interface.ts";
Expand Down
15 changes: 15 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Level } from "./level.enum.ts";

/** Generic interface for plain objects */
export interface KeyValuePair {
[key: string]: unknown;
Expand Down Expand Up @@ -34,4 +36,17 @@ export function filterKeys(
}
});
return res;
}

/** Converts a level to a number */
export function levelToNum(level: keyof typeof Level): Level | undefined {
return Level[level];
}

/** Converts numbers to levels */
export function numToLevel(level: Level): keyof typeof Level;
export function numToLevel(level: number): undefined;
export function numToLevel(level: Level | number): keyof typeof Level | undefined {
if(level < 0 || level > Object.keys(Level).length/2-1) return;
return Level[level] as keyof typeof Level;
}
42 changes: 41 additions & 1 deletion utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Rhum } from "./deps_test.ts";
import { filterKeys } from "./utils.ts";
import { Level } from "./level.enum.ts";
import { filterKeys, levelToNum, numToLevel } from "./utils.ts";

Rhum.testPlan("utils_test.ts", () => {

Expand All @@ -21,6 +22,45 @@ Rhum.testPlan("utils_test.ts", () => {

});

Rhum.testSuite("levelToNum()", () => {

const testCases: [string, number | void][] = [
["trace", 0],
["debug", 1],
["info", 2],
["warn", 3],
["error", 4],
["xyz", void 0],
];

testCases.forEach(testCase => {
Rhum.testCase(`Using '${testCase[0]}' as argument returns ${testCase[1]}`, () => {
Rhum.asserts.assertEquals(levelToNum(testCase[0] as keyof typeof Level), testCase[1]);
});
});

});

Rhum.testSuite("numToLevel()", () => {

const testCases: [number, keyof typeof Level | void][] = [
[-1, void 0],
[0, "trace"],
[1, "debug"],
[2, "info"],
[3, "warn"],
[4, "error"],
[5, void 0],
];

testCases.forEach(testCase => {
Rhum.testCase(`Using '${testCase[0]}' as argument returns ${testCase[1]}`, () => {
Rhum.asserts.assertEquals(numToLevel(testCase[0]), testCase[1]);
});
});

});

});

Rhum.run();

0 comments on commit 7afb64f

Please sign in to comment.