Skip to content

Commit

Permalink
add: resolveLevelOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsunee committed Nov 24, 2024
1 parent c587e22 commit 58d189e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/resolveLevelOpts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { resolveTemplateOpts } from "./resolveTemplateOpts";
import type {
LevelOpts,
ResolvedLevelOpts,
ResolvedTemplateOpts
} from "./types";

export function resolveLevelOpts<Name extends string = string>(
level: LevelOpts<Name> | Name,
defaultTemplate: ResolvedTemplateOpts
) {
const levelObj: LevelOpts<Name> =
typeof level == "object" ? level : { name: level };
const template: ResolvedTemplateOpts = levelObj.template
? resolveTemplateOpts(levelObj.template, defaultTemplate)
: defaultTemplate;

const resolvedOpts: ResolvedLevelOpts<Name> = {
name: levelObj.name,
template,
color: levelObj.color,
colorMode: levelObj.colorMode || "name",
type: levelObj.type || "log"
};

return resolvedOpts;
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface LevelOpts<Name extends string> {
* Optional override for the template used when logging using this log level.
* Note that defaults are taken from the default template of the logger!
*/
template?: TemplateOpts;
template?: TemplateOpts | string;
/**
* Color Middleware that can be used to color or transform parts or the full
* prefix (see also `colorMode`)
Expand Down
62 changes: 62 additions & 0 deletions tests/resolveLevelOpts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { test } from "uvu";
import * as assert from "uvu/assert";
import { resolveLevelOpts } from "../src/resolveLevelOpts";
import type { ResolvedTemplateOpts } from "../src/types";

const template: ResolvedTemplateOpts = {
template: "- %#name%:",
hours: 24,
utc: true,
padLen: 20
};

test("it resolves level opts from string arg", () => {
const opts = resolveLevelOpts("Test", template);
assert.equal(opts, {
name: "Test",
template,
color: undefined,
colorMode: "name",
type: "log"
});
});

test("it resolves level opts from incomplete object arg", () => {
const opts = resolveLevelOpts(
{
name: "Warning",
type: "warn"
},
template
);
assert.equal(opts, {
name: "Warning",
template,
color: undefined,
colorMode: "name",
type: "warn"
});
});

test("it resolves template property to resolved template", () => {
const mockColFn = (str: string) => str;
const opts = resolveLevelOpts(
{
name: "Error",
template: "⛔ %#NAME%:",
type: "error",
color: mockColFn,
colorMode: "full"
},
template
);
assert.equal(opts, {
name: "Error",
template: { ...template, template: "⛔ %#NAME%:" },
color: mockColFn,
colorMode: "full",
type: "error"
});
});

test.run();

0 comments on commit 58d189e

Please sign in to comment.