fix: handle array edge case

This commit is contained in:
yassinedorbozgithub 2025-05-06 14:57:58 +01:00
parent 0b9f6e49e1
commit f5dec41b34
2 changed files with 8 additions and 0 deletions

View File

@ -111,4 +111,8 @@ describe('flatten', () => {
expect(result).toStrictEqual({});
});
it('should throw an error if data is an array', () => {
expect(() => flatten([])).toThrow('Data should be an object!');
});
});

View File

@ -18,6 +18,10 @@ export const flatten = (
prefix: string | undefined = undefined,
result: object = {},
): object => {
if (Array.isArray(data)) {
throw new Error('Data should be an object!');
}
for (const [key, value] of Object.entries(data)) {
const path = prefix ? `${prefix}.${key}` : key;