2022-05-26 21:41:24 +00:00
|
|
|
import { ReactNode } from 'react'
|
|
|
|
|
2022-01-18 03:36:33 +00:00
|
|
|
export const JoinSpans = (props: {
|
2022-05-26 22:22:44 +00:00
|
|
|
children: ReactNode[]
|
2022-05-26 21:41:24 +00:00
|
|
|
separator?: ReactNode
|
2022-01-18 03:36:33 +00:00
|
|
|
}) => {
|
|
|
|
const { separator } = props
|
|
|
|
const children = props.children.filter((x) => !!x)
|
|
|
|
|
|
|
|
if (children.length === 0) return <></>
|
2022-05-26 22:22:44 +00:00
|
|
|
if (children.length === 1) return <>{children[0]}</>
|
2022-01-18 03:36:33 +00:00
|
|
|
if (children.length === 2)
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{children[0]} and {children[1]}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
const head = children.slice(0, -1).map((child) => (
|
|
|
|
<>
|
|
|
|
{child}
|
|
|
|
{separator || ','}{' '}
|
|
|
|
</>
|
|
|
|
))
|
|
|
|
|
|
|
|
const tail = children[children.length - 1]
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{head}and {tail}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|