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

nerder-index

v0.2.8

Published

Calculate a difficulty for a given functions.

Downloads

1

Readme

Nerder Index (N)

npm Build Status Commitizen friendly Standard Version styled with prettier

Contents

Dependencies

  • Node >= 6.0.0
  • NPM >= 3.0.0

Usage

Install the "Nerder Index" generator:

npm i nerder-index

and import it in your application:

const { get } = require("nerder-index");

API

get(fn: string)

Get the "Nerder Index" as a float number representing the difficulty of the given function.

Parameters:

  • fn string

Returns:

  • nerder index float

analyse(fn: string)

Get a detailed object containing more information about the difficulty.

Parameters:

  • fn string

Returns:

  • Object object
    • nerder index nerderIndex: float
    • parameter count parameterCount: int
    • cyclomatic complexity: complexity: int
    • operator index operatorIndex: int

TypeScript support

This module supports typescript and is packed with its own type definition file. The analyse function returns the interface Report.

How

Following test setup was used to extract difficulty for a given functions (example):

const { get } = require("nerder-index");
const fn = function test() {
  return 1;
};
const result = get(fn.toString());

Info: The function to test has to be named, otherwise the resulting string is no valid JS on root level, needed for esprima to parse the code to an AST. result is an object containing multiple values calculated by escomplex.

Values relevant to calculate a difficulty level for one specific function are:

  • Cyclomatic complexity (C): Defined by Thomas J. McCabe in 1976, this is a count of the number of cycles in the program flow control graph. Effectively the number of distinct paths through a block of code. Lower is better.
  • Number of parameters (Pn): Analysed statically from the function signature, so no accounting is made for functions that rely on the arguments object. Lower is better.
  • Operator index (o): The number of total operators divided by the number of distinct operators. Lower is better.

Cyclomatic complexity

The "cyclomaticy" is C{1,...,n}. A function without control constructs still has a complexity of C = 1. The following examples will demonstrate:

// C = 1
function fn1() {
  return 1;
}

// C = 1
function fn2(p) {
  let a = 13;
  a = (2 * p) / a - 5 + 42;
  return 42;
}

// C = 2
function fn3(a) {
  if (a) {
    return a;
  }
  return 1;
}

Number of parameters

Number of parameters P{n} can be every natural number, but it should be restricted to max value. The parameters are a multiplier.

Operator index

The operator index is defined by o = oT / oD. The following functions defines 8 (function and return keyword are excluded) distinct operators (let, =, (), ., +, -, /, *) and a total of 10.

function fn2(p) {
  let a = 4;
  a = (2 * p) / a - 5 + 42;
  return 42;
}

The operator index for this function is o = 10 / 8 => 1.25.

Nerder Index (N)

N = o * Pn * C

Examples

Here is an example setup to calculate the "Nerder Index" for 4 functions:

const { get } = require("nerder-index");

const fns = [
  function fn1() {
    return 1;
  },
  function fn2(c) {
    let a = 42;
    a = (2 * a) / a - 5 + c;
    return 42;
  },
  function fn3(c, d) {
    let a = 23;
    a = (2 * a) / a - d + 42;
    return a + c;
  },
  function fn4(a, b, c) {
    if (a === 0) {
      return b;
    }
    return c;
  }
];

fns.forEach(fn => {
  const result = get(fn.toString());
  console.log("N =", result);
});

These methods produce the following results:

  • Nfn1 = 0
  • Nfn2 = 1.1
  • Nfn3 = 2.4
  • Nfn4 = 7.5

Result

The "Nerder Index" is a float where 0 is an invalid index (no parameters defined, so no difficulty). Which index is "easy" or "hard" needs to be tested. All of this is just a first test to calculate the complexity of a method.