Skip to content

Commit

Permalink
fix(core): Handle Date and RegExp objects in AugmentObject (#5809)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Mar 30, 2023
1 parent 753cfb8 commit e6d4e72
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
53 changes: 27 additions & 26 deletions packages/workflow/src/AugmentObject.ts
@@ -1,6 +1,24 @@
import type { IDataObject } from './Interfaces';
import util from 'util';

function augment<T>(value: T): T {
if (
typeof value !== 'object' ||
value === null ||
util.types.isProxy(value) ||
value instanceof RegExp
)
return value;

if (value instanceof Date) return new Date(value.valueOf()) as T;

// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (Array.isArray(value)) return augmentArray(value) as T;

// eslint-disable-next-line @typescript-eslint/no-use-before-define
return augmentObject(value) as T;
}

export function augmentArray<T>(data: T[]): T[] {
let newData: unknown[] | undefined = undefined;

Expand All @@ -17,24 +35,12 @@ export function augmentArray<T>(data: T[]): T[] {
},
get(target, key: string, receiver): unknown {
const value = Reflect.get(newData !== undefined ? newData : target, key, receiver) as unknown;

if (typeof value === 'object') {
if (value === null || util.types.isProxy(value)) {
return value;
}

const newValue = augment(value);
if (newValue !== value) {
newData = getData();

if (Array.isArray(value)) {
Reflect.set(newData, key, augmentArray(value));
} else {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
Reflect.set(newData, key, augmentObject(value as IDataObject));
}

return Reflect.get(newData, key);
Reflect.set(newData, key, newValue);
return newValue;
}

return value;
},
getOwnPropertyDescriptor(target, key) {
Expand Down Expand Up @@ -83,18 +89,13 @@ export function augmentObject<T extends object>(data: T): T {

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const value = Reflect.get(target, key, receiver);

if (value !== null && typeof value === 'object') {
if (Array.isArray(value)) {
newData[key] = augmentArray(value);
} else {
newData[key] = augmentObject(value as IDataObject);
}

return newData[key];
const newValue = augment(value);
if (newValue !== value) {
Object.assign(newData, { [key]: newValue });
return newValue;
}

return value as string;
return value;
},
deleteProperty(target, key: string) {
if (key in newData) {
Expand Down
8 changes: 7 additions & 1 deletion packages/workflow/test/AugmentObject.test.ts
Expand Up @@ -193,11 +193,15 @@ describe('AugmentObject', () => {

describe('augmentObject', () => {
test('should work with simple values on first level', () => {
const date = new Date(1680089084200);
const regexp = new RegExp('^test$', 'ig');
const originalObject: IDataObject = {
1: 11,
2: '22',
a: 111,
b: '222',
d: date,
r: regexp,
};
const copyOriginal = JSON.parse(JSON.stringify(originalObject));

Expand All @@ -221,14 +225,16 @@ describe('AugmentObject', () => {

augmentedObject.c = 3;

expect(originalObject).toEqual(copyOriginal);
expect({ ...originalObject, d: date.toJSON(), r: {} }).toEqual(copyOriginal);

expect(augmentedObject).toEqual({
1: 911,
2: '922',
a: 9111,
b: '9222',
c: 3,
d: date,
r: regexp,
});
});

Expand Down

0 comments on commit e6d4e72

Please sign in to comment.