/
readline.js
73 lines (62 loc) · 1.58 KB
/
readline.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const readline = require('readline')
const MAX_NUM = 4294967295
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.setPrompt('Please provide an integer: ')
rl.on('line', (input) => {
input = input.trim()
if (input === 'exit') {
rl.close()
process.exit(1)
} else {
return rl.handleInput(input)
}
})
rl.handleInput = function (input) {
if(!input || isNaN(input)) {
console.log('\nThat doesn\'t look like a number')
return rl.prompt()
} else {
let num = Number(input)
if (!Number.isInteger(num)) {
num = parseInt(num)
console.log('received float, parsing integer: ' + num)
}
if (Math.abs(num) > MAX_NUM) {
console.log(`\nno integers less than -${MAX_NUM} or greater than ${MAX_NUM}. sorry.`)
return rl.prompt()
} else {
const buffer = rl.numToBinary(Math.abs(num))
if (rl.connection) {
return rl.connection.write(buffer)
} else {
console.log('no connection discovered, aborting')
process.exit(1)
}
}
}
}
rl.numToBinary = function (num) {
const binary = num.toString(2)
const bytes = Math.ceil(binary.length/8)
let buffer
switch (bytes) {
case 1:
buffer = Buffer.alloc(1)
buffer.writeUInt8(num)
break
case 2:
buffer = Buffer.alloc(2)
buffer.writeUInt16BE(num)
break
case 3:
case 4:
buffer = Buffer.alloc(4)
buffer.writeUInt32BE(num)
break
}
return buffer
}
module.exports = rl