Skip to content

Commit

Permalink
feat(data): support parent matching for arrays (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
kribor authored Oct 10, 2023
1 parent b1c543c commit f0ed0ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/scrubber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ test('Support scrubbing based on parent', () => {
})
})

test('Support scrubbing array based on parent', () => {
const data = { nested: { encryption: [{ key: 'secret' }] } }
const scrubber = new Scrubber(configParentScrubbersMock())
const result = scrubber.scrub(data)

expect(result).toEqual({
nested: { encryption: [{ key: 'replaced' }] },
})
})

test('Support scrubbing based on multi-level parent', () => {
const data = { multi: { api: { secret: 'notsosecret' }, interim: { secret: '123456' } } }
const scrubber = new Scrubber(configParentScrubbersMock())
Expand Down
4 changes: 3 additions & 1 deletion src/scrubber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class Scrubber {
}

private applyScrubbers<T>(data: T, parents: string[] = []): T {
const isArray = Array.isArray(data)
const dataCopy: any = Array.isArray(data) ? [...data] : { ...data }

Object.keys(dataCopy).forEach(key => {
Expand Down Expand Up @@ -102,7 +103,8 @@ export class Scrubber {

// Deep traverse
if (typeof dataCopy[key] === 'object' && dataCopy[key]) {
const parentsNext = [...parents, key]
// Don't append array keys to parent array as it breaks parent matching
const parentsNext = isArray ? parents : [...parents, key]
dataCopy[key] = this.applyScrubbers(dataCopy[key], parentsNext)
}

Expand Down

0 comments on commit f0ed0ce

Please sign in to comment.