Skip to content

5.7 text to lines

wiki[bot] edited this page Aug 23, 2026 · 1 revision

Text To Lines

A class that splits text into sentences and provides a chainable API to accumulate text.

import { TextToLines } from "@triplef/helpers/text-to-lines";

Supported Punctuation

Type Punctuation
Western ., ?, !, ...
CJK (Chinese, Japanese, Korean) , , , ……

Constructor

new TextToLines(base: string | string[]): TextToLines

Initialize with a string or array of strings.

Properties

lines

Returns the number of sentences currently accumulated.

const splitter = new TextToLines("Hello! How are you?");
splitter.lines; // 2

Methods

append

Append more text or array of strings to the current sentences. Returns the same instance for chaining.

append(val: string | string[]): this

build

Returns all accumulated sentences as an array of strings.

build(): string[]

Throws an error if no valid sentences exist.

Examples

Basic Usage

const splitter = new TextToLines("Hello world! How are you?");
splitter.lines; // 2
splitter.build(); // ["Hello world!", "How are you?"]

Chaining

const text = new TextToLines("First sentence.")
  .append("Second sentence?")
  .append("Third sentence!");

text.lines; // 3
text.build(); // ["First sentence.", "Second sentence?", "Third sentence!"]

Array Input

const text = new TextToLines(["Hello.", "How are you?"]);
text.lines; // 2
text.build(); // ["Hello.", "How are you?"]

CJK Support

const text = new TextToLines("这是中文句子。还有另一个!");
text.lines; // 2
text.build(); // ["这是中文句子。", "还有一个!"]

Mixed Content

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!"]

Clone this wiki locally