39 lines
917 B
TypeScript
39 lines
917 B
TypeScript
import type { ReactNode } from "react";
|
|
|
|
const ARABIC_SCRIPT = /[\u0600-\u06FF]/;
|
|
|
|
/** Wrap Persian/Arabic runs in IRANYekan; leave Latin as-is. */
|
|
export function withFaSpans(value: string): ReactNode {
|
|
if (!value || !ARABIC_SCRIPT.test(value)) return value;
|
|
|
|
const parts = value.split(/([\u0600-\u06FF][\u0600-\u06FF\s\u200c]*)/g);
|
|
return parts.map((part, index) =>
|
|
ARABIC_SCRIPT.test(part) ? (
|
|
<span key={index} className="fa" lang="fa" dir="rtl">
|
|
{part}
|
|
</span>
|
|
) : (
|
|
part
|
|
),
|
|
);
|
|
}
|
|
|
|
export function hasArabicScript(value: string | null | undefined) {
|
|
return Boolean(value && ARABIC_SCRIPT.test(value));
|
|
}
|
|
|
|
/** Keep phone / E.164 numbers LTR even next to Persian text. */
|
|
export function PhoneLtr({
|
|
value,
|
|
className,
|
|
}: {
|
|
value: string;
|
|
className?: string;
|
|
}) {
|
|
return (
|
|
<span className={className} dir="ltr" lang="en">
|
|
{value}
|
|
</span>
|
|
);
|
|
}
|