Revert "web/util: fix oxfordHumanJoin(React)? for two-element arrays"

This reverts commit 3288d86e29.
This commit is contained in:
Tulir Asokan 2024-12-10 23:24:12 +02:00
parent ac3438ad25
commit 8ee34be466
3 changed files with 9 additions and 23 deletions

View file

@ -18,7 +18,7 @@ import { PulseLoader } from "react-spinners"
import { getAvatarURL } from "@/api/media.ts" import { getAvatarURL } from "@/api/media.ts"
import { useRoomTyping } from "@/api/statestore" import { useRoomTyping } from "@/api/statestore"
import { MemberEventContent } from "@/api/types/mxtypes.ts" import { MemberEventContent } from "@/api/types/mxtypes.ts"
import { oxfordHumanJoinReact } from "@/util/reactjoin.tsx" import { humanJoinReact } from "@/util/reactjoin.tsx"
import ClientContext from "../ClientContext.ts" import ClientContext from "../ClientContext.ts"
import { useRoomContext } from "../roomview/roomcontext.ts" import { useRoomContext } from "../roomview/roomcontext.ts"
import "./TypingNotifications.css" import "./TypingNotifications.css"
@ -55,7 +55,7 @@ const TypingNotifications = () => {
description = <div>{typing.length} users are typing</div> description = <div>{typing.length} users are typing</div>
} else if (typing.length > 0) { } else if (typing.length > 0) {
description = <div> description = <div>
{oxfordHumanJoinReact(memberNames)} {humanJoinReact(memberNames)}
{typing.length === 1 ? " is " : " are "} {typing.length === 1 ? " is " : " are "}
typing typing
</div> </div>

View file

@ -13,12 +13,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
export function humanJoin( export function humanJoin(arr: string[], sep: string = ", ", lastSep: string = " and "): string {
arr: string[],
sep: string = ", ",
sep2: string = " and ",
lastSep: string = " and ",
): string {
if (arr.length === 0) { if (arr.length === 0) {
return "" return ""
} }
@ -26,9 +21,7 @@ export function humanJoin(
return arr[0] return arr[0]
} }
if (arr.length === 2) { if (arr.length === 2) {
return arr.join(sep2) return arr.join(lastSep)
} }
return arr.slice(0, -1).join(sep) + lastSep + arr[arr.length - 1] return arr.slice(0, -1).join(sep) + lastSep + arr[arr.length - 1]
} }
export const oxfordHumanJoin = (arr: string[]) => humanJoin(arr, ", ", " and ", ", and ")

View file

@ -18,20 +18,13 @@ import { Fragment, JSX } from "react"
export function humanJoinReact( export function humanJoinReact(
arr: (string | JSX.Element)[], arr: (string | JSX.Element)[],
sep: string | JSX.Element = ", ", sep: string | JSX.Element = ", ",
sep2: string | JSX.Element = " and ",
lastSep: string | JSX.Element = " and ", lastSep: string | JSX.Element = " and ",
): JSX.Element[] { ): JSX.Element[] {
return arr.map((elem, idx) => { return arr.map((elem, idx) =>
let separator = sep <Fragment key={idx}>
if (idx === arr.length - 2) {
separator = (arr.length === 2) ? sep2 : lastSep
}
return <Fragment key={idx}>
{elem} {elem}
{idx < arr.length - 1 ? separator : null} {idx < arr.length - 1 ? (idx === arr.length - 2 ? lastSep : sep) : null}
</Fragment> </Fragment>)
})
} }
export const oxfordHumanJoinReact = (arr: (string | JSX.Element)[]) => humanJoinReact(arr, ", ", " and ", ", and ") export const joinReact = (arr: (string | JSX.Element)[]) => humanJoinReact(arr, " ", " ")
export const joinReact = (arr: (string | JSX.Element)[]) => humanJoinReact(arr, " ", " ", " ")