mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-24 20:26:24 +00:00
Rename files
This commit is contained in:
46
ts/util/splitText.std.ts
Normal file
46
ts/util/splitText.std.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user