How to use jsbi - 10 common examples

To help you get started, we’ve selected a few jsbi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GoogleChromeLabs / babel-plugin-transform-jsbi-to-bigint / test / fixtures / short-import-specifier / input.js View on Github external
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// .
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import JSBI from 'jsbi';

const a = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
const b = JSBI.BigInt(12);
console.log(JSBI.add(a, b));
JSBI.BigInt('1');
JSBI.BigInt('34');
JSBI.BigInt('00034');

const JSBigInt = JSBI.BigInt;
JSBigInt(34);
const JSBigInt2 = JSBI['BigInt'];
JSBigInt2(56);
const JSBIadd = JSBI.add;
JSBIadd(7, 8);

JSBI.add(a, b);
JSBI.subtract(a, b);
JSBI.multiply(a, b);
JSBI.divide(a, b);
JSBI.remainder(a, b);
github dbusjs / mpris-service / src / interfaces / player.js View on Github external
SetPosition(trackId, position) {
    let e = {
      trackId: trackId,
      // XXX overflow
      position: JSBI.toNumber(position)
    };
    this.player.emit('position', e);
  }
github dbusjs / mpris-service / src / interfaces / player.js View on Github external
Seek(offset) {
    // XXX overflow
    offset = JSBI.toNumber(offset);
    this.player.emit('seek', offset);
  }
github amzn / ion-js / src / JsbiSerde.ts View on Github external
public static toUnsignedIntBytes(value: JSBI): Uint8Array {
        // Remove the sign if negative
        if (JsbiSupport.isNegative(value)) {
            value = JSBI.unaryMinus(value);
        }

        let sizeInBytes = this.getUnsignedIntSizeInBytes(value);
        let bytes = new Uint8Array(sizeInBytes);
        for (let m = sizeInBytes - 1; m >= 0; m--) {
            let lastByte = JSBI.toNumber(JSBI.bitwiseAnd(value, this.BYTE_MAX_VALUE));
            value = JSBI.signedRightShift(value, this.BITS_PER_BYTE);
            bytes[m] = lastByte;
        }

        return bytes;
    }
github amzn / ion-js / src / IonParserBinaryRaw.ts View on Github external
private load_value(): void {
        if (this._curr != undefined) return;   // current value is already loaded
        if (this.isNull()) return;
        switch (this._raw_type) {
            case IonBinary.TB_BOOL:
                break;
            case IonBinary.TB_INT:
                this._curr = this._readIntegerMagnitude();
                break;
            case IonBinary.TB_NEG_INT:
                this._curr = JSBI.unaryMinus(this._readIntegerMagnitude());
                break;
            case IonBinary.TB_FLOAT:
                this._curr = this.read_binary_float();
                break;
            case IonBinary.TB_DECIMAL:
                if (this._len === 0) {
                    this._curr = Decimal.ZERO;
                } else {
                    this._curr = this.read_decimal_value();
                }
                break;
            case IonBinary.TB_TIMESTAMP:
                this._curr = this.read_timestamp_value();
                break;
            case IonBinary.TB_SYMBOL:
                this._curr = this.readUnsignedIntAsNumber();
github amzn / ion-js / src / IonParserBinaryRaw.ts View on Github external
private static readDecimalValueFrom(input: BinarySpan, numberOfBytes: number): Decimal {
        // Decimal representations have two components: exponent (a VarInt) and coefficient (an Int).
        // The decimal’s value is: coefficient * 10 ^ exponent

        let initialPosition = input.position();

        let exponent: number = ParserBinaryRaw._readVarSignedIntFrom(input);
        let numberOfExponentBytes = input.position() - initialPosition;
        let numberOfCoefficientBytes = numberOfBytes - numberOfExponentBytes;

        let signedInt = ParserBinaryRaw._readSignedIntFrom(input, numberOfCoefficientBytes);
        let isNegative = signedInt.isNegative;
        let coefficient = isNegative ? JSBI.unaryMinus(signedInt.magnitude) : signedInt.magnitude;
        return Decimal._fromBigIntCoefficient(
            isNegative,
            coefficient,
            exponent
        );
    }
github amzn / ion-js / src / JsbiSerde.ts View on Github external
public static toUnsignedIntBytes(value: JSBI): Uint8Array {
        // Remove the sign if negative
        if (JsbiSupport.isNegative(value)) {
            value = JSBI.unaryMinus(value);
        }

        let sizeInBytes = this.getUnsignedIntSizeInBytes(value);
        let bytes = new Uint8Array(sizeInBytes);
        for (let m = sizeInBytes - 1; m >= 0; m--) {
            let lastByte = JSBI.toNumber(JSBI.bitwiseAnd(value, this.BYTE_MAX_VALUE));
            value = JSBI.signedRightShift(value, this.BITS_PER_BYTE);
            bytes[m] = lastByte;
        }

        return bytes;
    }
github amzn / ion-js / src / JsbiSerde.ts View on Github external
static getUnsignedIntSizeInBytes(value: JSBI): number {
        // TODO: Profile on real data sets to see if binary search is preferable to a low-to-high linear search.
        for (let m = 0; m < this.SIZE_THRESHOLDS.length; m++) {
            let threshold = this.SIZE_THRESHOLDS[m];
            if (JSBI.lessThanOrEqual(value, threshold)) {
                return m + 1;
            }
        }

        let sizeInBytes = this.SIZE_THRESHOLDS.length;
        let threshold: JSBI = this.calculateSizeThreshold(sizeInBytes);
        while (JSBI.greaterThan(value, threshold)) {
            // TODO: Make larger jumps, then refine the search.
            sizeInBytes++;
            threshold = this.calculateSizeThreshold(sizeInBytes);
        }

        return sizeInBytes;
    }
github amzn / ion-js / src / JsbiSerde.ts View on Github external
static getUnsignedIntSizeInBytes(value: JSBI): number {
        // TODO: Profile on real data sets to see if binary search is preferable to a low-to-high linear search.
        for (let m = 0; m < this.SIZE_THRESHOLDS.length; m++) {
            let threshold = this.SIZE_THRESHOLDS[m];
            if (JSBI.lessThanOrEqual(value, threshold)) {
                return m + 1;
            }
        }

        let sizeInBytes = this.SIZE_THRESHOLDS.length;
        let threshold: JSBI = this.calculateSizeThreshold(sizeInBytes);
        while (JSBI.greaterThan(value, threshold)) {
            // TODO: Make larger jumps, then refine the search.
            sizeInBytes++;
            threshold = this.calculateSizeThreshold(sizeInBytes);
        }

        return sizeInBytes;
    }
github amzn / ion-js / src / JsbiSerde.ts View on Github external
public static toUnsignedIntBytes(value: JSBI): Uint8Array {
        // Remove the sign if negative
        if (JsbiSupport.isNegative(value)) {
            value = JSBI.unaryMinus(value);
        }

        let sizeInBytes = this.getUnsignedIntSizeInBytes(value);
        let bytes = new Uint8Array(sizeInBytes);
        for (let m = sizeInBytes - 1; m >= 0; m--) {
            let lastByte = JSBI.toNumber(JSBI.bitwiseAnd(value, this.BYTE_MAX_VALUE));
            value = JSBI.signedRightShift(value, this.BITS_PER_BYTE);
            bytes[m] = lastByte;
        }

        return bytes;
    }

jsbi

JSBI is a pure-JavaScript implementation of [the ECMAScript BigInt proposal](https://tc39.es/proposal-bigint/), which officially became a part of the JavaScript language in ES2020.

Apache-2.0
Latest version published 3 years ago

Package Health Score

77 / 100
Full package analysis

Similar packages