rsformat
Version:
Formatting/printing library for JavaScript that takes after rust's string formatting
78 lines (77 loc) • 2.04 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printToStream = printToStream;
exports.print = print;
exports.println = println;
exports.eprint = eprint;
exports.eprintln = eprintln;
exports.dbg = dbg;
const node_process_1 = __importDefault(require("node:process"));
const _1 = require(".");
/**
* Print a string (or instance of String/RsString) to a `Writable` stream.
*
* @param stream Stream to print the string to
* @param string String to print
* @param newline Whether to append a newline after the string
* @param colored Whether to use colors for `rs` debug formatting
*/
function printToStream(stream, string, newline = false, colored = false) {
if (string instanceof String) {
string = string.toString(colored);
}
if (newline)
string = string + '\n';
stream.write(string);
}
/**
* Print a string to stdout.
*
* @param string String to print
*/
function print(string) {
printToStream(node_process_1.default.stdout, string, false, true);
}
/**
* Print a string to stdout and append a newline.
*
* @param string String to print
*/
function println(string) {
printToStream(node_process_1.default.stdout, string, true, true);
}
/**
* Print a string to stderr.
*
* @param string String to print
*/
function eprint(string) {
printToStream(node_process_1.default.stderr, string, false, true);
}
/**
* Print a string to stderr and append a newline.
*
* @param string String to print
*/
function eprintln(string) {
printToStream(node_process_1.default.stderr, string, true, true);
}
/**
* Debug print a value to stderr and return it.
*
* ```js
* let r = Math.random();
* if(dbg(r < 0.3)) { // prints 'true' or 'false'
* println("Unlucky!");
* }
* ```
*
* @param value Value to debug print
*/
function dbg(value) {
eprintln((0, _1.rs) `${value}:#?`);
return value;
}