Skip to content

Commit

Permalink
fix: handle both of property descriptor types in masker (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
oljekechoro authored Sep 29, 2023
1 parent 3a308db commit d104f46
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/ts/utils/deepmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ export function deepMap(
return
}

// descriptor cannot contain both accessor method and value
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#description
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { get, set, ...rest } = descriptor
const value = get ? get() : descriptor.value

Object.defineProperty(n, key, {
...descriptor,
value: deepMap(descriptor.value, fn, refs, key),
...rest,
value: deepMap(value, fn, refs, key),
})
})

Expand Down
16 changes: 16 additions & 0 deletions src/test/ts/utils/deepmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ test('deepMap handle error objects', () => {
)
})

test('deepMap handles accessor and data property descriptors', async () => {
const objectWithDataPropertyDescriptor = {}
Object.defineProperty(objectWithDataPropertyDescriptor, 'bar', { value: 42 })
assert.equal(
deepMap(objectWithDataPropertyDescriptor, (el) => el + 'foo').bar,
'42foo',
)

const objectWithAccessorPropertyDescriptor = {}
Object.defineProperty(objectWithAccessorPropertyDescriptor, 'bar', { get: () => 42 })
assert.equal(
deepMap(objectWithAccessorPropertyDescriptor, (el) => el + 'foo').bar,
'42foo',
)
})

test.run()

0 comments on commit d104f46

Please sign in to comment.