GithubHelp home page GithubHelp logo

agoricbot / nat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from agoric/nat

0.0 1.0 0.0 357 KB

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

License: Apache License 2.0

JavaScript 74.86% HTML 10.72% Shell 14.42%

nat's Introduction

Nat

Build Status dependency status dev dependency status License

Nat(value) returns its argument if it is a non-negative BigInt instance. If the argument is negative, an ordinary Number, or some other non-BigInt type, an exception is thrown. This makes it easy to use Nat() on incoming arguments, or as an assertion on generated values. You can think of Nat() as a type enforcement.

BigInts can accurately represent arbitrarily large integers without concern of rounding or loss of precision. Two Nats added together will always produce another Nat. To create a BigInt, you can append n to the end of an integer literal (3n) or by call the function BigInt(). Note that as of 12/5/2019, BigInt (and therefore Nat) is at Stage 4 in the standardization process and is not yet supported in Edge, Internet Explorer, or Safari.

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);
  ...
}

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:

const myOldBal = 12n; // BigInt numeric literal
const amount = 3n;
Nat(myOldBal + amount);

const withdrawalAmount = 2n;
// balances cannot be negative
const newBal = Nat(myOldBal - withdrawalAmount);

Non-monetary usage

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

const a = ['hello', 'my', 'name', 'is'];
function add(index, value) {
  a[Nat(index)] = value;
}
add(4n, 'alice'); // works
add(2.5, 'bob'); // throws rather than add a key named "2.5"
a // [ 'hello', 'my', 'name', 'is', 'alice' ]

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

Bounds

Because Nat uses JavaScript's upcoming BigInt standard, the range of accurately-representable integers is 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

nat's People

Contributors

katelynsills avatar agoricbot avatar michaelfig avatar warner avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.