UNPKG

1.11 kBJavaScriptView Raw
1const { Readable } = require('stream')
2
3const aliens = require('./aliens.json')
4const words = require('./words.json')
5
6const inputFromAliens = () => {
7 const input = []
8 for (let i = 0; i < 10; i++) {
9 const alien = aliens[Math.floor(Math.random() * aliens.length)]
10 input.push(`${alien}\n`)
11 }
12 return input
13}
14
15const inputFromWords = () => {
16 const input = []
17 const offset = Math.floor(words.length * Math.random())
18 for (let i = 0; i < 10; i++) {
19 const word = words[(offset + i) % words.length]
20 input.push(`${word}\n`)
21 }
22 return input
23}
24
25const rndPort = () => Math.floor(Math.random() * 40000 + 10000)
26
27const writeStream = (stream, input, time) => {
28 let count = 0
29 const iv = setInterval(function () {
30 stream.write(input[count].trim() + '\n')
31
32 if (++count === input.length) {
33 clearInterval(iv)
34 stream.end()
35 }
36 }, time)
37}
38
39const readableStream = () => {
40 const stream = new Readable({ objectMode: true })
41 stream._read = function () {}
42 return stream
43}
44
45module.exports = {
46 inputFromAliens,
47 inputFromWords,
48 rndPort,
49 writeStream,
50 readableStream
51}