Skip to content

Commit

Permalink
Refactor the mixing logic (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Feb 15, 2022
1 parent dbde8b7 commit 3053a99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,23 @@ function write (_obj, msg, num) {
let obj

if (_obj === undefined || _obj === null) {
obj = mixin ? mixin({}) : {}
obj = {}
} else if (_obj instanceof Error) {
obj = Object.assign(mixin ? mixin({}) : {}, { err: _obj })
obj = { err: _obj }
if (msg === undefined) {
msg = _obj.message
}
} else {
obj = Object.assign(mixin ? mixin({}) : {}, _obj)
obj = _obj
if (msg === undefined && _obj.err) {
msg = _obj.err.message
}
}

if (mixin) {
obj = Object.assign(mixin(obj), obj)
}

const s = this[asJsonSym](obj, msg, num, t)

const stream = this[streamSym]
Expand Down
9 changes: 7 additions & 2 deletions test/mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ test('mixin not a function', async ({ throws }) => {
})
})

test('mixin can use context', async ({ ok }) => {
test('mixin can use context', async ({ ok, same }) => {
const stream = sink()
const instance = pino({
mixin (context) {
ok(context !== null, 'context should be defined')
ok(context !== undefined, 'context should be defined')
same(context, {
message: '123',
stack: 'stack'
})
return Object.assign({
error: context.message,
stack: context.stack
Expand All @@ -121,12 +125,13 @@ test('mixin can use context', async ({ ok }) => {
}, 'test')
})

test('mixin works without context', async ({ ok }) => {
test('mixin works without context', async ({ ok, same }) => {
const stream = sink()
const instance = pino({
mixin (context) {
ok(context !== null, 'context is still defined w/o passing mergeObject')
ok(context !== undefined, 'context is still defined w/o passing mergeObject')
same(context, {})
return {
something: true
}
Expand Down

0 comments on commit 3053a99

Please sign in to comment.