-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd-in.js
38 lines (33 loc) · 934 Bytes
/
std-in.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const assert = require('assert')
const readline = require('readline')
let reader = null
/**
* StdIn
* @classdesc JavaScript implementation of StdIn.
* @see {@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/StdIn.java.html}
*/
class StdIn {
/**
* Returns a new Object with a Readline Interface as prototype.
* @see {@link https://nodejs.org/api/readline.html}
* @returns {EventEmitter} The reader interface.
*/
static read () {
if (reader === null) {
reader = Object.create(readline.createInterface({
input: process.stdin
}))
}
return reader
}
/**
* Parses a string line to a integer number.
* @param {string} The string representing a number.
* @returns {number} The parsed number to integer.
*/
static readInt (line) {
assert(typeof line === 'string', 'line should be a string')
return parseInt(line, 10)
}
}
module.exports = StdIn