Skip to content

Commit

Permalink
Fix cloning errors (#286)
Browse files Browse the repository at this point in the history
* Fix cloning objects with errors

* Add tests for cloning errors

* Remove rfd as it isn't used anymore

Co-authored-by: Gergő Lengyel <gergo@eventtrips.com>
  • Loading branch information
Allain55 and Allain55 authored Jun 1, 2022
1 parent 16b15a3 commit 53b2ab4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const clone = require('rfdc')({ circles: true })
const fastCopy = require('fast-copy')
const dateformat = require('dateformat')
const SonicBoom = require('sonic-boom')
const stringifySafe = require('fast-safe-stringify')
Expand Down Expand Up @@ -573,12 +573,12 @@ function deleteLogProperty (log, property) {
* Filter a log object by removing any ignored keys.
*
* @param {object} log The log object to be modified.
* @param {string} ignoreKeys An array of strings identifying the properties to be removed.
* @param {Set<string> | Array<string>} ignoreKeys An array of strings identifying the properties to be removed.
*
* @returns {object} A new `log` object instance that does not include the ignored keys.
*/
function filterLog (log, ignoreKeys) {
const logCopy = clone(log)
const logCopy = fastCopy(log)
ignoreKeys.forEach((ignoreKey) => {
deleteLogProperty(logCopy, ignoreKey)
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
"args": "5.0.1",
"colorette": "^2.0.7",
"dateformat": "^4.6.3",
"fast-copy": "^2.1.1",
"fast-safe-stringify": "^2.0.7",
"joycon": "^3.1.1",
"on-exit-leak-free": "^0.2.0",
"pino-abstract-transport": "^0.5.0",
"pump": "^3.0.0",
"readable-stream": "^3.6.0",
"rfdc": "^1.3.0",
"secure-json-parse": "^2.4.0",
"sonic-boom": "^2.2.0",
"strip-json-comments": "^3.1.1"
Expand Down
8 changes: 4 additions & 4 deletions test/lib/utils.internals.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const tap = require('tap')
const clone = require('rfdc')()
const fastCopy = require('fast-copy')
const stringifySafe = require('fast-safe-stringify')
const { internals } = require('../../lib/utils')

Expand Down Expand Up @@ -140,19 +140,19 @@ tap.test('#deleteLogProperty', t => {
}

t.test('deleteLogProperty deletes property of depth 1', async t => {
const log = clone(logData)
const log = fastCopy(logData)
internals.deleteLogProperty(log, 'data1')
t.same(log, { level: 30 })
})

t.test('deleteLogProperty deletes property of depth 2', async t => {
const log = clone(logData)
const log = fastCopy(logData)
internals.deleteLogProperty(log, 'data1.data2')
t.same(log, { level: 30, data1: { } })
})

t.test('deleteLogProperty deletes property of depth 3', async t => {
const log = clone(logData)
const log = fastCopy(logData)
internals.deleteLogProperty(log, 'data1.data2.data-3')
t.same(log, { level: 30, data1: { data2: { } } })
})
Expand Down
10 changes: 8 additions & 2 deletions test/lib/utils.public.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,26 @@ tap.test('#filterLog', t => {
level: 30,
time: 1522431328992,
data1: {
data2: { 'data-3': 'bar' }
data2: { 'data-3': 'bar' },
error: new Error('test')
}
}

t.test('filterLog removes single entry', async t => {
const result = filterLog(logData, ['data1.data2.data-3'])
t.same(result, { level: 30, time: 1522431328992, data1: { data2: { } } })
t.same(result, { level: 30, time: 1522431328992, data1: { data2: { }, error: new Error('test') } })
})

t.test('filterLog removes multiple entries', async t => {
const result = filterLog(logData, ['time', 'data1'])
t.same(result, { level: 30 })
})

t.test('filterLog keeps error instance', async t => {
const result = filterLog(logData, [])
t.equals(logData.data1.error, result.data1.error)
})

const logData2 = Object.assign({
'logging.domain.corp/operation': {
id: 'foo',
Expand Down

0 comments on commit 53b2ab4

Please sign in to comment.