UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

19 lines (18 loc) • 625 B
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; }