What is split-lines?
The split-lines npm package is a simple utility for splitting a string into an array of lines. It handles different newline characters and can optionally keep the newline characters in the resulting array.
What are split-lines's main functionalities?
Basic Line Splitting
This feature allows you to split a string into an array of lines based on newline characters. It handles different types of newline characters such as \n, \r\n, and \r.
const splitLines = require('split-lines');
const text = 'Hello\nWorld';
const lines = splitLines(text);
console.log(lines); // ['Hello', 'World']
Keep Newline Characters
This feature allows you to split a string into an array of lines while keeping the newline characters in the resulting array. This can be useful if you need to preserve the original formatting.
const splitLines = require('split-lines');
const text = 'Hello\nWorld';
const lines = splitLines(text, { keepNewlines: true });
console.log(lines); // ['Hello\n', 'World']
Other packages similar to split-lines
split-string
The split-string package provides more advanced string splitting capabilities, including splitting by multiple delimiters and handling escape characters. It is more versatile but also more complex compared to split-lines.
line-reader
The line-reader package is designed for reading lines from a file rather than splitting a string. It provides a way to read lines asynchronously, which is useful for large files. It is more focused on file I/O compared to split-lines.
readline
The readline package is a built-in Node.js module that provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time. It is more comprehensive and suitable for interactive command-line applications.
split-lines
Split lines into an array of lines
Install
$ npm install --save split-lines
Usage
var splitLines = require('split-lines');
splitLines('foo\r\nbar\r\nbaz\nrainbow');
License
MIT © Sindre Sorhus