UNPKG

@beenotung/tslib

Version:
50 lines (49 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IO = exports.getRL = void 0; exports.createRL = createRL; const readline_1 = require("readline"); const lazy_1 = require("./lazy"); function createRL(options = { input: process.stdin, output: process.stdout }) { return (0, readline_1.createInterface)(options); } exports.getRL = (0, lazy_1.createLazy)(createRL); var IO; (function (IO) { /** * @description lineNum start from 0 * */ function forEachLine(onnext, oncomplete) { const rl = (0, exports.getRL)(); let lineNum = -1; rl.on('line', line => { lineNum++; if (line) { onnext(line, lineNum); } }); if (typeof oncomplete === 'function') { rl.on('close', oncomplete); } } IO.forEachLine = forEachLine; /** * @description lineNum start from 0 * */ async function mapLine(f) { return new Promise((resolve, reject) => { try { const res = []; forEachLine((line, lineNum) => res.push(f(line, lineNum)), () => resolve(res)); } catch (e) { reject(e); } }); } IO.mapLine = mapLine; async function collect() { return mapLine(x => x); } IO.collect = collect; })(IO || (exports.IO = IO = {}));