项目作者: JoshuaWise

项目描述 :
Native 64-bit integers with overflow protection.
高级语言: JavaScript
项目地址: git://github.com/JoshuaWise/integer.git
创建时间: 2017-05-31T15:27:58Z
项目社区:https://github.com/JoshuaWise/integer

开源协议:MIT License

下载


integer Build Status

Native 64-bit signed integers in Node.js.

  • All standard operators (arithmetic, bitwise, logical)
  • Protection from overflow and unsafe numbers
  • Always immutable
  • Other useful utilities

Installation

  1. npm install --save integer

You must be using Node.js v10 or above. Prebuilt binaries are available for LTS versions + Linux/OSX.

Usage

  1. var Integer = require('integer');
  2. var a = Integer('7129837312139827189');
  3. var b = a.subtract(1).shiftRight(3);
  4. assert(b.equals('891229664017478398'));

Overflow protection

We will not let you perform operations that would result in overflow. If you try to create an Integer that cannot be represented in 64-bits (signed), we will throw a RangeError.

  1. // These will each throw a RangeError
  2. var tooBig = Integer(13897283129).multiply(13897283129);
  3. var tooSmall = Integer.MIN_VALUE.subtract(1);
  4. var divideByZero = Integer(123).divide(0);
  5. var alsoTooBig = Integer('4029384203948203948923');
  6. // You are also protected against two's complement overflow (this will throw a RangeError)
  7. var twosComplement = Integer.MIN_VALUE.divide(-1);

Unsafe number protection

It’s easy to convert between me and regular JavaScript numbers.

  1. var int = Integer(12345);
  2. assert(int instanceof Integer);
  3. var num = Number(int); // same as int.toNumber()
  4. assert(typeof num === 'number');

However, we will prevent you from converting an Integer to an unsafe number, and vice-versa. To learn more about unsafe numbers, click here.

  1. // This will throw a RangeError
  2. var unsafe = Integer(Number.MAX_SAFE_INTEGER + 1);
  3. // This is okay
  4. var int = Integer(Number.MAX_SAFE_INTEGER).plus(1);
  5. // But this will throw a RangeError
  6. var unsafe = int.toNumber();

API

Integer(value) -> Integer

Casts a value to an Integer. If the value cannot be converted safely and losslessly, a RangeError is thrown.

  1. var a = Integer();
  2. var b = Integer(12345);
  3. var c = Integer('12345');
  4. assert(a.equals(0));
  5. assert(b.equals(c));

Integer.fromNumber(number, [defaultValue]) -> Integer

Casts a regular number to an Integer.

If the number is unsafe the defaultValue is used instead (or a RangeError is thrown if no defaultValue was provided).

  1. Integer.fromNumber(12345, 0); // results in Integer(12345)
  2. Integer.fromNumber(Number.MAX_SAFE_INTEGER + 1, 0); // results in Integer(0)

Integer.fromString(string, [radix, [defaultValue]]) -> Integer

Casts a string to an Integer. The string is assumed to be base-10 unless a different radix is specified.

If conversions fails the defaultValue is used instead (or a RangeError is thrown if no defaultValue was provided).

  1. var hexColor = 'ff55dd';
  2. var int = Integer.fromString(hexColor, 16, 'ffffff');

Integer.fromBits(lowBits, [highBits]) -> Integer

Creates an Integer by concatenating two regular 32-bit signed integers. The highBits are optional and default to 0.

  1. var int = Integer.fromBits(0x40, 0x20);
  2. int.toString(16); // => '2000000040'

Arithmetic operations

.add/plus(other) -> Integer

.subtract/sub/minus(other) -> Integer

.multiply/mul/times(other) -> Integer

.divide/div/divideBy/dividedBy/over(other) -> Integer

.modulo/mod(other) -> Integer

Performs the arithmetic operation and returns a new Integer. The argument must either be a number, a base-10 string, or an Integer. If the operation results in overflow, a RangeError is thrown.

.negate/neg() -> Integer

Returns the unary negation (-value) of the Integer.

.abs/absoluteValue() -> Integer

Returns the absolute value of the Integer.

Bitwise operations

.and(other) -> Integer

.or(other) -> Integer

.xor(other) -> Integer

.not() -> Integer

Performs the bitwise operation and returns a new Integer. The argument must either be a number, a base-10 string, or an Integer.

.shiftLeft/shl(numberOfBits) -> Integer

.shiftRight/shr(numberOfBits) -> Integer

Shifts the Integer by specified number of bits and returns the result.

Logical operations

.equals/eq/isEqualTo(other) -> boolean

.notEquals/neq/isNotEqualTo/doesNotEqual(other) -> boolean

.greaterThan/gt/isGreaterThan(other) -> boolean

.lessThan/lt/isLessThan(other) -> boolean

.greaterThanOrEquals/gte/isGreaterThanOrEqualTo(other) -> boolean

.lessThanOrEquals/lte/isLessThanOrEqualTo(other) -> boolean

Performs the logical operation and returns true or false. The argument must either be a number, a base-10 string, or an Integer.

.compare(other) -> number

Compares the value of the Integer and other, resulting in:

  • -1 if this is less than other
  • 1 if this is greater than other
  • 0 if this is equal to other

Converting to other values

.toString([radix]) -> string

Converts the Integer to a string. A base-10 string is returned unless a different radix is specified.

.valueOf/toNumber() -> number

Converts the Integer to a regular number. If the Integer is not within the safe range, a RangeError is thrown.

.toNumberUnsafe() -> number

Converts the Integer to a regular number, even if the conversion would result in a loss of precision. This method will never throw an error.

Other utility

.bitSizeAbs() -> number

Returns the number of bits necessary to hold the absolute value of the Integer.

  1. Integer(0).bitSizeAbs(); // => 1
  2. Integer(128).bitSizeAbs(); // => 8
  3. Integer(-255).bitSizeAbs(); // => 8
  4. Integer.fromString('4fffffffffff', 16).bitSizeAbs(); // => 47

.isEven() -> boolean

.isOdd() -> boolean

.isPositive() -> boolean

.isNegative() -> boolean

.isZero() -> boolean

.isNonZero/isNotZero() -> boolean

These methods are self-explanatory.

.isSafe() -> boolean

.isUnsafe() -> boolean

Returns whether or not the Integer is within the safe range. If it’s not within the safe range, trying to convert it to a regular number would result in a RangeError being thrown.

The safe range is defined as n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER.

Integer.isInstance(value) -> boolean

Determines if the given value is an Integer object.

Getters

  • .low -> number - the lower 32-bits of the Integer
  • .high -> number - the upper 32-bits of the Integer

Constants

  • Integer.MAX_VALUE - maximum value of an Integer
  • Integer.MIN_VALUE - minimum value of an Integer
  • Integer.ZERO - an Integer with a value of 0
  • Integer.ONE - an Integer with a value of 1
  • Integer.NEG_ONE - an Integer with a value of -1

License

MIT