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

eslint-plugin-no-nan

v0.1.2

Published

## Starting

Downloads

18

Readme

eslint-plugin-no-nan

Starting

This ESLint plugin helps developers detect points where NaN may arise, providing an alternative that will pop errors. By preventing NaN, there will be less chance of bugs passing through seemingly safe points.

Requirements

The only requirement is that you have ESLint version 5 or higher in your project.

How to use

  1. First, you need to install eslint-plugin-no-nan as a development dependency. You can do this using your preferred package manager:
npm install eslint-plugin-no-nan --save-dev

or

yarn add eslint-plugin-no-nan -D

or 

pnpm install eslint-plugin-no-nan  --save-dev
  1. Then integrate the plugin into your .eslintrc.js configuration (or the respective ESLint configuration file you are using).
{
    "plugins": ["no-nan"],
    "rules": {
      // ... other rules
      "no-nan/no-nan": "error",
    },
}
  1. Example of unsafe code in the context of this library.
const secureNumber = Number('anyInsecureString');

In the example above, we have a Number that tries to convert a string into a number, this rule considers that this string will always be unsafe, and that it may return a NaN

  1. How to fix this?

This plugin provides alternative functions, which perform the same operation as the Number, parseFloat and parseInt functions, however, if NaN is returned, these custom functions throw an error.

Note, instead of returning NaN, the custom function throws an error, 🚨 you need to handle this error! 🚨

import { NumberStrict } from 'eslint-plugin-no-nan';

const secureNumber = NumberStrict('anyInsecureString');

Note, you can create your own custom functions, for example:

const MyNumberCustomFunction = (value: unknown) => {
   // eslint-disable-next-line no-nan/no-nan
   const result = Number(value);
   if (isNaN(result)) {
     return 0
   }
   return result;
};

const secureNumber = MyNumberCustomFunction('anyInsecureString');

This plugin only prohibits the use of global javascript functions, which has this behavior that the developers of this plugin do not like.

Contributing to the project

Would you like to contribute to the project? Great! In our contributing.md guide, you'll find information about how to create a pull request, the code standards we follow, and how to report bugs. Your contribution is valuable and will help improve the tool for everyone.

I hope these explanations and examples are clear and helpful!

Known issues

To report issues, if possible, please provide a snippet or link to the code on GitHub where the issue can be replicated. You can report issues here.

Next steps

The objective of this plugin is to detect points where conversions may return NaN values, requiring a switch to a function that will return an error.