@beenotung/tslib
Version:
utils library in Typescript
67 lines • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IO = exports.rl = exports.getRL = exports.createRL = void 0;
const tslib_1 = require("tslib");
const readline_1 = require("readline");
const lazy_1 = require("./lazy");
function createRL(options = { input: process.stdin, output: process.stdout }) {
return readline_1.createInterface(options);
}
exports.createRL = createRL;
exports.getRL = lazy_1.createLazy(createRL);
{
let isNew = true;
exports.rl = new Proxy({}, {
get: (target, p) => {
if (isNew) {
isNew = false;
Object.assign(target, exports.getRL());
}
return target[p];
},
});
}
var IO;
(function (IO) {
/**
* @description lineNum start from 0
* */
function forEachLine(onnext, oncomplete) {
const rl = 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
* */
function mapLine(f) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
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;
function collect() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return mapLine(x => x);
});
}
IO.collect = collect;
})(IO = exports.IO || (exports.IO = {}));
//# sourceMappingURL=io.js.map