Buckets:
| const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" }) | |
| export function promptOffsetWidth(value: string) { | |
| let width = 0 | |
| for (const part of graphemes.segment(value)) { | |
| // Textarea offsets count newlines as one position; Bun.stringWidth counts them as zero. | |
| width += part.segment === "\n" ? 1 : Bun.stringWidth(part.segment) | |
| } | |
| return width | |
| } | |
| function displayOffsetIndex(value: string, offset: number) { | |
| if (offset <= 0) return 0 | |
| let width = 0 | |
| for (const part of graphemes.segment(value)) { | |
| const next = width + promptOffsetWidth(part.segment) | |
| if (next > offset) return part.index | |
| width = next | |
| } | |
| return value.length | |
| } | |
| export function displaySlice(value: string, start = 0, end = promptOffsetWidth(value)) { | |
| return value.slice(displayOffsetIndex(value, start), displayOffsetIndex(value, end)) | |
| } | |
| export function displayCharAt(value: string, offset: number) { | |
| let width = 0 | |
| for (const part of graphemes.segment(value)) { | |
| const next = width + promptOffsetWidth(part.segment) | |
| if (offset === width || offset < next) return part.segment | |
| width = next | |
| } | |
| } | |
| export function mentionTriggerIndex(value: string, offset = promptOffsetWidth(value)) { | |
| const text = displaySlice(value, 0, offset) | |
| const index = text.lastIndexOf("@") | |
| if (index === -1) return | |
| const before = index === 0 ? undefined : text[index - 1] | |
| const query = text.slice(index) | |
| if ((before === undefined || /\s/.test(before)) && !/\s/.test(query)) { | |
| return promptOffsetWidth(text.slice(0, index)) | |
| } | |
| } | |
Xet Storage Details
- Size:
- 1.56 kB
- Xet hash:
- f9ebd83c9b9aad9cd2bb727129cb9dafc0b16e9cc2b9b45559ac56e774123d67
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.