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

strto

v1.0.0

Published

strto is a strict string-to-number conversion library

Downloads

6

Readme

strto.js

strto is a strict string-to-number conversion library that works in node and browsers. strto is tiny, adding ~300 bytes to your gzipped payload.

Usage

var strto = require("strto"); // or include strto.js in a <script> tag

Examples

var n = strto.safeint(stringifiedNumber, null);
if (n !== null) {
    // n is an integer number in the range [-9007199254740991, 9007199254740991]
}

// errval can be used as a default value when that fits your code
n = strto.inexactint(formval.trim(), 0);
// n is an integer number and it may or may not be outside of range
// [-9007199254740991, 9007199254740991] thus inexact, even +-Infinity, but never NaN.

// errval can be any value, if you dislike null
var nil = {toString: function() { bomb }, valueOf: function() { bomb }};
n = strto.finitefloat(formval.trim(), nil);
if (n !== nil) {
    // n is an integer or floating point number, anything except +-Infinity or NaN
}
n = strto.float(formval.trim(), nil);
if (n !== nil) {
    // n may be any JS number (integer, floating point, +-Infinity and NaN)
}

// errval can be NaN if you want that monster back
n = strto.float(formval.trim(), NaN);
// whatever

Exceptions

All functions throw exceptions if str is not a string or if errval is missing. This is to help you catch errors early in development. You're not supposed to try-catch these.

strto.safeint(str: string, errval: any): (number | errval)

strto.safeint converts a string that apart from digits may only contain an optional leading - (no ., +, whitespace or any other characters). Put another way, str must match /^-?[0-9]+$/. If such a string is possible to convert exactly to a JavaScript integer number, i.e. it fits within the range [-9007199254740991, 9007199254740991], then that number will be returned. In all other cases (be it out of range or invalid string characters), errval will be returned. The base is always 10. Negative zero (a floating point peculiarity) will be normalized to integer zero.

strto.inexactint(str: string, errval: any): (number | errval)

strto.inexactint is like strto.safeint except it does not require the return value to fit within the range [-9007199254740991, 9007199254740991]. This means that strto.inexactint("123456789123456789", null) returns a non-null value that is roughly (but not exactly) similar to 123456789123456789. It also means that strto.inexactint(new Array(400).join("1"), null) returns Infinity. It can however never return NaN or negative zero.

strto.finitefloat(str: string, errval: any): (number | errval)

strto.finitefloat converts a string in scientific notation format to a JavaScript number. This is the everyday floating point notation you're used to in JavaScript and base-10 integers are a subset of it. Valid examples are "+1", "-0", "15e4", "0.3", ".3", ".3e3", ".3e+3" and ".3e-3". The string can't contain whitespace or any other characters not belonging to the number. Put another way, str must match /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/. If such a string is possible to convert to a finite number, i.e. any number that isn't -Infinity, Infinity or NaN, then that number is returned. In all other cases, errval will be returned.

strto.float(str: string, errval: any): (number | errval)

strto.float is like strto.finitefloat except it does not require the return value to be finite, so -Infinity, Infinity and NaN are also possible return values. Put another way, str must match /^([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?|Infinity|-Infinity|NaN)$/.

Installation

Node

Install using npm

npm install strto
var strto = require("strto");

Browser

Install using npm

npm install strto
<script src="node_modules/strto/strto.min.js"></script>