Skip to content

countLines

function countLines(text, stopAfter?): number;

Defined in: packages/engine/src/utilities/Strings.ts:29

How many lines text has, without splitting it into an array.

split("\n").length answers the same question and allocates the whole document to do it, which is the wrong shape for a caller whose next move may be to refuse the document for being too large. Counting stops as soon as the answer is “more than the caller cares about”.

ParameterTypeDefault valueDescription
textstringundefinedThe document.
stopAfternumberNumber.MAX_SAFE_INTEGERStop counting once past this many lines.

number

The line count, or stopAfter + 1 for anything longer. A document with no newline in it is one line, matching split.