HomePhabricator
Diffusion Comm 1d97b312f296

[lib] Remove extraneous ternary from `pluralize` function in `text-utils`

Tags
None
Referenced Files
F81015: image.png
Jun 22 2022, 10:00 AM
File Not Attached
F81011: image.png
Jun 22 2022, 10:00 AM
File Not Attached
F81009: image.png
Jun 22 2022, 10:00 AM
File Not Attached
F81019: image.png
Jun 22 2022, 10:00 AM
File Not Attached
F81002: image.png
Jun 22 2022, 10:00 AM
File Not Attached
F81001: image.png
Jun 22 2022, 10:00 AM
File Not Attached
Subscribers

Description

[lib] Remove extraneous ternary from pluralize function in text-utils

Summary: The ternary condition in the pluralize function in text-utils is unnecessary since its "else" condition is unreachable. Because of the order of the cases in pluralize, if the maxNumberOfNouns === 1 conditional is entered, nouns.length will always be greater than 1, since the prior two conditionals check if nouns.length equals 0 or 1. Therefore, it can be removed.

Test Plan:
The length property of an array can never be a negative number, and only a positive integer greater than or equal to 0. So nouns.length is guaranteed to be greater than 1 if the maxNumberOfNouns === 1 conditional is reached.

In addition, after @palys-swm's review, I checked all instances of lib/text/text-utils (specifically calls to pluralize and pluralizeAndTrim) in the codebase to make sure this change did not break anything:

  • Test to make sure InlineSidebars are displayed correctly:

image.png (184×928 px, 22 KB)
image.png (118×372 px, 11 KB)

Also, to be doubly sure, this is the usage of pluralizeAndTrim in InlineSidebarText, which calls pluralizeAndTrim with maxNumberOfNouns = 25. The code that was changed in this diff was in the conditional, which is only executed if maxNumberOfNouns === 1, so this change will not affect InlineSidebar.
image.png (360×1 px, 104 KB)

  • Test to make sure JoinThreadMessageSpec works correctly:

This is the only call to pluralize/usage of text-utils in JoinThreadMessageSpec:

image.png (112×1 px, 35 KB)

Because this call to pluralize does not define the maxNumberOfNouns parameter, the code that was changed will never be executed since maxNumberOfNouns is by default initialized to 3 (see line 5 in this diff), not 1. The code that was changed in this diff was in the conditional, which is only executed if maxNumberOfNouns === 1, so this change will not affect JoinThreadMessageSpec.

  • Test to make sure thread-utils works correctly:

This is the only call to pluralize in thread-utils:

image.png (496×1 px, 130 KB)

Once again, because this call to pluralize does not define the maxNumberOfNouns parameter, the code that was changed will never be executed since maxNumberOfNouns is by default initialized to 3, not 1. Therefore, this change will not affect thread-utils (or anything that uses it).

  • Test to make sure LeaveThreadMessageSpec works correctly:

Same logic as described above for JoinThreadMessageSpec:

image.png (134×1 px, 38 KB)

Reviewers: atul, palys-swm

Reviewed By: palys-swm

Subscribers: ashoat, Adrian

Differential Revision: https://phab.comm.dev/D4317

Details

Provenance
aboshAuthored on Jun 21 2022, 12:16 PM
Reviewer
tomek
Differential Revision
D4317: [lib] Remove extraneous ternary from `pluralize` function in `text-utils`
Parents
rCOMM87b2cca30c62: [web] Merge store and server search results
Branches
Unknown
Tags
Unknown