1.0.3 • Published 2 years ago
@santimir/line-reader v1.0.3
line-reader
Splits and Reads lines from a string or string array.
If the argument is an array, it does not further split, just enables the functionality for reading lines.
- Class keeps track of the current index,
- Performs string splitting using Regex or string characters
- A few of the helper methods:
readLine
,readLines
,readTo
,getLines
,skip
,record
,rewind
,reset
Installation
npm i @santimir/line-reader
Examples
import { LineReader } from "@santimir/line-reader";
const myString = "hello\n world\n new string\n here";
const options = { eol:'\n', index:0 };//eol can also be regex
let lines = new LineReader(myString, options);
const example = lines.readLine() + lines.skip().readLine(); // "hello new string"
Or more complex..
import { readFileSync as rfs } from "fs";
import { LineReader } from "@santimir/line-reader";
const myStuff = rfs("./path/to/file").toString("utf8");
let lines = new LineReader(myStuff);
lines.skip(10) //skip first 10 lines
.record() //records current index
.seek(3) //moves to index 3
.rewind() //back to index 11
.reset() //index 0
.readLine() //returns first line
let switch = true;
while(switch){
if(lines.readLine().startsWith("<body>")){
switch=false
}
}
const bodyFirstLine = lines.readLine();