generated from foxkit-js/library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |