UNPKG

68kcounter

Version:
97 lines (96 loc) 3.44 kB
"use strict"; /** * Formatter implementations for CLI output */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlainTextFormatter = exports.JsonFormatter = void 0; const chalk_1 = __importDefault(require("chalk")); const _1 = require("."); class JsonFormatter { constructor(options) { this.options = options; } format(lines, totals) { const inc = this.options.include; const output = { lines: inc.text || inc.timings || inc.bytes ? lines.map((l) => ({ text: inc.text ? l.statement.text : undefined, timing: inc.timings ? l.timing : undefined, bytes: inc.bytes ? l.bytes : undefined, })) : undefined, totals: inc.totals ? totals : undefined, }; return this.options.prettyPrint ? JSON.stringify(output, null, 2) : JSON.stringify(output); } } exports.JsonFormatter = JsonFormatter; class PlainTextFormatter { constructor(options) { this.options = options; } format(lines, totals) { const inc = this.options.include; let output = []; if (inc.text || inc.timings || inc.bytes) { output = lines.map((l) => { let annotation = ""; if (l.timing && inc.timings) { const format = this.options.color ? this.formatTimingColored : _1.formatTiming; annotation += l.timing.values.map(format).join(" / "); } if (l.bytes && inc.bytes) { annotation += " " + this.formatNumber(l.bytes); } annotation = this.pad(annotation, this.options.width); if (inc.text) { annotation += " | " + l.statement.text; } return annotation; }); } if (inc.totals) { output.push("\nTotals:"); if (totals.isRange) { output.push(_1.formatTiming(totals.min) + " - " + _1.formatTiming(totals.max)); } else { output.push(_1.formatTiming(totals.min)); } output.push(`${this.formatNumber(totals.bytes)} bytes (${this.formatNumber(totals.objectBytes)} object, ${this.formatNumber(totals.bssBytes)} BSS)`); } return output.join("\n"); } formatTimingColored(timing) { const output = _1.formatTiming(timing); const level = _1.timingLevel(timing); return chalk_1.default[PlainTextFormatter.levelToColor[level]](output); } /** * Display a string with padding */ pad(str, l) { /*eslint-disable no-control-regex */ const strClean = str.replace(/(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]/g, ""); const p = l - strClean.length; return p > 0 ? Array(p).fill(" ").join("") + str : str; } formatNumber(num) { return num.toLocaleString("en"); } } exports.PlainTextFormatter = PlainTextFormatter; PlainTextFormatter.levelToColor = { [_1.Levels.VHigh]: "bgRed", [_1.Levels.High]: "red", [_1.Levels.Med]: "yellow", [_1.Levels.Low]: "green", };