/** * Tests if this Long's value is negative. * @returns {boolean} */ isNegative() { return this.value < BigInt(0) }
/** * Returns this Long with bits arithmetically shifted to the right by the given amount. * @param {number|bigint} numBits Number of bits * @returns {!Long} Shifted bigint */ shiftRight(numBits) { return new Long(this.value >> BigInt(numBits)) }
/** * Gets the high 32 bits as a signed integer. * @returns {number} Signed high bits */ getHighBits() { return Number(BigInt.asIntN(32, this.value >> BigInt(32))) }
/** * @param {string} str * @returns {!Long} * @inner */ static fromString(str) { if (str.length === 0) throw Error('empty string') if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity') return Long.ZERO return new Long(BigInt(str)) }
/** * @function * @param {bigint|number|string|Long} val * @returns {!Long} * @inner */ static fromValue(val) { if (typeof val === 'number') return this.fromNumber(val) if (typeof val === 'string') return this.fromString(val) if (typeof val === 'bigint') return new Long(val) if (this.isLong(val)) return new Long(BigInt(val.value)) return new Long(BigInt(val)) }
readInt64() { const first = this.buffer[this.offset] const last = this.buffer[this.offset + 7] const low = (first << 24) + // Overflow this.buffer[this.offset + 1] * 2 ** 16 + this.buffer[this.offset + 2] * 2 ** 8 + this.buffer[this.offset + 3] const high = this.buffer[this.offset + 4] * 2 ** 24 + this.buffer[this.offset + 5] * 2 ** 16 + this.buffer[this.offset + 6] * 2 ** 8 + last this.offset += INT64_SIZE return (BigInt(low) << 32n) + BigInt(high) }
/** * @param {number} value * @returns {!Long} * @inner */ static fromInt(value) { if (isNaN(value)) return Long.ZERO return new Long(BigInt.asIntN(64, BigInt(value))) }
describe(`toJson`, () => { const v = process.versions.node.split(`.`), highVer = +v[0], lowVer = +v[1]; if (highVer > 10 || (highVer === 10 && lowVer >= 4)) { // Node.js v10.4.0 is required to support BigInt natively. describe(`for a direct value`, () => { expect(internal.toJson(BigInt(`0`))).toBe(`0`); expect(internal.toJson(BigInt(`123`))).toBe(`123`); expect(internal.toJson(BigInt(`-12345678901234567890`))).toBe(`-12345678901234567890`); }); describe(`for an object`, () => { expect(internal.toJson({value: BigInt(`0`)})).toEqual(`{"value":0}`); expect(internal.toJson({value: BigInt(`123`)})).toEqual(`{"value":123}`); expect(internal.toJson({value: BigInt(`-456`)})).toEqual(`{"value":-456}`); const mix1 = { val1: 12345678901234567890, val2: BigInt(`12345678901234567890`) }; expect(internal.toJson(mix1)).toEqual(`{"val1":12345678901234567000,"val2":12345678901234567890}`); }); describe(`for an undefined`, () => { expect(internal.toJson()).toBeUndefined(); expect(internal.toJson(undefined)).toBeUndefined(); }); } });
describe('Other functions', () => { let input1, input2 beforeAll(() => { input1 = new Long(BigInt(5)) input2 = new Long(BigInt(13)) }) const zero = new Long(BigInt(0)) expect(zero.isZero()).toEqual(true) }) expect(new Long(BigInt(-15)).isNegative()).toEqual(true) expect(new Long(BigInt(2)).isNegative()).toEqual(false) })
/** * @param {number} value * @returns {!Long} * @inner */ static fromBits(value) { return new Long(BigInt(value)) }
/** * Returns this Long with bits logically shifted to the right by the given amount. * @param {number|bigint} numBits Number of bits * @returns {!Long} Shifted bigint */ shiftRightUnsigned(numBits) { return new Long(this.value >> BigInt.asUintN(64, BigInt(numBits))) }
/** * @param {number} value * @returns {!Long} * @inner */ static fromNumber(value) { if (isNaN(value)) return Long.ZERO return new Long(BigInt(value)) }
/** * Tests if this Long's value equals zero. * @returns {boolean} */ isZero() { return this.value === BigInt(0) }
/** * Returns this Long with bits shifted to the left by the given amount. * @param {number|bigint} numBits Number of bits * @returns {!Long} Shifted bigint */ shiftLeft(numBits) { return new Long(this.value << BigInt(numBits)) }