Skip to content

Conversation

@lucasrijllart
Copy link
Contributor

The badge function provides an easy way to get the length of a variable (usually an array), and include an optional text prefix, such as media: 4 when counting number of media objects. It needs to handle empty and undefined values as well without erroring

@lucasrijllart lucasrijllart linked an issue Mar 27, 2025 that may be closed by this pull request
@lucasrijllart lucasrijllart requested a review from sww314 March 27, 2025 16:41
Copy link
Collaborator

@sww314 sww314 left a comment

Choose a reason for hiding this comment

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

We want to return empty string if there data is not right.
This follows the logic in most "badge" components.

src/functions.js Outdated
Comment on lines 20 to 21
const length = array?.length || 0;
return `${prefix}${length}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

if the length is 0 or undefined return nothing.

Also handle type error (swallow the error) if they pass in an object with out a length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sww314 I forgot you mentioned it should be empty rather than 0. Changed in aa58c18

  1. If an object without a length is passed in, the ?.length already returns undefined and then the || '' will return empty. Do I need to add more handling? I thought this would handle every undefined case
  2. For BADGE("A String"), given this is a length calculation, it makes sense to me that it returns the length of the string
  3. For BADGE({"a": "1", "b": "2"}) the result is empty string. The object has no length so it defaulted to empty

Copy link
Collaborator

@sww314 sww314 left a comment

Choose a reason for hiding this comment

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

Do not show anything with length == 0 (or if object is undefined).

If we want, we could add a showZero prop... but I am not sure it is needed.
https://mui.com/material-ui/react-badge/

Also if you pass in:

BADGE("A String")

You will get an answer. Maybe that is fine.

What happens if you pass in:

BADGE({"a": "1", "b": "2"})

Copy link
Collaborator

@sww314 sww314 left a comment

Choose a reason for hiding this comment

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

for completeness should we add postfix?

BADGE(values, "[", "]") --> [5]

src/functions.js Outdated
Comment on lines 20 to 21
const length = array?.length || '';
return `${prefix}${length}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const length = array?.length || '';
return `${prefix}${length}`;
const length = array?.length || '';
if length:
return `${prefix}${length}`;
return '';

make it easy to return nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added postfix modified logic in in ab2c955

This does modify the behavior tho: in my previous code it would still show the pre/postfix with an empty value, whereas now it doesn't. Seems fine either way but probably more correct to display nothing

@sww314 sww314 changed the title feat: new badge function and tests feat: new BADGE function Mar 27, 2025
@sww314 sww314 merged commit 59abb96 into main Mar 27, 2025
3 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.

Custom function BADGE

3 participants