Skip to content

Commit

Permalink
Rebase and requested fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
matteozambon89 committed Dec 1, 2021
1 parent 1cf4e18 commit 8d328c0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const colored = {
greyMessage: gray
}

function resolveCustomColored (customColors) {
function resolveCustomColoredColorizer (customColors) {
return customColors.reduce(
function (agg, [level, color]) {
agg[level] = typeof availableColors[color] === 'function' ? availableColors[color] : white
Expand Down Expand Up @@ -71,7 +71,7 @@ coloredColorizer.message = colored.message
coloredColorizer.greyMessage = colored.greyMessage

function customColoredColorizerFactory (customColors) {
const customColored = resolveCustomColored(customColors)
const customColored = resolveCustomColoredColorizer(customColors)

const customColoredColorizer = function (level, opts) {
return colorizeLevel(level, customColored, opts)
Expand All @@ -88,6 +88,7 @@ function customColoredColorizerFactory (customColors) {
*
* @param {boolean} [useColors=false] When `true` a function that applies standard
* terminal colors is returned.
* @param {array[]} [customColors] Touple where first item of each array is the level index and the second item is the color
*
* @returns {function} `function (level) {}` has a `.message(str)` method to
* apply colorization to a string. The core function accepts either an integer
Expand Down
9 changes: 7 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ function prettifyErrorLog ({
* @param {object} input.log The log object.
* @param {function} [input.colorizer] A colorizer function that accepts a level
* value and returns a colorized string. Default: a no-op colorizer.
* @param {string} [levelKey='level'] The key to find the level under.
* @param {string} [input.levelKey='level'] The key to find the level under.
* @param {function} [input.prettifier] A user-supplied formatter to be called instead of colorizer.
* @param {object} [input.customLevels] The custom levels where key as the level index and value as the level name.
* @param {object} [input.customLevelNames] The custom level names where key is the level name and value is the level index.
*
* @returns {undefined|string} If `log` does not have a `level` property then
* `undefined` will be returned. Otherwise, a string from the specified
Expand All @@ -242,12 +244,15 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE
* @param {function} [input.colorizer] A colorizer function that has a
* `.message(str)` method attached to it. This function should return a colorized
* string which will be the "prettified" message. Default: a no-op colorizer.
* @param {string} [input.levelLabel='levelLabel'] The label used to output the log level
* @param {string} [input.levelKey='level'] The key to find the level under.
* @param {object} [input.customLevels] The custom levels where key as the level index and value as the level name.
*
* @returns {undefined|string} If the message key is not found, or the message
* key is not a string, then `undefined` will be returned. Otherwise, a string
* that is the prettified message.
*/
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL, customLevels, levelKey = LEVEL_KEY }) {
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL, levelKey = LEVEL_KEY, customLevels }) {
if (messageFormat && typeof messageFormat === 'string') {
const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) {
// return log level as string instead of int
Expand Down
4 changes: 2 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,10 @@ test('basic prettifier tests', (t) => {
t.plan(7)
const expectedLines = [
' msg: {',
' "a": "[Circular]",',
' "b": {',
' "c": "d"',
' }',
' },',
' "a": "[Circular ~]"',
' }'
]
const pretty = prettyFactory()
Expand Down
4 changes: 2 additions & 2 deletions test/lib/colors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const testColoringColorizer = getColorizer => async t => {
t.equal(colorized, '\u001B[90mfoo\u001B[39m')
}

const testCustomColoringColorizer = getColorizer = async t => {
const testCustomColoringColorizer = getColorizer => async t => {
const customLevels = {
0: 'INFO',
1: 'ERR',
Expand Down Expand Up @@ -120,4 +120,4 @@ test('returns default colorizer - public export', testDefaultColorizer(getColori
test('returns colorizing colorizer - private export', testColoringColorizer(getColorizerPrivate))
test('returns colorizing colorizer - public export', testColoringColorizer(getColorizerPublic))
test('returns custom colorizing colorizer - private export', testCustomColoringColorizer(getColorizerPrivate))
test('returns custom colorizing colorizer - public export', testCustomColoringColorizer(getColorizerPublic))
test('returns custom colorizing colorizer - public export', testCustomColoringColorizer(getColorizerPublic))

0 comments on commit 8d328c0

Please sign in to comment.