Skip to content

Commit bf9a158

Browse files
author
Isaac Ramirez
committed
- implement the isEmpty & readString methods to In
1 parent 7becc35 commit bf9a158

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/libs/in/in.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ class In {
2323
this.content = fs.readFileSync(this.fileName, 'utf8').toString().trim()
2424
}
2525

26+
/**
27+
* Returns if the In is empty.
28+
*/
29+
isEmpty () {
30+
return this.content.length === 0
31+
}
32+
33+
/**
34+
* Parses the file's content to and
35+
* array of strings separated by new lines and whitespace,
36+
* then, it returns the first element of the array.
37+
*/
38+
readString () {
39+
if (!this._isArrayOfStrings) {
40+
this.content = this.content.split(/[\n\r\s]*/)
41+
this._isArrayOfStrings = true
42+
}
43+
44+
if (this.isEmpty()) throw new ReferenceError(`stream ${this.fileName} is empty`)
45+
46+
const string = this.content.shift()
47+
48+
return string
49+
}
50+
2651
/**
2752
* Parses the file's contents to Integers
2853
* @returns {[number]} The array of parsed integers.

0 commit comments

Comments
 (0)