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:
Get Started
You are able to use Streader as the importable npm package or directly in browser.
In Node.js
; var reader = "example"; while !reader var char = reader; // Do something with each char
In Browser
API
read(count)
Reads the next characters. Count - optional count of characters to read
var reader = "Example"; var chars = reader; // chars => "Example"
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 = "Example"; var chars = reader; // chars => "amp"
skip(count)
Skips the next characters. Count - optional count of characters to read.
var reader = "Example"; var skipped = reader; // skipped => 3
readPattern(pattern)
Reads characters that match either string or regexp pattern.
var reader = "Pattern Example"; var chars = reader; // chars => "Pattern"var index = reader; // index => 7 var chars = reader; // chars => " "var index = reader; // index => 8
peekPattern(pattern, offset)
Reads characters that match either string or regexp pattern without advancing the cursor.
var reader = "Pattern Example"; var chars = reader; // chars => "Ex"var index = reader; // index => 0 var chars = reader; // chars => "Pattern"var index = reader; // index => 0
skipPattern(pattern)
Skips characters that match either string or regexp.
var reader = "Pattern Example"; var skipped = reader; // skipped => 7 var skipped = reader; // skipped => 1
eof()
Checks if we're at the end of the source
var reader = "example";reader; // Read all characters var isEof = reader; // isEof => true
reset()
Resets current cursor position
var reader = "example";reader; // Read 5 characters for example reader; var index = reader; // index => 0
getSource()
Gets the current reader's source string.
var reader = "example"; var source = reader; // source => "example"
getIndex()
Gets the current index of the cursor.
var reader = "example";reader; // shift cursor for example var index = reader; // index => 5
setSource(text)
Loads the new text source to StringReader. Method resets current cursor data. Useful for deferred read.
var reader = "old";reader; // shift cursor for example reader; var source = reader; // source => "new"var index = reader; // index => 0
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