diff --git a/packages/core/src/integer.ts b/packages/core/src/integer.ts index 458871e78..43536a245 100644 --- a/packages/core/src/integer.ts +++ b/packages/core/src/integer.ts @@ -197,6 +197,21 @@ class Integer { } } + /** + * Converts the Integer to it primitive value. + * + * @since 5.4.0 + * @returns {bigint} + * + * @see {@link Integer#toBigInt} + * @see {@link Integer#toInt} + * @see {@link Integer#toNumber} + * @see {@link Integer#toString} + */ + valueOf (): bigint { + return this.toBigInt() + } + /** * Gets the high 32 bits as a signed integer. * @returns {number} Signed high bits diff --git a/packages/core/test/integer.test.ts b/packages/core/test/integer.test.ts index bacefcccd..f3bc8a3d8 100644 --- a/packages/core/test/integer.test.ts +++ b/packages/core/test/integer.test.ts @@ -370,6 +370,97 @@ describe('Integer', () => { fc.assert(fc.property(fc.integer(), i => i.toString() === int(i).toString())) }) + test('Integer.valueOf should be equivalent to the Integer.toBigInt', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + num => int(num).toBigInt() === int(num).valueOf() + ) + ) + }) + + test('Integer should concatenate with a string', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + num => 'string' + int(num) + 'str' === 'string' + int(num).toString() + 'str' + ) + ) + }) + + test('Integer should be able to be used in the string interpolation', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + num => `string${int(num)}str` === 'string' + int(num).toString() + 'str' + ) + ) + }) + + test('Integer should be able to use + operator as bigint', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + (num1, num2) => + // @ts-expect-error + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + num1 + int(num2) === num1 + num2 && + // @ts-expect-error + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + int(num1) + num2 === num1 + num2 && + // @ts-expect-error + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + int(num1) + int(num2) === num1 + num2 + )) + }) + + test('Integer should be able to use - operator as bigint', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + (num1, num2) => + // @ts-expect-error + num1 - int(num2) === num1 - num2 && + // @ts-expect-error + int(num1) - num2 === num1 - num2 && + // @ts-expect-error + int(num1) - int(num2) === num1 - num2 + )) + }) + + test('Integer should be able to use * operator as bigint', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + (num1, num2) => + // @ts-expect-error + num1 * int(num2) === num1 * num2 && + // @ts-expect-error + int(num1) * num2 === num1 * num2 && + // @ts-expect-error + int(num1) * int(num2) === num1 * num2 + )) + }) + + test('Integer should be able to use / operator as bigint', () => { + fc.assert( + fc.property( + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }), + fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }).filter(n => n !== BigInt(0)), + (num1, num2) => + // @ts-expect-error + num1 / int(num2) === num1 / num2 && + // @ts-expect-error + int(num1) / num2 === num1 / num2 && + // @ts-expect-error + int(num1) / int(num2) === num1 / num2 + )) + }) + test('int(string) should match int(Integer)', () => { fc.assert(fc.property(fc.integer(), i => int(i).equals(int(i.toString())))) }) diff --git a/packages/neo4j-driver-deno/lib/core/integer.ts b/packages/neo4j-driver-deno/lib/core/integer.ts index 3f70960ed..13105c988 100644 --- a/packages/neo4j-driver-deno/lib/core/integer.ts +++ b/packages/neo4j-driver-deno/lib/core/integer.ts @@ -197,6 +197,21 @@ class Integer { } } + /** + * Converts the Integer to it primitive value. + * + * @since 5.4.0 + * @returns {bigint} + * + * @see {@link Integer#toBigInt} + * @see {@link Integer#toInt} + * @see {@link Integer#toNumber} + * @see {@link Integer#toString} + */ + valueOf (): bigint { + return this.toBigInt() + } + /** * Gets the high 32 bits as a signed integer. * @returns {number} Signed high bits diff --git a/packages/neo4j-driver/test/internal/routing-table.test.js b/packages/neo4j-driver/test/internal/routing-table.test.js index d88ba12cf..a58c0f503 100644 --- a/packages/neo4j-driver/test/internal/routing-table.test.js +++ b/packages/neo4j-driver/test/internal/routing-table.test.js @@ -265,7 +265,7 @@ describe('#unit RoutingTable', () => { })) it('should return Integer.MAX_VALUE as expirationTime when ttl overflowed', async () => { - const ttl = int(Integer.MAX_VALUE - 2) + const ttl = int(Integer.MAX_VALUE - 2n) const routers = ['router:7699'] const readers = ['reader1:7699', 'reader2:7699'] const writers = ['writer1:7693', 'writer2:7692', 'writer3:7629']