halstead-metrics 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/report.js ADDED
@@ -0,0 +1,567 @@
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+
5
+ const { formatRounded } = require("./metrics");
6
+
7
+ const VALUE_ROWS = [
8
+ ["No of Distinct Operators(n1)", "n1"],
9
+ ["No of Distinct Operands(n2)", "n2"],
10
+ ["Total No of Operators(N1)", "N1"],
11
+ ["Total No of Operands(N2)", "N2"],
12
+ ["Program Length", "length"],
13
+ ["Program Vocabulary", "vocabulary"],
14
+ ["Estimated Length", "estimatedLength"],
15
+ ["Purity Ratio", "purityRatio"],
16
+ ["Volume", "volume"],
17
+ ["Difficulty", "difficulty"],
18
+ ["Program Effort", "effort"],
19
+ ["Programming Time", "programmingTime"],
20
+ ];
21
+
22
+ /**
23
+ * Report stem: everything before the first dot.
24
+ * @param {string} filename
25
+ * @returns {string}
26
+ */
27
+ function reportBaseName(filename) {
28
+ const base = path.basename(String(filename));
29
+ const dot = base.indexOf(".");
30
+ return dot === -1 ? base : base.slice(0, dot);
31
+ }
32
+
33
+ /**
34
+ * @param {string} value
35
+ * @returns {string}
36
+ */
37
+ function escapeHtml(value) {
38
+ return String(value)
39
+ .replace(/&/g, "&")
40
+ .replace(/</g, "&lt;")
41
+ .replace(/>/g, "&gt;")
42
+ .replace(/"/g, "&quot;");
43
+ }
44
+
45
+ const ABOUT = [
46
+ "Halstead software science treats a program as a sequence of operators and operands. The four base counts are the distinct operators (n1), the distinct operands (n2), the total operators (N1), and the total operands (N2). Vocabulary and length are those counts added together. Volume, difficulty, effort, and programming time are calculated from them.",
47
+ "The measures describe the size of the implementation and how much mental work the token mix implies. They are not a defect count, a coverage figure, or a statement about runtime performance.",
48
+ ];
49
+
50
+ const FORMULAS = [
51
+ ["Vocabulary", "n1 + n2"],
52
+ ["Program length", "N1 + N2"],
53
+ ["Estimated length", "n1 log2(n1) + n2 log2(n2)"],
54
+ ["Purity ratio", "estimated length / program length"],
55
+ ["Volume", "program length \u00d7 log2(vocabulary)"],
56
+ ["Difficulty", "(n1 / 2) \u00d7 (N2 / n2)"],
57
+ ["Effort", "volume \u00d7 difficulty"],
58
+ ["Programming time", "effort / 18 seconds"],
59
+ ];
60
+
61
+ const SCOPE = [
62
+ "This report covers one source file. Comments are left out. Keywords and operators are counted as operators. Identifiers, numbers, and quoted strings are counted as operands. When a brace pair is balanced, that pair is recorded as one more operator.",
63
+ "Compare files in the same language that do similar work. A larger number means a larger or more demanding implementation under this model. It is not, by itself, a pass or fail result.",
64
+ ];
65
+
66
+ const SUMMARY_KEYS = [
67
+ ["Length", "length"],
68
+ ["Vocabulary", "vocabulary"],
69
+ ["Volume", "volume"],
70
+ ["Difficulty", "difficulty"],
71
+ ["Effort", "effort"],
72
+ ["Time", "programmingTime"],
73
+ ];
74
+
75
+ /**
76
+ * Sentences that explain this file's measured values.
77
+ * @param {object} metrics
78
+ * @returns {string[]}
79
+ */
80
+ function readingFor(metrics) {
81
+ const purity = metrics.purityRatio;
82
+ let purityNote = "Purity ratio is not defined for this count.";
83
+ if (Number.isFinite(purity) && metrics.length > 0) {
84
+ if (purity < 0.85) {
85
+ purityNote = "Purity ratio is " + formatRounded(purity) + ". The measured length is longer than the length estimated from the vocabulary, so operators or operands are repeated more than a compact expression of the same tokens would suggest.";
86
+ } else if (purity > 1.15) {
87
+ purityNote = "Purity ratio is " + formatRounded(purity) + ". The measured length is shorter than the length estimated from the vocabulary. The file introduces many distinct operators or operands and uses each of them only a few times.";
88
+ } else {
89
+ purityNote = "Purity ratio is " + formatRounded(purity) + ". The measured length is close to the length estimated from the vocabulary, which is what the Halstead length equation expects for this mix of tokens.";
90
+ }
91
+ }
92
+
93
+ const volumeNote = Number.isFinite(metrics.volume)
94
+ ? "Volume is " + formatRounded(metrics.volume) + ". That is the information size of the implementation, in bits. A larger volume means more of the program has to be accounted for, token by token."
95
+ : "Volume is not defined for this count.";
96
+
97
+ let difficultyNote = "Difficulty is not defined for this count.";
98
+ if (Number.isFinite(metrics.difficulty) && metrics.n2 > 0) {
99
+ const reuse = formatRounded(metrics.N2 / metrics.n2);
100
+ difficultyNote = "Difficulty is " + formatRounded(metrics.difficulty) + ". It rises when there are many distinct operators, or when operands are reused often. This file has " + metrics.n1 + " distinct operators and " + metrics.n2 + " distinct operands, and each distinct operand appears about " + reuse + " times.";
101
+ }
102
+
103
+ let timeNote = "Programming time is not defined for this count.";
104
+ if (Number.isFinite(metrics.programmingTime)) {
105
+ timeNote = "Programming time is " + formatRounded(metrics.programmingTime) + " seconds. Halstead divides effort by 18, an estimate of elementary mental discriminations per second. Use it to compare files with each other, not as a clock of how long the file took to write.";
106
+ if (metrics.programmingTime >= 60) {
107
+ timeNote += " That is about " + formatRounded(metrics.programmingTime / 60) + " minutes on the same scale.";
108
+ }
109
+ }
110
+
111
+ return [purityNote, volumeNote, difficultyNote, timeNote];
112
+ }
113
+
114
+ /**
115
+ * @param {string[]} paragraphs
116
+ * @returns {string}
117
+ */
118
+ function prose(paragraphs) {
119
+ let html = "<div class=\"prose\">";
120
+ for (let i = 0; i < paragraphs.length; i += 1) {
121
+ html += "<p>" + escapeHtml(paragraphs[i]) + "</p>";
122
+ }
123
+ return html + "</div>";
124
+ }
125
+
126
+ /**
127
+ * @param {object} metrics
128
+ * @returns {string}
129
+ */
130
+ function summaryCards(metrics) {
131
+ let html = "<div class=\"stats\">";
132
+ for (let i = 0; i < SUMMARY_KEYS.length; i += 1) {
133
+ html += "<div class=\"stat\"><span>" + SUMMARY_KEYS[i][0] + "</span><strong>"
134
+ + escapeHtml(formatRounded(metrics[SUMMARY_KEYS[i][1]])) + "</strong></div>";
135
+ }
136
+ return html + "</div>";
137
+ }
138
+
139
+ /**
140
+ * @returns {string}
141
+ */
142
+ function formulaTable() {
143
+ let rows = "<tr><th>Measure</th><th>Formula</th></tr>";
144
+ for (let i = 0; i < FORMULAS.length; i += 1) {
145
+ rows += "<tr><td>" + escapeHtml(FORMULAS[i][0]) + "</td><td>"
146
+ + escapeHtml(FORMULAS[i][1]) + "</td></tr>";
147
+ }
148
+ return "<div class=\"sheet formulas\"><table>" + rows + "</table></div>";
149
+ }
150
+
151
+ /**
152
+ * @param {Array<[string, number]>} entries
153
+ * @returns {string}
154
+ */
155
+ function countTable(entries) {
156
+ let rows = "<tr><th>Name</th><th>Count</th></tr>";
157
+ for (let i = 0; i < entries.length; i += 1) {
158
+ rows += "<tr><td>" + escapeHtml(entries[i][0]) + "</td><td>"
159
+ + escapeHtml(entries[i][1]) + "</td></tr>";
160
+ }
161
+ return "<div class=\"sheet\"><table>" + rows + "</table></div>";
162
+ }
163
+
164
+ /**
165
+ * Detailed HTML report: operators, operands, and metric values.
166
+ * @param {object} metrics result of analyzeSource
167
+ * @param {string} filename display name, usually the source file name
168
+ * @returns {string}
169
+ */
170
+ function renderHtmlReport(metrics, filename) {
171
+ const fileName = path.basename(String(filename));
172
+ let values = "";
173
+ for (let i = 0; i < VALUE_ROWS.length; i += 1) {
174
+ const label = VALUE_ROWS[i][0];
175
+ const key = VALUE_ROWS[i][1];
176
+ values += "<tr><th>" + label + "</th><td>" + escapeHtml(formatRounded(metrics[key])) + "</td></tr>";
177
+ }
178
+
179
+ return "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\">"
180
+ + "<title>Halstead Metrics</title>"
181
+ + "<style>"
182
+ + "html,body{margin:0;background:#f3f4f6;color:#18181b;font:14px/1.45 ui-sans-serif,system-ui,-apple-system,\"Segoe UI\",sans-serif;letter-spacing:-0.011em;}"
183
+ + "#layout{box-sizing:border-box;width:min(800px,calc(100% - 32px));margin:24px auto;padding:24px 28px 20px;background:#fff;border:1px solid #e4e4e7;border-radius:14px;box-shadow:0 1px 2px rgba(24,24,27,.04),0 8px 24px rgba(24,24,27,.04);}"
184
+ + ".brand{display:flex;align-items:center;gap:10px;margin-bottom:18px;}"
185
+ + ".mark{display:grid;place-items:center;width:32px;height:32px;border-radius:10px;background:#18181b;color:#fff;font-weight:650;letter-spacing:-0.04em;}"
186
+ + ".brand-name{font-weight:650;letter-spacing:-0.03em;}"
187
+ + ".brand-sub{color:#71717a;font-size:12px;}"
188
+ + "h1{margin:0;font-size:28px;font-weight:650;letter-spacing:-0.03em;}"
189
+ + ".lede{margin:6px 0 18px;color:#71717a;font-size:13px;}"
190
+ + ".file{margin:0 0 22px;padding:12px 14px;border:1px solid #e4e4e7;border-radius:12px;background:#fafafa;font-size:20px;font-weight:650;letter-spacing:-0.03em;}"
191
+ + ".file h3{margin:0 0 2px;color:#71717a;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;}"
192
+ + "h4{margin:26px 0 8px;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;color:#71717a;}"
193
+ + ".stats{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px;}"
194
+ + ".stat{padding:10px 12px;border:1px solid #e4e4e7;border-radius:12px;background:#fafafa;}"
195
+ + ".stat span{display:block;color:#71717a;font-size:11px;font-weight:600;letter-spacing:0.04em;text-transform:uppercase;}"
196
+ + ".stat strong{display:block;margin-top:2px;font-size:20px;font-weight:650;letter-spacing:-0.03em;font-variant-numeric:tabular-nums;}"
197
+ + ".prose p{margin:0 0 10px;color:#3f3f46;font-size:14px;line-height:1.55;}"
198
+ + ".callout{padding:12px 14px;border:1px solid #e0e7ff;border-left:3px solid #4f46e5;border-radius:12px;background:#f8faff;}"
199
+ + ".callout p:last-child{margin-bottom:0;}"
200
+ + ".sheet{overflow:hidden;border:1px solid #e4e4e7;border-radius:12px;}"
201
+ + "table{width:100%;border-collapse:collapse;background:#fff;}"
202
+ + "th,td{padding:8px 12px;border-bottom:1px solid #e4e4e7;text-align:left;}"
203
+ + "tr:last-child td,tr:last-child th{border-bottom:0;}"
204
+ + "th{background:#fafafa;font-size:12px;font-weight:600;}"
205
+ + "td:last-child,th:last-child{text-align:right;font-variant-numeric:tabular-nums;}"
206
+ + ".formulas td,.formulas th{text-align:left;font-variant-numeric:normal;}"
207
+ + "#footer{margin-top:22px;padding-top:14px;border-top:1px solid #e4e4e7;}"
208
+ + "#footer-contents{margin:0;color:#71717a;font-size:12px;}"
209
+ + "</style></head><body><div id=\"layout\">"
210
+ + "<header id=\"header\"><div class=\"brand\"><span class=\"mark\">H</span><div>"
211
+ + "<div class=\"brand-name\">Halstead</div><div class=\"brand-sub\">Metrics</div></div></div>"
212
+ + "<h1 id=\"heading\">Halstead Metrics Detailed Report</h1>"
213
+ + "<p class=\"lede\">Operators, operands, and the derived Halstead values for the open file.</p>"
214
+ + "</header><div id=\"body\">"
215
+ + "<div class=\"file\"><h3>File Name: </h3>" + escapeHtml(fileName) + "</div>"
216
+ + "<h4>Summary</h4>" + summaryCards(metrics)
217
+ + "<h4>About Halstead metrics</h4>" + prose(ABOUT)
218
+ + "<h4>Reading this file</h4><div class=\"callout\">" + prose(readingFor(metrics)) + "</div>"
219
+ + "<h4>Formulas</h4>" + formulaTable()
220
+ + "<h4>Operators</h4>" + countTable(metrics.operators.entries())
221
+ + "<h4>Operands</h4>" + countTable(metrics.operands.entries())
222
+ + "<h4>Measured values</h4><div class=\"sheet\"><table>" + values + "</table></div>"
223
+ + "<h4>How to use this report</h4>" + prose(SCOPE)
224
+ + "</div><footer id=\"footer\"><p id=\"footer-contents\">Copyright &copy; Harish Ramasubramanian</p></footer>"
225
+ + "</div></body></html>";
226
+ }
227
+
228
+ /**
229
+ * @param {import("pdfkit")} doc
230
+ * @returns {number}
231
+ */
232
+ function contentWidth(doc) {
233
+ return doc.page.width - doc.page.margins.left - doc.page.margins.right;
234
+ }
235
+
236
+ /**
237
+ * Gray page with a white card, matching the HTML report canvas.
238
+ * @param {import("pdfkit")} doc
239
+ */
240
+ function paintPage(doc) {
241
+ const inset = 22;
242
+ doc.save();
243
+ doc.rect(0, 0, doc.page.width, doc.page.height).fill("#f3f4f6");
244
+ doc.roundedRect(inset, inset, doc.page.width - inset * 2, doc.page.height - inset * 2, 14).fill("#ffffff");
245
+ doc.restore();
246
+ }
247
+
248
+ /**
249
+ * @param {import("pdfkit")} doc
250
+ * @param {string} fileName
251
+ */
252
+ function drawHeader(doc, fileName) {
253
+ const left = doc.page.margins.left;
254
+ const width = contentWidth(doc);
255
+ const y = doc.y;
256
+ doc.save();
257
+ doc.roundedRect(left, y, 32, 32, 10).fill("#18181b");
258
+ doc.restore();
259
+ doc.fillColor("#ffffff").font("Helvetica-Bold").fontSize(14);
260
+ doc.text("H", left, y + 9, { width: 32, align: "center", lineBreak: false });
261
+ doc.fillColor("#18181b").font("Helvetica-Bold").fontSize(13);
262
+ doc.text("Halstead", left + 42, y + 4, { lineBreak: false });
263
+ doc.fillColor("#71717a").font("Helvetica").fontSize(10);
264
+ doc.text("Metrics", left + 42, y + 18, { lineBreak: false });
265
+ doc.x = left;
266
+ doc.y = y + 48;
267
+ doc.fillColor("#18181b").font("Helvetica-Bold").fontSize(22);
268
+ doc.text("Halstead Metrics Detailed Report", { width: width });
269
+ doc.moveDown(0.25);
270
+ doc.fillColor("#71717a").font("Helvetica").fontSize(11);
271
+ doc.text("Operators, operands, and the derived Halstead values for the open file.", { width: width });
272
+ doc.moveDown(0.7);
273
+ const cardY = doc.y;
274
+ const cardH = 50;
275
+ doc.save();
276
+ doc.roundedRect(left, cardY, width, cardH, 12).fillAndStroke("#fafafa", "#e4e4e7");
277
+ doc.restore();
278
+ doc.fillColor("#71717a").font("Helvetica-Bold").fontSize(8);
279
+ doc.text("FILE NAME", left + 14, cardY + 10, { lineBreak: false });
280
+ doc.fillColor("#18181b").font("Helvetica-Bold").fontSize(16);
281
+ doc.text(fileName, left + 14, cardY + 24, { width: width - 28, lineBreak: false });
282
+ doc.x = left;
283
+ doc.y = cardY + cardH + 8;
284
+ }
285
+
286
+ /**
287
+ * @param {import("pdfkit")} doc
288
+ * @param {object} metrics
289
+ */
290
+ function drawStatGrid(doc, metrics) {
291
+ const left = doc.page.margins.left;
292
+ const width = contentWidth(doc);
293
+ const gap = 8;
294
+ const cols = 3;
295
+ const cardW = (width - gap * (cols - 1)) / cols;
296
+ const cardH = 48;
297
+ const origin = doc.y;
298
+ for (let i = 0; i < SUMMARY_KEYS.length; i += 1) {
299
+ const col = i % cols;
300
+ const row = Math.floor(i / cols);
301
+ const x = left + col * (cardW + gap);
302
+ const y = origin + row * (cardH + gap);
303
+ doc.save();
304
+ doc.roundedRect(x, y, cardW, cardH, 10).fillAndStroke("#fafafa", "#e4e4e7");
305
+ doc.restore();
306
+ doc.fillColor("#71717a").font("Helvetica-Bold").fontSize(8);
307
+ doc.text(SUMMARY_KEYS[i][0].toUpperCase(), x + 10, y + 8, {
308
+ width: cardW - 20,
309
+ lineBreak: false,
310
+ });
311
+ doc.fillColor("#18181b").font("Helvetica-Bold").fontSize(14);
312
+ doc.text(formatRounded(metrics[SUMMARY_KEYS[i][1]]), x + 10, y + 22, {
313
+ width: cardW - 20,
314
+ lineBreak: false,
315
+ });
316
+ }
317
+ const rows = Math.ceil(SUMMARY_KEYS.length / cols);
318
+ doc.x = left;
319
+ doc.y = origin + rows * (cardH + gap);
320
+ }
321
+
322
+ /**
323
+ * @param {import("pdfkit")} doc
324
+ * @param {Array<Array<{text: string, fill?: boolean, bold?: boolean}>>} rows
325
+ * @param {{alignLast?: "left"|"right"}} [options]
326
+ */
327
+ function drawTable(doc, rows, options) {
328
+ const alignLast = options && options.alignLast === "left" ? "left" : "right";
329
+ const left = doc.page.margins.left;
330
+ const tableWidth = contentWidth(doc);
331
+ const colWidths = [tableWidth * 0.62, tableWidth * 0.38];
332
+ const heights = [];
333
+ let total = 0;
334
+ for (let r = 0; r < rows.length; r += 1) {
335
+ const row = rows[r];
336
+ let rowHeight = 24;
337
+ for (let c = 0; c < row.length; c += 1) {
338
+ const align = c === row.length - 1 ? alignLast : "left";
339
+ doc.font(row[c].bold ? "Helvetica-Bold" : "Helvetica").fontSize(10);
340
+ const textHeight = doc.heightOfString(String(row[c].text), {
341
+ width: colWidths[c] - 20,
342
+ align: align,
343
+ });
344
+ rowHeight = Math.max(rowHeight, textHeight + 12);
345
+ }
346
+ heights.push(rowHeight);
347
+ total += rowHeight;
348
+ }
349
+ const bottom = doc.page.height - doc.page.margins.bottom;
350
+ const fits = doc.y + total <= bottom;
351
+ const startY = doc.y;
352
+ if (fits) {
353
+ doc.save();
354
+ doc.roundedRect(left, startY, tableWidth, total, 10).clip();
355
+ }
356
+ for (let r = 0; r < rows.length; r += 1) {
357
+ const row = rows[r];
358
+ const rowHeight = heights[r];
359
+ if (!fits && doc.y + rowHeight > bottom) {
360
+ doc.addPage();
361
+ }
362
+ const y = doc.y;
363
+ if (row[0].fill) {
364
+ doc.save();
365
+ doc.rect(left, y, tableWidth, rowHeight).fill("#fafafa");
366
+ doc.restore();
367
+ }
368
+ if (r < rows.length - 1) {
369
+ doc.save();
370
+ doc.lineWidth(0.6).strokeColor("#e4e4e7");
371
+ doc.moveTo(left, y + rowHeight).lineTo(left + tableWidth, y + rowHeight).stroke();
372
+ doc.restore();
373
+ }
374
+ let x = left;
375
+ for (let c = 0; c < row.length; c += 1) {
376
+ const cell = row[c];
377
+ const align = c === row.length - 1 ? alignLast : "left";
378
+ doc.font(cell.bold ? "Helvetica-Bold" : "Helvetica").fontSize(10).fillColor("#18181b");
379
+ doc.text(String(cell.text), x + 10, y + 6, {
380
+ width: colWidths[c] - 20,
381
+ align: align,
382
+ lineBreak: true,
383
+ });
384
+ x += colWidths[c];
385
+ }
386
+ doc.y = y + rowHeight;
387
+ doc.x = left;
388
+ }
389
+ if (fits) {
390
+ doc.restore();
391
+ doc.save();
392
+ doc.lineWidth(0.8).strokeColor("#e4e4e7");
393
+ doc.roundedRect(left, startY, tableWidth, total, 10).stroke();
394
+ doc.restore();
395
+ }
396
+ doc.x = left;
397
+ doc.y = fits ? startY + total + 8 : doc.y + 8;
398
+ }
399
+
400
+ /**
401
+ * Indigo reading panel, matching the HTML callout.
402
+ * @param {import("pdfkit")} doc
403
+ * @param {string[]} paragraphs
404
+ */
405
+ function drawCallout(doc, paragraphs) {
406
+ const left = doc.page.margins.left;
407
+ const width = contentWidth(doc);
408
+ const inner = width - 32;
409
+ doc.font("Helvetica").fontSize(10.5);
410
+ let textHeight = 8;
411
+ for (let i = 0; i < paragraphs.length; i += 1) {
412
+ textHeight += doc.heightOfString(paragraphs[i], { width: inner }) + 8;
413
+ }
414
+ if (doc.y + textHeight > doc.page.height - doc.page.margins.bottom) {
415
+ doc.addPage();
416
+ }
417
+ const y = doc.y;
418
+ doc.save();
419
+ doc.roundedRect(left, y, width, textHeight, 12).fillAndStroke("#f8faff", "#e0e7ff");
420
+ doc.rect(left, y + 10, 3, Math.max(8, textHeight - 20)).fill("#4f46e5");
421
+ doc.restore();
422
+ let cursor = y + 10;
423
+ doc.fillColor("#3f3f46").font("Helvetica").fontSize(10.5);
424
+ for (let i = 0; i < paragraphs.length; i += 1) {
425
+ doc.text(paragraphs[i], left + 16, cursor, { width: inner });
426
+ cursor = doc.y + 6;
427
+ }
428
+ doc.x = left;
429
+ doc.y = y + textHeight + 10;
430
+ }
431
+
432
+ /**
433
+ * @param {import("pdfkit")} doc
434
+ * @param {string[]} paragraphs
435
+ */
436
+ function drawParagraphs(doc, paragraphs) {
437
+ const left = doc.page.margins.left;
438
+ const width = contentWidth(doc);
439
+ doc.fillColor("#3f3f46").font("Helvetica").fontSize(10.5);
440
+ for (let i = 0; i < paragraphs.length; i += 1) {
441
+ doc.x = left;
442
+ doc.text(paragraphs[i], { width: width, lineGap: 2 });
443
+ doc.moveDown(0.35);
444
+ }
445
+ }
446
+
447
+ /**
448
+ * @param {import("pdfkit")} doc
449
+ * @param {string} text
450
+ */
451
+ function drawHeading(doc, text) {
452
+ const left = doc.page.margins.left;
453
+ doc.x = left;
454
+ doc.moveDown(0.45);
455
+ doc.fillColor("#71717a").font("Helvetica-Bold").fontSize(9);
456
+ doc.text(text.toUpperCase(), { characterSpacing: 0.6 });
457
+ doc.moveDown(0.3);
458
+ }
459
+
460
+ /**
461
+ * Printable report with the same sections as the HTML report.
462
+ * @param {object} metrics
463
+ * @param {string} filename
464
+ * @returns {Promise<Buffer>}
465
+ */
466
+ function renderPdfReport(metrics, filename) {
467
+ const PDFDocument = require("pdfkit");
468
+ const fileName = path.basename(String(filename));
469
+ return new Promise((resolve, reject) => {
470
+ const doc = new PDFDocument({
471
+ size: "LETTER",
472
+ compress: false,
473
+ margins: { top: 46, bottom: 46, left: 46, right: 46 },
474
+ info: {
475
+ Title: "Halstead Metrics - " + reportBaseName(filename) + " Report",
476
+ Author: "Harish Ramasubramanian",
477
+ Subject: "Halstead Metrics Detailed Report for " + fileName,
478
+ },
479
+ });
480
+ const chunks = [];
481
+ doc.on("data", (chunk) => chunks.push(chunk));
482
+ doc.on("end", () => resolve(Buffer.concat(chunks)));
483
+ doc.on("error", reject);
484
+ paintPage(doc);
485
+ doc.on("pageAdded", () => paintPage(doc));
486
+
487
+ drawHeader(doc, fileName);
488
+
489
+ drawHeading(doc, "Summary");
490
+ drawStatGrid(doc, metrics);
491
+
492
+ drawHeading(doc, "About Halstead metrics");
493
+ drawParagraphs(doc, ABOUT);
494
+ drawHeading(doc, "Reading this file");
495
+ drawCallout(doc, readingFor(metrics));
496
+ drawHeading(doc, "Formulas");
497
+ const formulaRows = [[
498
+ { text: "Measure", fill: true, bold: true },
499
+ { text: "Formula", fill: true, bold: true },
500
+ ]];
501
+ for (let i = 0; i < FORMULAS.length; i += 1) {
502
+ formulaRows.push([
503
+ { text: FORMULAS[i][0] },
504
+ { text: FORMULAS[i][1] },
505
+ ]);
506
+ }
507
+ drawTable(doc, formulaRows, { alignLast: "left" });
508
+
509
+ drawHeading(doc, "Operators:");
510
+ const operators = metrics.operators.entries();
511
+ const operatorRows = [[
512
+ { text: "Name", fill: true, bold: true },
513
+ { text: "Count", fill: true, bold: true },
514
+ ]];
515
+ for (let i = 0; i < operators.length; i += 1) {
516
+ operatorRows.push([
517
+ { text: operators[i][0] },
518
+ { text: String(operators[i][1]) },
519
+ ]);
520
+ }
521
+ drawTable(doc, operatorRows);
522
+
523
+ drawHeading(doc, "Operands:");
524
+ const operands = metrics.operands.entries();
525
+ const operandRows = [[
526
+ { text: "Name", fill: true, bold: true },
527
+ { text: "Count", fill: true, bold: true },
528
+ ]];
529
+ for (let i = 0; i < operands.length; i += 1) {
530
+ operandRows.push([
531
+ { text: operands[i][0] },
532
+ { text: String(operands[i][1]) },
533
+ ]);
534
+ }
535
+ drawTable(doc, operandRows);
536
+
537
+ drawHeading(doc, "Measured values");
538
+ const valueRows = [];
539
+ for (let i = 0; i < VALUE_ROWS.length; i += 1) {
540
+ valueRows.push([
541
+ { text: VALUE_ROWS[i][0], fill: true },
542
+ { text: formatRounded(metrics[VALUE_ROWS[i][1]]) },
543
+ ]);
544
+ }
545
+ drawTable(doc, valueRows);
546
+ drawHeading(doc, "How to use this report");
547
+ drawParagraphs(doc, SCOPE);
548
+ doc.moveDown(0.4);
549
+ const left = doc.page.margins.left;
550
+ const width = doc.page.width - doc.page.margins.left - doc.page.margins.right;
551
+ doc.save();
552
+ doc.lineWidth(0.6).strokeColor("#e4e4e7");
553
+ doc.moveTo(left, doc.y).lineTo(left + width, doc.y).stroke();
554
+ doc.restore();
555
+ doc.moveDown(0.5);
556
+ doc.fillColor("#71717a").font("Helvetica").fontSize(10);
557
+ doc.text("Copyright \u00a9 Harish Ramasubramanian", left, doc.y, { width: width });
558
+ doc.end();
559
+ });
560
+ }
561
+
562
+ module.exports = {
563
+ VALUE_ROWS,
564
+ reportBaseName,
565
+ renderHtmlReport,
566
+ renderPdfReport,
567
+ };
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.