halstead-metrics 0.0.0-stage → 1.1.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/metrics.js ADDED
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+
3
+ const { OPERATORS } = require("./languages");
4
+
5
+ const BRACE_PAIRS = [
6
+ ["{", "}"],
7
+ ["(", ")"],
8
+ ["[", "]"],
9
+ ["<", ">"],
10
+ ];
11
+
12
+ /**
13
+ * Insertion-ordered multiset, matching Operators/Operands ArrayLists.
14
+ */
15
+ class Counter {
16
+ constructor() {
17
+ this.names = [];
18
+ this.counts = [];
19
+ }
20
+
21
+ /**
22
+ * @param {string} name
23
+ * @param {number} [amount]
24
+ */
25
+ add(name, amount) {
26
+ const step = amount === undefined ? 1 : amount;
27
+ const index = this.names.indexOf(name);
28
+ if (index === -1) {
29
+ this.names.push(name);
30
+ this.counts.push(step);
31
+ return;
32
+ }
33
+ this.counts[index] += step;
34
+ }
35
+
36
+ /**
37
+ * @returns {boolean}
38
+ */
39
+ has(name) {
40
+ return this.names.indexOf(name) !== -1;
41
+ }
42
+
43
+ /**
44
+ * @returns {Array<[string, number]>}
45
+ */
46
+ entries() {
47
+ const pairs = [];
48
+ for (let i = 0; i < this.names.length; i += 1) {
49
+ pairs.push([this.names[i], this.counts[i]]);
50
+ }
51
+ return pairs;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Keywords, then the shared operator list, then balanced brace pairs, then leftovers.
57
+ * @param {string[]} tokens
58
+ * @param {Set<string>|null} keywords
59
+ * @param {string[]|null} [symbols]
60
+ */
61
+ function classify(tokens, keywords, symbols) {
62
+ const operators = new Counter();
63
+ const operands = new Counter();
64
+ const symbolList = symbols || OPERATORS;
65
+
66
+ for (let i = 0; i < tokens.length; i += 1) {
67
+ const token = tokens[i];
68
+ if (keywords && keywords.has(token)) {
69
+ operators.add(token);
70
+ } else if (isStringToken(token)) {
71
+ operands.add(token);
72
+ }
73
+ }
74
+
75
+ for (let s = 0; s < symbolList.length; s += 1) {
76
+ const symbol = symbolList[s];
77
+ let count = 0;
78
+ for (let i = 0; i < tokens.length; i += 1) {
79
+ if (tokens[i] === symbol) {
80
+ count += 1;
81
+ }
82
+ }
83
+ if (count > 0) {
84
+ operators.add(symbol, count);
85
+ }
86
+ }
87
+
88
+ for (let b = 0; b < BRACE_PAIRS.length; b += 1) {
89
+ const open = BRACE_PAIRS[b][0];
90
+ const close = BRACE_PAIRS[b][1];
91
+ let startCount = 0;
92
+ let endCount = 0;
93
+ for (let i = 0; i < tokens.length; i += 1) {
94
+ if (tokens[i] === open) {
95
+ startCount += 1;
96
+ } else if (tokens[i] === close) {
97
+ endCount += 1;
98
+ }
99
+ }
100
+ if (startCount === endCount && startCount > 0) {
101
+ operators.add(open + " " + close, startCount);
102
+ }
103
+ }
104
+
105
+ for (let i = 0; i < tokens.length; i += 1) {
106
+ const token = tokens[i];
107
+ if (operators.has(token) || operands.has(token) || token === "<EOF>") {
108
+ continue;
109
+ }
110
+ operands.add(token);
111
+ }
112
+
113
+ return evaluate(operators, operands);
114
+ }
115
+
116
+ /**
117
+ * Quoted literals are operands.
118
+ * @param {string} token
119
+ * @returns {boolean}
120
+ */
121
+ function isStringToken(token) {
122
+ const ch = token.charAt(0);
123
+ return ch === "\"" || ch === "'" || ch === "`";
124
+ }
125
+
126
+ /**
127
+ * Formulas from MetricsEvaluator.
128
+ * @param {Counter} operators
129
+ * @param {Counter} operands
130
+ */
131
+ function evaluate(operators, operands) {
132
+ const n1 = operators.names.length;
133
+ const n2 = operands.names.length;
134
+ let N1 = 0;
135
+ let N2 = 0;
136
+ for (let i = 0; i < operators.counts.length; i += 1) {
137
+ N1 += operators.counts[i];
138
+ }
139
+ for (let i = 0; i < operands.counts.length; i += 1) {
140
+ N2 += operands.counts[i];
141
+ }
142
+
143
+ const programLength = N1 + N2;
144
+ const programVocabulary = n1 + n2;
145
+ const estimatedLength = (n1 * (Math.log(n1) / Math.log(2))) + (n2 * (Math.log(n2) / Math.log(2)));
146
+ const purityRatio = estimatedLength / programLength;
147
+ const volume = programLength * (Math.log(programVocabulary) / Math.log(2));
148
+ const difficulty = (n1 / 2) * (N2 / n2);
149
+ const effort = volume * difficulty;
150
+ const programmingTime = effort / 18;
151
+
152
+ return {
153
+ n1: n1,
154
+ n2: n2,
155
+ N1: N1,
156
+ N2: N2,
157
+ vocabulary: programVocabulary,
158
+ length: programLength,
159
+ estimatedLength: estimatedLength,
160
+ purityRatio: purityRatio,
161
+ volume: volume,
162
+ difficulty: difficulty,
163
+ effort: effort,
164
+ programmingTime: programmingTime,
165
+ operators: operators,
166
+ operands: operands,
167
+ };
168
+ }
169
+
170
+ /**
171
+ * Half-even rounding to two decimal places, then parsed back to a number.
172
+ * Non-finite values become null.
173
+ * @param {number} value
174
+ * @returns {number|null}
175
+ */
176
+ function roundTo2Decimals(value) {
177
+ if (!Number.isFinite(value)) {
178
+ return null;
179
+ }
180
+ const negative = value < 0;
181
+ const scaled = Math.abs(value) * 100;
182
+ const floor = Math.floor(scaled + 1e-10);
183
+ const fraction = scaled - floor;
184
+ let digits = floor;
185
+ if (fraction > 0.5 + 1e-10) {
186
+ digits = floor + 1;
187
+ } else if (Math.abs(fraction - 0.5) <= 1e-10) {
188
+ digits = floor % 2 === 0 ? floor : floor + 1;
189
+ }
190
+ const rounded = (negative ? -digits : digits) / 100;
191
+ return Number(rounded.toFixed(2));
192
+ }
193
+
194
+ /**
195
+ * Whole numbers keep one decimal place (`5.0`).
196
+ * @param {number} value
197
+ * @returns {string}
198
+ */
199
+ function formatRounded(value) {
200
+ const rounded = roundTo2Decimals(value);
201
+ if (rounded === null) {
202
+ return "";
203
+ }
204
+ if (Number.isInteger(rounded)) {
205
+ return rounded.toFixed(1);
206
+ }
207
+ return String(rounded);
208
+ }
209
+
210
+ /**
211
+ * JSON document printed by cli.NewMain.
212
+ * @param {ReturnType<typeof evaluate>} metrics
213
+ * @param {string} filename
214
+ */
215
+ function toReport(metrics, filename) {
216
+ const operatorMap = {};
217
+ const operators = metrics.operators.entries();
218
+ for (let i = 0; i < operators.length; i += 1) {
219
+ operatorMap[operators[i][0]] = String(operators[i][1]);
220
+ }
221
+ const operandMap = {};
222
+ const operands = metrics.operands.entries();
223
+ for (let i = 0; i < operands.length; i += 1) {
224
+ operandMap[operands[i][0]] = String(operands[i][1]);
225
+ }
226
+
227
+ return {
228
+ Halstead: {
229
+ n1: metrics.n1,
230
+ n2: metrics.n2,
231
+ N1: metrics.N1,
232
+ N2: metrics.N2,
233
+ Vocabulary: roundTo2Decimals(metrics.vocabulary),
234
+ Length: roundTo2Decimals(metrics.length),
235
+ "Estimated program length": roundTo2Decimals(metrics.estimatedLength),
236
+ Volume: roundTo2Decimals(metrics.volume),
237
+ Difficulty: roundTo2Decimals(metrics.difficulty),
238
+ Effort: roundTo2Decimals(metrics.effort),
239
+ "Programming time": roundTo2Decimals(metrics.programmingTime),
240
+ "Purity ratio": roundTo2Decimals(metrics.purityRatio),
241
+ _Operands: operandMap,
242
+ _Operators: operatorMap,
243
+ },
244
+ filename: filename,
245
+ };
246
+ }
247
+
248
+ module.exports = {
249
+ Counter,
250
+ classify,
251
+ evaluate,
252
+ roundTo2Decimals,
253
+ formatRounded,
254
+ toReport,
255
+ };