Streader
A simple String Reader for a convenient way to read through strings
Basic Overview
Streader is a String Reader which simplifies the way we read through strings.
Library can be used both in Node.js and directly in Browser.
It is well-documented and completely covered with test specs (excluding webpack bundling definitions and expressions).
If you want to add some features or to suggest any idea, feel free ? contributions are always welcome.
How to Install
Using NPM
To use Streader with NPM simply call:
npm install --save streader
In Browser
To use Streader directly in browser simply download this repository and copy dist/streader.js into your project.
Next, include it on your .html page:
<script src="path/to/your/js/streader.js"></script>
Get Started
You are able to use Streader as the importable npm package or directly in browser.
In Node.js
import Streader from "streader";
var reader = new Streader("example");
while( !reader.eof() ) {
var char = reader.read();
}
In Browser
<script>
var reader = new Streader("example");
while( !reader.eof() ) {
var char = reader.read();
}
</script>
API
read(count)
Reads the next characters. Count - optional count of characters to read
var reader = new Streader("Example");
var chars = reader.read(7);
peek(count, offset)
Reads the next character without advancing the cursor. Count - optional count of characters to read; Offset - optional offset to start read at
var reader = new Streader("Example");
var chars = reader.peek(3, 2);
skip(count)
Skips the next characters. Count - optional count of characters to read.
var reader = new Streader("Example");
var skipped = reader.skip(3);
readPattern(pattern)
Reads characters that match either string or regexp pattern.
var reader = new Streader("Pattern Example");
var chars = reader.readPattern("Pattern");
var index = reader.getIndex();
var chars = reader.readPattern(/\s+/);
var index = reader.getIndex();
peekPattern(pattern, offset)
Reads characters that match either string or regexp pattern without advancing the cursor.
var reader = new Streader("Pattern Example");
var chars = reader.peekPattern("Ex", 8);
var index = reader.getIndex();
var chars = reader.peekPattern(/\w+/);
var index = reader.getIndex();
skipPattern(pattern)
Skips characters that match either string or regexp.
var reader = new Streader("Pattern Example");
var skipped = reader.skipPattern("Pattern");
var skipped = reader.skipPattern(/\s|\n/);
eof()
Checks if we're at the end of the source
var reader = new Streader("example");
reader.read(100);
var isEof = reader.eof();
reset()
Resets current cursor position
var reader = new Streader("example");
reader.read(5);
reader.reset();
var index = reader.getIndex();
getSource()
Gets the current reader's source string.
var reader = new Streader("example");
var source = reader.getSource();
getIndex()
Gets the current index of the cursor.
var reader = new Streader("example");
reader.read(5);
var index = reader.getIndex();
setSource(text)
Loads the new text source to StringReader. Method resets current cursor data. Useful for deferred read.
var reader = new Streader("old");
reader.read(2);
reader.setSource("new");
var source = reader.getSource();
var index = reader.getIndex();
Contributing to Streader
Contributions are always welcome.
Before contributing please read the code of conduct &
search the issue tracker (your issue may have already been discussed or fixed).
To contribute, follow next steps:
- Fork Streader
- Commit your changes
- Open a Pull Request.
Feature Requests
Feature requests should be submitted in the issue tracker, with a description
of the expected behavior & use case, where they?ll remain closed until sufficient interest (e.g. ? reactions).
Before submitting a feature request, please search for similar ones in the closed issues.
License
Released under the MIT License