diff --git a/src/main/ts/utils/deepmap.ts b/src/main/ts/utils/deepmap.ts index 71c6c2c..1871a1f 100644 --- a/src/main/ts/utils/deepmap.ts +++ b/src/main/ts/utils/deepmap.ts @@ -20,9 +20,15 @@ export function deepMap( return } + const value = descriptor.get ? descriptor.get() : descriptor.value + // descriptor cannot contain both accessor method and value + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#description + delete descriptor.get + delete descriptor.set + Object.defineProperty(n, key, { ...descriptor, - value: deepMap(descriptor.value, fn, refs, key), + value: deepMap(value, fn, refs, key), }) }) diff --git a/src/test/ts/utils/deepmap.ts b/src/test/ts/utils/deepmap.ts index fd99b4d..42d5a6e 100644 --- a/src/test/ts/utils/deepmap.ts +++ b/src/test/ts/utils/deepmap.ts @@ -49,4 +49,20 @@ test('deepMap handle error objects', () => { ) }) +test('deepMap handles accessor property descriptor', 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()