@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
104 lines (103 loc) • 3.33 kB
JavaScript
;
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var formatters_exports = {};
__export(formatters_exports, {
formatCoverage: () => formatCoverage,
getCoverageEmoji: () => getCoverageEmoji
});
module.exports = __toCommonJS(formatters_exports);
var import_strings = require("../strings");
const COVERAGE_EMOJI_THRESHOLDS = [
{ emoji: " \u{1F680}", threshold: 99 },
{ emoji: " \u{1F3AF}", threshold: 95 },
{ emoji: " \u2728", threshold: 90 },
{ emoji: " \u{1F49A}", threshold: 85 },
{ emoji: " \u2705", threshold: 80 },
{ emoji: " \u{1F7E2}", threshold: 70 },
{ emoji: " \u{1F7E1}", threshold: 60 },
{ emoji: " \u{1F528}", threshold: 50 },
{ emoji: " \u26A0\uFE0F", threshold: 0 }
];
function getCoverageEmoji(percent) {
const entry = COVERAGE_EMOJI_THRESHOLDS.find(
({ threshold }) => percent >= threshold
);
return entry?.emoji || "";
}
function formatCoverage(options) {
const opts = {
__proto__: null,
format: "default",
...options
};
const { code, format, type } = opts;
if (format === "json") {
return JSON.stringify({ code, type }, null, 2);
}
const overall = calculateOverall(code, type);
if (format === "simple") {
return overall;
}
let output = "";
output += "Code Coverage:\n";
output += (0, import_strings.indentString)(`Statements: ${code.statements.percent}%
`, {
count: 2
});
output += (0, import_strings.indentString)(`Branches: ${code.branches.percent}%
`, { count: 2 });
output += (0, import_strings.indentString)(`Functions: ${code.functions.percent}%
`, {
count: 2
});
output += (0, import_strings.indentString)(`Lines: ${code.lines.percent}%
`, { count: 2 });
if (type) {
output += "\nType Coverage:\n";
output += (0, import_strings.indentString)(
`${type.percent}% (${type.covered}/${type.total})
`,
{ count: 2 }
);
}
const emoji = getCoverageEmoji(Number.parseFloat(overall));
output += `
Overall: ${overall}%${emoji}
`;
return output;
}
function calculateOverall(code, type) {
const metrics = [
Number.parseFloat(code.statements.percent),
Number.parseFloat(code.branches.percent),
Number.parseFloat(code.functions.percent),
Number.parseFloat(code.lines.percent)
];
if (type) {
metrics.push(Number.parseFloat(type.percent));
}
const average = metrics.reduce((sum, val) => sum + val, 0) / metrics.length;
return average.toFixed(2);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
formatCoverage,
getCoverageEmoji
});