-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput-output_dev.js
More file actions
49 lines (41 loc) · 1.13 KB
/
input-output_dev.js
File metadata and controls
49 lines (41 loc) · 1.13 KB
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
39
40
41
42
43
44
45
46
47
48
49
import readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const promisedReadline = () =>
new Promise((resolve, reject) => {
const id = setTimeout(() => {
// throw new Error('TIMEOUT wait line stdin');
// eslint-disable-next-line prefer-promise-reject-errors
reject('TIMEOUT wait line stdin');
}, 10000);
rl.once('line', (data) => {
clearTimeout(id);
resolve(data);
});
});
export const input = async (INPUT_LINE_COUNT = 1) => {
const inputLines = [];
do {
try {
const data = await promisedReadline();
inputLines.push(data.toString().trim());
} catch (error) {
console.log(error);
console.log(
`Received ${inputLines.length} just lines. Expected ${INPUT_LINE_COUNT}`
);
rl.close();
return inputLines;
}
} while (inputLines.length < INPUT_LINE_COUNT);
rl.close();
return inputLines;
};
export const output = (outputLines) => {
process.stdout.write(
Array.isArray(outputLines) ? outputLines.join('\n') : String(outputLines)
);
// process.exit();
};