Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@agoric/nat

Package Overview
Dependencies
Maintainers
3
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@agoric/nat

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

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
79
decreased by-15.05%
Maintainers
3
Weekly downloads
 
Created
Source

Nat

Build Status dependency status dev dependency status License

Nat(value) returns its argument if it represents a non-negative integer (i.e. a "natural number") that can be accurately represented in a JavaScript Number, specifically (0, 1, 2... to 2 ** 53 - 1). Otherwise it throws a RangeError exception. This makes it easy to use on incoming arguments, or as an assertion on generated values.

Traditional JavaScript has a single Number type, which is defined to contain a 64-bit IEEE-754 floating point value. This can safely represent a wide range of integers, but if they get too large, Number will lose precision: 2**53 + 1 will give you the same value as 2**53 + 2. In situations where you care about accuracy rather than range, this would be a problem.

You can think of Nat() as a type enforcement.

How to use

Nat() can be used to enforce desired properties on account balances, where precision is important.

For instance, in a deposit scenario, you would want to defend against someone "depositing" a negative value. Use Nat to validate the amount to be deposited before proceeding:

deposit: function(amount) {
  amount = Nat(amount);
  ...
}

We also want to use Nat() before using values internally, as a precondition check:

Nat(ledger.get(purse));

Any addition or subtraction expressions dealing with monetary amounts should protected with Nat() to guard against overflow/underflow errors. Without this check, the two balances might both be safe, but their sum might be too large to represent accurately, causing precision errors in subsequent computation:

Nat(myOldBal + amount);
const srcNewBal = Nat(srcOldBal - amount);

Non-monetary usage

Array indexes can be wrapped with Nat(), to guard against the surprising string coersion of non-integral index values:

const a = [2,4,6]
function add(index, value) {
  a[Nat(index)] = value;
}
add(3, 8); // works
add(2.5, 7); // throws rather than add a key named "2.5"

Nat can be used even in cases where it is not strictly necessary, for extra protection against human error.

Bounds

By excluding 2^53, we have the nice invariant that if

Nat(a),
Nat(b),
Nat(a+b),

are all true, then (a+b) is an accurate sum of a and b.

Future versions of Nat will use JavaScript's upcoming BigInt standard, to increase the range of accurately-representable integers to be effectively unbounded.

History

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

For more, see the discussion in TC39 notes

Keywords

FAQs

Package last updated on 27 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc