@sema-agent/core
Version:
Stateless, task-oriented AI agent core
26 lines (25 loc) • 841 B
JavaScript
export const isHighSurrogate = (c) => c >= 0xd800 && c <= 0xdbff;
export const isLowSurrogate = (c) => c >= 0xdc00 && c <= 0xdfff;
export function sliceHeadSafe(s, keep) {
if (keep <= 0)
return "";
if (s.length <= keep)
return s;
const head = s.slice(0, keep);
return keep > 0 && isHighSurrogate(head.charCodeAt(keep - 1)) ? head.slice(0, -1) : head;
}
export function sliceTailSafe(s, keep) {
if (keep <= 0)
return "";
if (s.length <= keep)
return s;
const tail = s.slice(-keep);
return keep > 0 && isLowSurrogate(tail.charCodeAt(0)) ? tail.slice(1) : tail;
}
export function codePointLength(s) {
let n = s.length;
for (let i = 1; i < s.length; i++)
if (isLowSurrogate(s.charCodeAt(i)) && isHighSurrogate(s.charCodeAt(i - 1)))
n--;
return n;
}