6a3848ecb5
Co-authored-by: Cursor <cursoragent@cursor.com>
11 lines
310 B
JavaScript
11 lines
310 B
JavaScript
export function getByteLength(str) {
|
|
return new TextEncoder().encode(str).length
|
|
}
|
|
|
|
export function truncateToMaxBytes(str, maxBytes) {
|
|
if (getByteLength(str) <= maxBytes) return str
|
|
let end = str.length
|
|
while (end > 0 && getByteLength(str.slice(0, end)) > maxBytes) end--
|
|
return str.slice(0, end)
|
|
}
|