0.2.3-development • Published 2 years ago
@modular-rocks/metrics-ts-js v0.2.3-development
Quality metrics for Javascript/Typescript
Installation
npm install @modular-rocks/metrics-ts-js
or
yarn add @modular-rocks/metrics-ts-js
Metrics
There are currently 8 metrics:
- Cyclomatic
- Total Cyclomatic Complexity
- Maintainability
- Import Coupling
- Identifier Coupling
- Halstead
- LOC
- LCOM*
*LCOM is not directly adaptable to functional Javascript because LCOM compares identifiers in methods in classes. So this LCOM method is a custom adaption that accounts for modern Javascript usage.
Usage
Each metric accepts an Options argument containing configuration options. Either one of two options are required, code or ast.
code is the code string to parse and evaluate. ast is an already parsed AST.
Examples
Execute the following examples in a node environment (not in a browser).
import { cyclomatic, totalCyclomaticComplexity, loc, halstead, maintainability } from "@modular-rocks/metrics"
const opts = {
code: `
function calculateAverage(numbers) {
if (!Array.isArray(numbers)) {
return 'Invalid input. Please provide an array of numbers.';
}
if (numbers.length === 0) {
return 'Empty array. Please provide an array with at least one number.';
}
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
const average = sum / numbers.length;
return average;
}
`
}
cyclomatic(opts)
// 7
totalCyclomaticComplexity(opts)
// 4
loc(opts)
// 17
halstead(opts)
// {
// "bugs": 0.0533612357509481,
// "difficulty": 8.125,
// "effort": 2025.446143672791,
// "length": 49,
// "time": 112.52478575959951,
// "vocabulary": 34,
// "volume": 249.2856792212666,
// }
maintainability(opts)
// 75.59906223945923import { lcom, identifierCoupling } from "@modular-rocks/metrics"
const opts = {
code: `
// Shared variables
let sharedVariable = 0;
function incrementSharedVariable() {
sharedVariable++;
console.log('Shared Variable:', sharedVariable);
}
// Not shared variables
function multiplyNumbers(a, b) {
let result = a * b;
console.log('Result:', result);
}
function addNumbers(a, b) {
let sum = a + b;
console.log('Sum:', sum);
}
`
}
lcom(opts)
// { "cohesion": 0.5, "lcom": 2.25 }
identifierCoupling(opts)
// 0.25import { importCoupling } from "@modular-rocks/metrics"
const opts = {
totalModulesCount: 10,
code: `
import ModuleA from 'module-a';
import { FunctionB } from 'module-b';
`
}
importCoupling(opts)
// 0.003316749585406302Test Project
The code in the test-project folder is a snapshot of the code from DIM, containing 600+ components/modules.
License
Apache 2.0