Rename files

This commit is contained in:
Fedor Indutny
2025-10-16 17:33:01 -07:00
parent 3387cf6a77
commit 44076ece79
2411 changed files with 0 additions and 0 deletions

46
ts/util/splitText.std.ts Normal file
View File

@@ -0,0 +1,46 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export type SplitTextOptionsType = Readonly<{
granularity: 'grapheme' | 'word';
shouldBreak: (slice: string) => boolean;
}>;
export function splitText(
text: string,
{ granularity, shouldBreak }: SplitTextOptionsType
): Array<string> {
const isWordBased = granularity === 'word';
const segmenter = new Intl.Segmenter(undefined, {
granularity,
});
const result = new Array<string>();
// Compute number of lines and height of text
let acc = '';
let best = '';
for (const { segment, isWordLike } of segmenter.segment(text)) {
acc += segment;
// For "grapheme" segmenting, "isWordLike" is always "undefined"
if (isWordLike === false) {
best = acc;
continue;
}
if (shouldBreak(isWordBased ? acc.trim() : acc)) {
result.push(best);
acc = acc.slice(best.length);
best = acc;
} else {
best = acc;
}
}
if (best) {
result.push(best);
}
return isWordBased ? result.map(x => x.trim()) : result;
}