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

js-safe-number

v1.0.4

Published

The safeNumber package ensures safe conversion of any value to a valid, finite number in JavaScript. It handles diverse inputs like strings, objects, arrays, null, undefined, and Infinity, always returning a reliable number or 0 for invalid cases. Whether

Downloads

286

Readme

Why?

  • Bundle size
  • No dependencies
  • Light weighted
  • Typescript support
  • Works on both server and client
  • Works with vanila as well as all js frameworks

🚀 Features

  • Reliable Conversion: Safely handles values like null, undefined, objects, arrays, and more.
  • Finite Number Guarantee: Ensures that values like Infinity and large numbers beyond JavaScript’s safe integer range (2^53) are handled correctly.
  • Cross-Platform: Works seamlessly in both browser environments and Node.js.

✨ Benefits

✅ Input Validation

Ensures that data is valid for calculations and numeric operations, preventing application crashes or errors when dealing with unpredictable inputs.

Example

const value = safeNumber("123");  // Returns 123

🔒 Prevent Crashes

Avoids runtime errors caused by invalid or non-numeric data, ensuring your app doesn’t break due to bad input.

Example

const value = safeNumber(null);  // Returns 0

🎯 Finite Value Handling

Protects your app from issues with very large numbers or Infinity, which can disrupt calculations if not handled properly.

Example

const value = safeNumber(Infinity);  // Returns 0

🔧 Usage

npm install js-safe-number # yarn add js-safe-number

Then use in your project:

const safeNumber = require('js-safe-number').default;

// import safeNumber from 'js-safe-number'; // es6

const num = safeNumber("123abc");  // Returns 0
const safeNum = safeNumber(Infinity);  // Returns 0
// Test cases
console.log(safeNumber(10)); // 10
console.log(safeNumber("-10")); // -10
console.log(safeNumber(Infinity)); // 0
console.log(safeNumber(() => {})); // 0
console.log(safeNumber([])); // 0
console.log(safeNumber({})); // 0
console.log(safeNumber(null)); // 0
console.log(safeNumber(undefined)); // 0
console.log(safeNumber(true)); // 1
console.log(safeNumber(false)); // 0
console.log(safeNumber(Math.pow(2, 53))); // 0 (because it exceeds MAX_SAFE_INTEGER)

⚠️ Why You Need safeNumber:

  1. Invalid Input Handling 🤔:- Without this function, passing invalid or unexpected data types into number-based operations can cause unpredictable behavior (e.g., NaN, errors, or application crashes).
  2. Infinity and Large Numbers 🔢:- Unbounded numbers like Infinity or values beyond JavaScript’s safe integer range (2^53) can introduce bugs into your calculations if not properly constrained.
  3. Cross-Environment Consistency 🌍:- Whether in the browser or on the server (Node.js), safeNumber ensures that all numeric operations are predictable and error-free.

Links

📜 License

This module is open-sourced under the MIT License.