1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00

fix: omit yes no from stale data comparison (#7052)

This commit is contained in:
Mateusz Kwasniewski 2024-05-14 12:42:39 +02:00 committed by GitHub
parent 5b8c63f748
commit 668cb81384
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 5 deletions

View File

@ -6,5 +6,5 @@ export const comparisonModerator = (
): DeepOmit<IFeatureToggle, keyof IFeatureToggle> => {
const tempData = { ...data };
return deepOmit(tempData, 'lastSeenAt');
return deepOmit(tempData, 'lastSeenAt', 'yes', 'no');
};

View File

@ -47,3 +47,33 @@ test('should omit all instances of a given field', () => {
},
});
});
test('should omit all instances of multiple fields', () => {
const input = {
name: 'some-name',
color: 'blue',
children: {
name: 'some-name',
color: 'blue',
children: {
name: 'some-name',
color: 'blue',
children: {
name: 'some-name',
color: 'blue',
children: {},
},
},
},
};
expect(deepOmit(input, 'name', 'color')).toEqual({
children: {
children: {
children: {
children: {},
},
},
},
});
});

View File

@ -4,17 +4,22 @@ export type DeepOmit<T, K extends keyof any> = T extends Record<string, any>
export function deepOmit<T, K extends keyof any>(
obj: T,
keyToOmit: K,
...keysToOmit: K[]
): DeepOmit<T, K> {
const omitSet = new Set(keysToOmit);
if (Array.isArray(obj)) {
return obj.map((item) =>
deepOmit(item, keyToOmit),
deepOmit(item, ...keysToOmit),
) as unknown as DeepOmit<T, K>;
} else if (typeof obj === 'object' && obj !== null) {
const result: Partial<DeepOmit<T, K>> = {};
for (const [key, value] of Object.entries(obj)) {
if (key !== keyToOmit) {
result[key as Exclude<keyof T, K>] = deepOmit(value, keyToOmit);
if (!omitSet.has(key as K)) {
result[key as Exclude<keyof T, K>] = deepOmit(
value,
...keysToOmit,
);
}
}
return result as DeepOmit<T, K>;