mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 13:27:31 -08:00
fix(core): augmentObject
should use existing property descriptors whenever possible (#5872)
* fix(core): Augmented objects should use existing property descriptors whenever possible * add a test for non-enumerable keys
This commit is contained in:
parent
31cd04c476
commit
6a1b7c306b
|
@ -1,5 +1,7 @@
|
|||
import type { IDataObject } from './Interfaces';
|
||||
|
||||
const defaultPropertyDescriptor = Object.freeze({ enumerable: true, configurable: true });
|
||||
|
||||
const augmentedObjects = new WeakSet<object>();
|
||||
|
||||
function augment<T>(value: T): T {
|
||||
|
@ -47,7 +49,8 @@ export function augmentArray<T>(data: T[]): T[] {
|
|||
if (key === 'length') {
|
||||
return Reflect.getOwnPropertyDescriptor(newData, key);
|
||||
}
|
||||
return { configurable: true, enumerable: true };
|
||||
|
||||
return Object.getOwnPropertyDescriptor(data, key) ?? defaultPropertyDescriptor;
|
||||
},
|
||||
has(target, key) {
|
||||
return Reflect.has(newData !== undefined ? newData : target, key);
|
||||
|
@ -124,18 +127,17 @@ export function augmentObject<T extends object>(data: T): T {
|
|||
|
||||
return true;
|
||||
},
|
||||
|
||||
ownKeys(target) {
|
||||
return [...new Set([...Reflect.ownKeys(target), ...Object.keys(newData)])].filter(
|
||||
const originalKeys = Reflect.ownKeys(target);
|
||||
const newKeys = Object.keys(newData);
|
||||
return [...new Set([...originalKeys, ...newKeys])].filter(
|
||||
(key) => deletedProperties.indexOf(key) === -1,
|
||||
);
|
||||
},
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
getOwnPropertyDescriptor(k) {
|
||||
return {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
};
|
||||
getOwnPropertyDescriptor(target, key) {
|
||||
return Object.getOwnPropertyDescriptor(data, key) ?? defaultPropertyDescriptor;
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -520,5 +520,13 @@ describe('AugmentObject', () => {
|
|||
|
||||
expect(timeAugmented).toBeLessThan(timeCopied);
|
||||
});
|
||||
|
||||
test('should ignore non-enumerable keys', () => {
|
||||
const originalObject = { a: 1, b: 2 };
|
||||
Object.defineProperty(originalObject, '__hiddenProp', { enumerable: false });
|
||||
|
||||
const augmentedObject = augmentObject(originalObject);
|
||||
expect(Object.keys(augmentedObject)).toEqual(['a', 'b']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue