{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "normalize-persian-text",
  "title": "Normalize Persian Text",
  "description": "Unifies Arabic-codepage letters onto Persian ones (ي→ی, ك→ک, …) with opt-in digit folding, diacritic stripping, half-space repair, and space tidying.",
  "registryDependencies": [
    "@persianlabsui/normalize-persian-digits"
  ],
  "files": [
    {
      "path": "registry/base/lib/normalize-persian-text.ts",
      "content": "import { normalizePersianDigits } from \"@/lib/normalize-persian-digits\"\n\n/** Arabic-codepage look-alikes mapped onto their canonical Persian letters. */\nconst LETTER_MAP: Record<string, string> = {\n  \"\\u064A\": \"\\u06CC\", // ي arabic yeh → ی farsi yeh\n  \"\\u0649\": \"\\u06CC\", // ى alef maksura → ی\n  \"\\u0643\": \"\\u06A9\", // ك arabic kaf → ک keheh\n  \"\\u0671\": \"\\u0627\", // ٱ alef wasla → ا\n  \"\\u0623\": \"\\u0627\", // أ alef hamza above → ا\n  \"\\u0625\": \"\\u0627\", // إ alef hamza below → ا\n  \"\\u0629\": \"\\u0647\", // ة teh marbuta → ه\n}\n\nconst LETTER_PATTERN = /[يىكٱأإة]/g\n\n/** Harakat, Quranic superscripts, and the kashida elongation character. */\n// eslint-disable-next-line no-misleading-character-class -- combining marks are stripped individually by design\nconst DIACRITIC_PATTERN = /[\\u064B-\\u0655\\u0670\\u0640\\u06D6-\\u06ED]/g\n\nconst ZWNJ = \"\\u200C\"\n\nexport interface NormalizePersianTextOptions {\n  /** Fold Persian (۰-۹) and Arabic-Indic (٠-٩) digits to plain 0-9.\n   * @default false */\n  digits?: boolean\n  /** Strip Arabic diacritics (harakat), superscript alef, and kashida.\n   * @default false */\n  diacritics?: boolean\n  /** Repair broken half-spaces: collapse consecutive ZWNJs and drop stray\n   * ones sitting next to spaces, punctuation, or the string edges.\n   * @default true */\n  zwnj?: boolean\n  /** Collapse runs of blanks to a single space and trim the ends. Line\n   * breaks are preserved. @default true */\n  spaces?: boolean\n}\n\nfunction repairZwnj(value: string): string {\n  // Whitespace plus common ASCII/Persian punctuation.\n  const edge = \"[\\\\s.,;:!?\\u00AB\\u00BB()'\\u2013\\u2014\\\"\\\\-\\u061F\\u060C\\u061B]\"\n  return value\n    .replace(new RegExp(`${ZWNJ}{2,}`, \"g\"), ZWNJ)\n    .replace(new RegExp(`${ZWNJ}(?=${edge}|$)`, \"g\"), \"\")\n    .replace(new RegExp(`(${edge})${ZWNJ}`, \"g\"), \"$1\")\n}\n\nfunction collapseSpaces(value: string): string {\n  return value.replace(/[\\t \\u00A0\\u2000-\\u200A]{2,}/g, \" \").trim()\n}\n\n/**\n * Normalizes Persian text for storage, comparison, and display.\n *\n * Arabic codepage look-alikes are always unified onto their Persian letters\n * (ي→ی, ك→ک, ة→ه, …), so text pasted from Arabic keyboards or legacy fonts\n * becomes consistent. Everything else is opt-in: digit folding, diacritic\n * stripping, half-space (ZWNJ) repair, and whitespace tidying — all safely\n * skippable so the function doubles as a lossless display cleaner or an\n * aggressive search-key builder.\n */\nexport function normalizePersianText(\n  value: string,\n  options?: NormalizePersianTextOptions\n): string {\n  let out = value.replace(LETTER_PATTERN, (char) => LETTER_MAP[char] ?? char)\n\n  if (options?.diacritics) {\n    out = out.replace(DIACRITIC_PATTERN, \"\")\n  }\n\n  if (options?.digits) {\n    out = normalizePersianDigits(out)\n  }\n\n  if (options?.zwnj !== false) {\n    out = repairZwnj(out)\n  }\n\n  if (options?.spaces !== false) {\n    out = collapseSpaces(out)\n  }\n\n  return out\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}