UNPKG

getline

Version:

Classes for reading line-terminated data in files

42 lines (35 loc) 1.02 kB
#!/usr/bin/env node // put a copy of getline.js in the same directory as this file var Getline = require("./getline.js"); var fs = require("fs"); console.log("First, use GetlineSync to iterate through this file\n"); // read our own demonstration file var gl = new Getline.GetlineSync(__filename); try { for (var count = 1;; ++count) { console.log(count + ":\t" + gl.next()); } } catch (e) { if (e === "EOF") console.log("End of file reached"); else console.log(e.toString()); } console.log("\nNow, use GetlineAsync\n"); fs.open(__filename, "r", 0644, function (err, fd) { if (err) console.log(err.toString()); else { var count = 1; var gl = new Getline.GetlineAsync(fd); gl.on("line", function (err, eof, line) { if (err) console.log(err.toString()); else if (eof) console.log("End of file reached"); else { console.log(count + ":\t" + line); ++count; gl.next(); // carry on stepping through file } }); gl.next(); // kick it off } });