@agoric/nat

中文文档
4.1.0发布于 2021/06/10Apache-2.0ESM + CJS零依赖近 30 天下载 4956

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

原始使用文档
文档目录4 节
  1. isNat(allegedNum: any) => boolean
  2. Nat(allegedNum: bigint | number) => bigint
  3. Validators and Coercers
  4. History

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

版本与发布记录

@agoric/nat 共发布 8 个版本,首次发布于 2019/02/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.0latest2021/06/105 年前
4.0.02021/01/305 年前
3.0.12019/12/056 年前
3.0.02019/12/046 年前
2.0.12019/02/287 年前
2.0.02019/02/137 年前

关于 @agoric/nat 的常见问题

@agoric/nat 是什么?

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

如何安装 @agoric/nat?

在项目目录运行 npm install @agoric/nat,或使用 pnpm add / yarn add / bun add。

@agoric/nat 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/agoric__nat 是否存在。

@agoric/nat 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@agoric/nat 可以商用吗?

@agoric/nat 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@agoric/nat 的下载量有多少?

根据 npm 官方下载统计,@agoric/nat 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,956 次,日均约 165 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agoric/nat 还在维护吗?

最新版本 4.1.0 发布于 2021/06/10(5 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/02/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。