Is it possible to synchronously read from stdin in node.js? Because I\'m writing a brainfuck to JavaScript compiler in JavaScript (just for fun). Brainfuck supports a read o
I wrote this module to read one line at a time from file or stdin. The module is named as line-reader which exposes an ES6 *Generator function to iterate over one line at a time. here is a code sample(in TypeScript) from readme.md.
import { LineReader } from "line-reader"
// FromLine and ToLine are optional arguments
const filePathOrStdin = "path-to-file.txt" || process.stdin
const FromLine: number = 1 // default is 0
const ToLine: number = 5 // default is Infinity
const chunkSizeInBytes = 8 * 1024 // default is 64 * 1024
const list: IterableIterator = LineReader(filePathOrStdin, FromLine, ToLine, chunkSizeInBytes)
// Call list.next to iterate over lines in a file
list.next()
// Iterating using a for..of loop
for (const item of list) {
console.log(item)
}
Apart from above code, you can also take a look at src > tests folder in the repo.
Note:-
line-reader module doesn't read all stuff into memory instead it uses generator function to generate lines async or sync.