Skip to content

splitLines ​

Splits a string into lines, supporting \n, \r\n and \r line breaks. Like String.prototype.split, a trailing line break produces a trailing empty string. compact only removes lines that are exactly empty, so enable trim as well to also remove whitespace-only lines.

Usage ​

ts
import { splitLines } from 'rattail'

splitLines('a\r\nb\nc')
// return ['a', 'b', 'c']
splitLines(' a \n\n b \n', { trim: true })
// return ['a', '', 'b', '']
splitLines(' a \n\n b \n', { trim: true, compact: true })
// return ['a', 'b']

Arguments ​

ArgTypeDefaultDescription
textstringThe text to split
optionsobject{}Split options
options.trimbooleanfalseTrim each line
options.compactbooleanfalseRemove empty lines, checked after trim is applied

Return ​

Type
string[]