Parses lines from text, preserving line numbers, offsets and line endings.
$ npm install linez
/// <reference path="node_modules/linez/linez.d.ts" />
import linez = require('linez');
var linez = require('linez');
By default, linez uses /\r?\n/g
as the regular expression to detect newline character sequences and split lines. This regular expression is tuned for performance and only covers the most common newline types (i.e., \n
and \r\n
). If you have need for more newline character sequences, you can configure linez with a convenient newlines
property.
linez.newlines = ['\n', '\r\n', '\r', '\u000B'];
Setting this property will automatically create a piped regular expression for you and use it in any future linez.parse()
calls. You can make up your own newlines if you want. Linez doesn't care one way or the other.
linez.newlines = ['foo', 'bar'];
This would be converted into /(?:foo|bar)
. Newlines are just strings. They can be anything. There are, however, some known newline character sequences. Should you need them, refer to the following table:
String | Unicode | Name |
---|---|---|
\n |
U+000A | Line feed |
\r\n |
U+000D, U+000A | Carriage Return + Line Feed |
\r |
U+000D | Carriage Return |
\u000B |
U+000B | Vertical Tab |
\u000C |
U+000C | Form Feed |
\u0085 |
U+0085 | Next Line |
\u2028 |
U+2028 | Line Separator |
\u2029 |
U+2029 | Paragraph Separator |
Set this property to configure linez to use any number of newline character sequences.
linez.newlines = ['\n', '\r\n', '\r', '\u000B'];
Parses text into lines, each of which is defined by the ILine interface.
interface ILine {
offset: number;
number: number;
text: string;
ending?: string;
}
The specs show some great usage examples.
var lines = linez.parse('foo\nbar\nbaz');
lines[1].offset; // 4
lines[1].number; // 2
lines[1].text; // bar
lines[1].ending; // \n
Released under the MIT license.