jsshort
Version:
It will make your codes much short and easiar
67 lines (66 loc) • 2.03 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.log = exports.print = exports.input = exports.end = void 0;
const readline_1 = __importDefault(require("readline"));
const fs_1 = __importDefault(require("fs"));
exports.end = "\n";
global.end = '\n';
const input = async (question = "") => {
const rl = readline_1.default.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(question, async (input) => {
resolve(input.trim());
rl.close();
});
});
};
exports.input = input;
const print = (...args) => {
// Join the arguments with spaces and add the specified ending character
let output;
// Check if the last argument is an object and extract 'sep'
const lastArg = args[args.length - 1];
if (typeof lastArg === "object" || lastArg === "" || lastArg === " ") {
args.pop();
}
if (typeof lastArg === "object") {
if (lastArg.hasOwnProperty("sep")) {
let sep = lastArg.sep;
output = args.join(sep);
}
if (lastArg.end != null) {
if (!output)
output = args.join(" ") + lastArg.end;
else
output += lastArg.end;
}
}
else if (lastArg == "" || lastArg == " ") {
output = args.join(' ') + global.end;
}
else {
output = args.join(' ') + global.end;
}
if (lastArg && lastArg.file) {
fs_1.default.writeFile(lastArg.file, output, (err) => {
if (err) {
console.error(err);
}
});
}
else {
process.stdout.write(output);
}
global.end = "\n";
};
exports.print = print;
exports.log = console.log;
global.log = exports.log;
global.print = exports.print;
global.input = exports.input;
;