npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@modular-rocks/metrics

v0.2.0-development

Published

Code quality metrics for Javascript/Typescript

Downloads

2

Readme

Quality metrics for Javascript/Typescript

Installation

npm install @modular-rocks/metrics

or

yarn add @modular-rocks/metrics

Metrics

There are currently 8 metrics:

*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.59906223945923

import { 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.25

import { importCoupling } from "@modular-rocks/metrics"

const opts = {
  totalModulesCount: 10,
  code: `
    import ModuleA from 'module-a';
    import { FunctionB } from 'module-b';
  `
}

importCoupling(opts)
// 0.003316749585406302

License

To be licensed, either MIT or Apache 2.0. Lets decide.