string-reader
v0.9.0
Published
Reads string based on regular expression that represents delimiter
Downloads
12
Maintainers
Readme
StringReader
Reads string based on regular expression that represents delimiter.
const StringReader = require('string-reader')
var reader = new StringReader('The quick brown fox jumps over the lazy dog.');
var words = [];
while(!reader.end){
words.push(reader.readTo(/\s+|\./));
}
console.log(words);
// [ "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" ]
Install
npm install --save string-reader
Constructor
const StringReader = require('string-reader');
var reader = new StringReader(content);
Specify content
to read. This argument is passed to content
property. (content property)
Methods
readTo(delimiter, holdDelimiter, errorThrown)
Reads from the current position to the specified delimiter.
delimiter
: Specify delimiter as RegExp. When String is specified, it is treated as pattern of RegExp. For example,readTo(';')
is equivalent toreadTo(new RegExp(';'))
.holdDelimiter
: Settrue
to include matched delimiter into returned string. By default, delimiter does not be included. For example,new StringReader('a:b').readTo(/:/)
only returnesa
, butnew StringReader('a:b').readTo(/:/, true)
returnsa:
including delimiter.errorThrown
: Specify error that will be thrown if there are no delimiter matched. By default, no error be thrown if delimiter is unmatched, just only reads to end.
Returned value can be treated as String object, but has some additional properties:
position
: Position of the first character of its returned string.delimiter
: Returned value ofRegExp.exec
method. Can use this property to detect which delimiter is matchd.reader
: Instance of StringReader
Note:
- Returned value is extended class that is inherited from String object.
Therefore
new StringReader('abc;def').readTo(/;/) == 'abc'
istrue
, butnew StringReader('abc;def').readTo(/;/) === 'abc'
isfalse
. Can calltoString
method to get primitive String object to prevent such a harmful side effect. - delimiter never be a pattern that matches empty string. For example,
/$/m
does not matchCR
andLF
, thereforeposition
will be set beforeCR
andLF
and cannot advance to next line. So the following code will be loop:while (!reader.end) reader.readTo(/$/m)
. For that reason, throws error if empty match is appeared. Use alternativelyreadTo(/\r?\n|\r/)
orreadLine()
to read to end of the line.
readLine(errorThrown)
Reads from the current position to end of the current line.
The next three newline formats are supported:
CRLF
(Windows), LF
(Linux/Mac X and later) and CR
(Mac 9 and ealier)
errorThrown
: Specify error that be thrown if the current position is end of string. By default, no error be thrown, just only returns empty string.
readToEnd(errorThrown)
Reads from the current position to the end of string.
errorThrown
: Specify error that be thrown if the current position is end of string. By default, no error be thrown, just only returns empty string.
read(count, errorThrown)
Reads specified-count characters from the current position.
count
: Count to read. Can specify negative value to read backwards.errorThrown
: Specify error that be thrown when the current position reached out of the string. By default, no error be thrown, just only return empty or partial string.
peekTo(errorThrown), peekLine(errorThrown), peekToEnd(errorThrown), and peek(count, errorThrown)
peekTo
is equivalent toreadTo
but does not change current position.peekLine
is equivalent toreadLine
but does not change current position.peekToEnd
is equivalent toreadToEnd
but does not change current position.peek
is equivalent toread
but does not change current position.
push(), pop(holdPosition) [experimental]
push
and pop
can save/restore position to/from built-in stack,
pop
returns substring between last pushed index and current index.
holdPosition
: Specifytrue
if do not want to restore by last pushed index.
const StringReader = require('string-reader')
var reader = new StringReader('The quick brown fox jumps over the lazy dog.');
reader.readTo(/\s+/);
console.log(reader.position);
// 4
reader.push();
reader.readTo(/\s+/);
reader.readTo(/\s+/);
reader.readTo(/\s+/);
console.log(reader.pop());
// quick brown fox
console.log(reader.position);
// 4
Properties
content
Get or set the original string.
NOTE:
- If set some value,
position
is always reset to zero. - If set
null
orundefined
, empty string is alternatively set. - If set non-string object, return-value of its
toString
is alternatively set.
position
Get or set current position.
NOTE:
- If set value that is smaller than zero, zero is alternatively set.
- If set value that is greather than its length, value of
length
is alternatively set.
length (Readonly)
Returns total length of original string.
end (Readonly)
Returns whether its position is end of string.
License
Distributed under the MIT license.
Copyright (C) 2016 Retorillo