Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@makerx/node-winston",
"version": "2.0.0",
"version": "2.0.1",
"private": false,
"description": "A set of winston formats, console transport and logger creation functions",
"author": "MakerX",
Expand Down
37 changes: 37 additions & 0 deletions src/omit-nil-format.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TransformableInfo } from 'logform'
import { LEVEL, MESSAGE } from 'triple-beam'
import { describe, expect, it } from 'vitest'
import { omitNilFormat } from './omit-nil-format'

const runOmitNil = (info: Record<string | symbol, unknown>) => {
const transform = omitNilFormat()
const input = { [LEVEL]: 'info', [MESSAGE]: 'msg', level: 'info', message: 'msg', ...info } as TransformableInfo
return transform.transform(input, {}) as Record<string | symbol, unknown>
}

describe('omitNilFormat', () => {
it('removes null and undefined values', () => {
const result = runOmitNil({ a: 1, b: null, c: undefined, d: 'x' })

expect(result).toMatchObject({ a: 1, d: 'x' })
expect(result).not.toHaveProperty('b')
expect(result).not.toHaveProperty('c')
})

it('keeps falsy-but-not-nil values', () => {
const result = runOmitNil({ zero: 0, empty: '', no: false })

expect(result).toMatchObject({ zero: 0, empty: '', no: false })
})

it('preserves the LEVEL and MESSAGE symbols (regression: pg error length splat)', () => {
// pg errors carry a numeric `length` property; older es-toolkit/compat
// omitBy treated this object as array-like and stripped triple-beam symbols,
// breaking downstream colorize.
const result = runOmitNil({ length: 4, name: 'error', code: '23505' })

expect(result[LEVEL]).toBe('info')
expect(result[MESSAGE]).toBe('msg')
expect(result).toMatchObject({ length: 4, name: 'error', code: '23505' })
})
})
16 changes: 14 additions & 2 deletions src/omit-nil-format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { isNil, omitBy } from 'es-toolkit/compat'
import { TransformableInfo } from 'logform'
import { format } from 'winston'

export const omitNilFormat = format((info) => omitBy(info, isNil) as TransformableInfo)
// Hand-rolled rather than using es-toolkit/compat's omitBy: that treats any
// object with a numeric `length` property (e.g. a pg error spread via splat)
// as array-like, returns {}, and drops winston's LEVEL/MESSAGE symbols —
// breaking downstream formats like colorize. See toss/es-toolkit#1706.
export const omitNilFormat = format((info) => {
const out: Record<string | symbol, unknown> = {}
for (const key of Object.keys(info)) {
if (info[key] != null) out[key] = info[key]
}
for (const sym of Object.getOwnPropertySymbols(info)) {
if (info[sym] != null) out[sym] = info[sym]
}
return out as TransformableInfo
})