Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: d981de1229ef899957bbe968bc8dcda02a21f477
26 lines
1.1 KiB
TypeScript
26 lines
1.1 KiB
TypeScript
/**
|
|
* Removes unpaired Unicode surrogate characters from a string.
|
|
*
|
|
* Unpaired surrogates (high surrogates 0xD800-0xDBFF without matching low surrogates 0xDC00-0xDFFF,
|
|
* or vice versa) cause JSON serialization errors in many API providers.
|
|
*
|
|
* Valid emoji and other characters outside the Basic Multilingual Plane use properly paired
|
|
* surrogates and will NOT be affected by this function.
|
|
*
|
|
* @param text - The text to sanitize
|
|
* @returns The sanitized text with unpaired surrogates removed
|
|
*
|
|
* @example
|
|
* // Valid emoji (properly paired surrogates) are preserved
|
|
* sanitizeSurrogates("Hello 🙈 World") // => "Hello 🙈 World"
|
|
*
|
|
* // Unpaired high surrogate is removed
|
|
* const unpaired = String.fromCharCode(0xD83D); // high surrogate without low
|
|
* sanitizeSurrogates(`Text ${unpaired} here`) // => "Text here"
|
|
*/
|
|
export function sanitizeSurrogates(text: string): string {
|
|
// Replace unpaired high surrogates (0xD800-0xDBFF not followed by low surrogate)
|
|
// Replace unpaired low surrogates (0xDC00-0xDFFF not preceded by high surrogate)
|
|
return text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "");
|
|
}
|