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

@agoric/nat

v4.1.0

Published

Ensures that a number is within the natural numbers (0, 1, 2...) or throws a RangeError

Downloads

822

Readme

Nat

Build Status dependency status dev dependency status License

Numbers in a programming language are meaningful because we take them to represent abstract mathematical numbers. JavaScript has two data types representing numbers, JS numbers (IEEE 64 bit floating point) and bigints (arbitrary precision integers). Not all abstact mathematical numbers are representable by these data types, and not all values of one of these data types represent mathematical numbers (The JS number type's NaN, Infinity, and -Infinity). Because JavaScript uses "number" to refer to its floating point data type, we'll always say "mathematical number" when that's what we mean.

This package is concerned with the mathematical natural numbers, the non-negative integers. All of these can be safely represented as bigints, given enough memory. Some of these can be represented as JS numbers, and a smaller set can safely be represented as JS numbers, given a specific notion of safety.

A skippable detail about floating point:

  • The JavaScript expression 2**70 evaluates to a JS number that exactly represents the mathematical number you expect. However, the JavaScript expression 2**70+1 === 2**70 evaluates to true because this JS number is outside the contiguous range of integers that the JS number type can represent unambiguously. The contiguous range of exactly representable integers is -(2**53) to 2**53. However, 2**53+1 === 2**53 is true, demonstrating that other integers will round to 2**53. The JavaScript standard defines the safe JS numbers to be the JS numbers that represent mathematical integers and lie in the range -(2**53-1) to 2**53-1 . The JS safe natural numbers are the non-negative subset of that, between 0 and 2**53-1. No other integers coerce to any of these. If in JavaScript a + b === c and all three values are JS safe integers, then this accurately represents the mathematical sum of the mathematical numbers they represent.

The bigint datatype, by contrast, is inherently safe. Every bigint >= 0n safely represents a natural number.

This package exports two functions, isNat(allegedNum) and Nat(allegedNum).

isNat(allegedNum: any) => boolean

isNat(3); // true
isNat(3n); // true
isNat('3'); // false
isNat(2**70); // false
isNat(2n**70n); // true
isNat(-3n); // false
isNat(3.1); // false

The isNat function is a predicate that accepts any input and returns true iff that input safely represents a natural number, i.e., if it is a non-negative bigint or it is a non-negative JS number safely representing an integer. To the extent that we consider this abstract notion of mathematical natural number a type, isNat is a type tester of possible representations of this type.

Nat(allegedNum: bigint | number) => bigint

Nat(3); // 3n
Nat(3n); // 3n
Nat('3'); // throws TypeError
Nat(2 ** 70); // throws RangeError
Nat(2n ** 70n); // 1180591620717411303424n
Nat(-3); // throws RangeError
Nat(3.1); // throws RangeError

The Nat function accepts exactly those values that pass the isNat predicate. For those it returns a bigint that represents the same natural number. Otherwise it throws.

Validators and Coercers

Functions like Nat and the standard JavaScript BigInt can be classified validators or coercers.

When a validator accepts---returns normally rather than throwing---the caller knows that their input argument is as expected, and the output is the same as the input. When a coercer accepts, the caller knows that the output is as expected, but only knows that the input was one the coercer was willing to convert from. The BigInt function is a coercer. It will even accept strings as input but its output is always a bigint. Nat is an interesting mixture. It is a coercer at one level of abstraction, and a validator at another level of abstraction.

At the level of concrete JavaScript data representations, Nat is clearly a coercer---Nat will convert a qualifying JS number into a bigint. At the level of abstraction of the mathematical number any accepted input represents, Nat is a validator. If Nat succeeds the caller knows that their input safely represents some abstract mathematical natural number, and that the output safely represents the same abstract mathematical natural number. At this level of abstraction, on success, the output is the same as the input.

History

Nat comes from the Google Caja project, which tested whether a JS number was a primitive integer within the range of continguously and unambiguously representable non-negative integers.

For more, see the discussion in TC39 notes