Skip to content

Clean empty arrays and objects#29

Merged
ruscoder merged 11 commits intomainfrom
clean-empty-arrays-and-objects
Jun 30, 2025
Merged

Clean empty arrays and objects#29
ruscoder merged 11 commits intomainfrom
clean-empty-arrays-and-objects

Conversation

@dmitryashutov
Copy link
Member

According to FHIR spec a resource can not contain undefined, null or empty array values:

  • remove key/value pair if the value is empty array
  • remove key/value pair if the value is undefined

@dmitryashutov dmitryashutov requested a review from ruscoder June 30, 2025 09:16
@dmitryashutov dmitryashutov self-assigned this Jun 30, 2025
});

if (isPlainObject(objResult)) {
const cleanedObject = Object.entries(objResult).filter(([, value]) => value !== undefined);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add filterValues() function similar to mapValues for better readability

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function filterValues(obj: object, fn: (value: any, key: string) => any) {
    return Object.fromEntries(
        Object.entries(obj).filter(([key, value]) => {
            return [key, fn(value, key)];
        }),
    );
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}```

@ruscoder ruscoder merged commit a3f725d into main Jun 30, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants