Skip to content

Commit

Permalink
Fixing format fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Sep 29, 2023
1 parent d263ac2 commit df618ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,15 @@ describe('compactObject', () => {
expect(compactObject({ kibana: { alert: { rule: { execution: {} } }, rule: {} } })).toEqual({});
expect(
compactObject({
kibana: { rule: 34, alert: { rule: { execution: {}, nested_field: ['a', 'b'] } } },
kibana: {
rule: 34,
testField: [],
alert: { rule: { execution: {}, nested_field: ['a', 'b'] } },
},
})
).toEqual({ kibana: { rule: 34, alert: { rule: { nested_field: ['a', 'b'] } } } });
).toEqual({
kibana: { rule: 34, testField: [], alert: { rule: { nested_field: ['a', 'b'] } } },
});
});
expect(compactObject({ 'kibana.alert.rule.execution': {} })).toEqual({});
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ type Obj = Record<string, unknown>;
export const compactObject = (obj: Obj) => {
return Object.keys(obj)
.filter((key: string) => {
// just filter out empty objects
// keep any primitives or arrays, even empty arrays
return (
!!obj[key] &&
((typeof obj[key] === 'object' && !isEmpty(obj[key])) || typeof obj[key] !== 'object')
(Array.isArray(obj[key]) ||
typeof obj[key] !== 'object' ||
(typeof obj[key] === 'object' && !isEmpty(obj[key])))
);
})
.reduce<Obj>((acc, curr) => {
Expand Down

0 comments on commit df618ff

Please sign in to comment.