Adding DRY peekLine() function

This commit is contained in:
2019-03-16 09:39:10 -06:00
parent 9f13bb4c6d
commit d42ffd492a
2 changed files with 26 additions and 7 deletions

View File

@@ -94,7 +94,6 @@ class FileBuffer {
throw 'Expected to drop "${s}" but was "${actual}"';
}
var lines = s.split('\n');
if (lines.length > 1) {
line += lines.length - 1;
@@ -180,11 +179,9 @@ class FileBuffer {
return data;
}
/** Take the next line of data from the file.
@param trimmed Which sides of the line to trim ('r' 'l', 'lr', or 'rl')
**/
public function takeLine(trimmed = ''): Option<String> {
var nextLine = takeUntil(['\n'], true);
/** DRY Helper for peekLine() and takeLine() **/
function getLine(trimmed: String, retriever: Array<String> -> Bool -> Bool -> Option<BufferOutput>): Option<String> {
var nextLine = retriever(['\n'], true, true);
return switch (nextLine) {
case Some({output: nextLine, terminator: _}):
if (trimmed.indexOf('r') != -1) {
@@ -196,6 +193,20 @@ class FileBuffer {
Some(nextLine);
case None:
None;
}
};
}
/** Peek the next line of data from the file.
@param trimmed Which sides of the line to trim ('r' 'l', 'lr', or 'rl')
**/
public function peekLine(trimmed = ''): Option<String> {
return getLine(trimmed, peekUntil);
}
/** Take the next line of data from the file.
@param trimmed Which sides of the line to trim ('r' 'l', 'lr', or 'rl')
**/
public function takeLine(trimmed = ''): Option<String> {
return getLine(trimmed, takeUntil);
}
}

View File

@@ -66,6 +66,14 @@ class FileBufferTest extends utest.Test {
assertPosition();
}
function testPeekLine() {
assertPosition();
HankAssert.equals(Some('Line of text.'), file.peekLine());
assertPosition();
HankAssert.equals(Some('Line of text.'), file.peekLine());
assertPosition();
}
function testTakeLine() {
assertPosition();