-
-
Notifications
You must be signed in to change notification settings - Fork 0
5.7 text to lines
wiki[bot] edited this page Aug 23, 2026
·
1 revision
A class that splits text into sentences and provides a chainable API to accumulate text.
import { TextToLines } from "@triplef/helpers/text-to-lines";| Type | Punctuation |
|---|---|
| Western |
., ?, !, ...
|
| CJK (Chinese, Japanese, Korean) |
。, ?, !, ……
|
new TextToLines(base: string | string[]): TextToLinesInitialize with a string or array of strings.
Returns the number of sentences currently accumulated.
const splitter = new TextToLines("Hello! How are you?");
splitter.lines; // 2Append more text or array of strings to the current sentences. Returns the same instance for chaining.
append(val: string | string[]): thisReturns all accumulated sentences as an array of strings.
build(): string[]Throws an error if no valid sentences exist.
const splitter = new TextToLines("Hello world! How are you?");
splitter.lines; // 2
splitter.build(); // ["Hello world!", "How are you?"]const text = new TextToLines("First sentence.")
.append("Second sentence?")
.append("Third sentence!");
text.lines; // 3
text.build(); // ["First sentence.", "Second sentence?", "Third sentence!"]const text = new TextToLines(["Hello.", "How are you?"]);
text.lines; // 2
text.build(); // ["Hello.", "How are you?"]const text = new TextToLines("这是中文句子。还有另一个!");
text.lines; // 2
text.build(); // ["这是中文句子。", "还有一个!"]const text = new TextToLines("Hello world! 这是中文句子。How are you?")
.append("もう一つの文。")
.append(["And one more...", "Last one!"]);
text.lines; // 5
text.build();
// ["Hello world!", "这是中文句子。", "How are you?", "もう一つの文。", "And one more...", "Last one!"]