mirror of
https://github.com/Unleash/unleash.git
synced 2025-11-24 20:06:55 +01:00
https://linear.app/unleash/issue/2-2029/support-filtering-on-nested-properties Suggests nested properties in action filters. Also sorts them alphabetically. Follow up to https://github.com/Unleash/unleash/pull/6531 <img width="381" alt="image" src="https://github.com/Unleash/unleash/assets/14320932/4e2c900d-335b-4360-8be4-186f3887e42b">
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
export const flattenPayload = (
|
|
payload: Record<string, unknown> = {},
|
|
parentKey = '',
|
|
): Record<string, unknown> =>
|
|
Object.entries(payload).reduce(
|
|
(acc, [key, value]) => {
|
|
const newKey = parentKey ? `${parentKey}.${key}` : key;
|
|
|
|
if (
|
|
typeof value === 'object' &&
|
|
value !== null &&
|
|
!Array.isArray(value)
|
|
) {
|
|
// If it's an object, recurse and merge the results
|
|
Object.assign(
|
|
acc,
|
|
flattenPayload(value as Record<string, unknown>, newKey),
|
|
);
|
|
} else if (Array.isArray(value)) {
|
|
// If it's an array, map through it and handle objects and non-objects differently
|
|
value.forEach((item, index) => {
|
|
if (typeof item === 'object' && item !== null) {
|
|
Object.assign(
|
|
acc,
|
|
flattenPayload(item, `${newKey}[${index}]`),
|
|
);
|
|
} else {
|
|
acc[`${newKey}[${index}]`] = item;
|
|
}
|
|
});
|
|
} else {
|
|
acc[newKey] = value;
|
|
}
|
|
|
|
return acc;
|
|
},
|
|
{} as Record<string, unknown>,
|
|
);
|