Skip to content

Conversation

@bigmontz
Copy link
Contributor

@bigmontz bigmontz commented Jan 2, 2023

Given the scenario where:

const one = neo4j.int(1)
const two = neo4j.int(2)

The following operations was resulting in string concatenation:

console.log( one + two ) // '12'
console.log( 1 + one ) // '11'

console.log( one.add(two).toInt() ) // 3, which is correct
console.log( '1' + one ) // '11', correct

This is issue is solved by defining the method Integer.valueOf which defines the primitive type representation. The primitive representation of Integer is set as BigInt.

This way:

console.log( one + two ) // 3n
console.log( 1 + one ) // thrown an exception since we can not add bigint to number
console.log( 1n + one ) // 2n
console.log( -one ) // -1n
console.log( '1' + one ) // '11', correct

Given the scenarion where:

```javascript
const one = neo4j.int(1)
const two = neo4j.int(2)
```

The following operations was resulting in string concatenation:

```javascript
console.log( one + two ) // '12'
console.log( 1 + one ) // '11'

console.log( one.add(two).toInt() ) // 3, which is correct
console.log( '1' + one) // '11', correct
```

This is issue is solved by defining the method `Integer.valueOf` which defines the primitive type representation.
The primitive representation of `Integer` is set as `BigInt`.

This way:

```javascript
console.log (one + two ) // 3n
console.log ( 1 + one ) // thrown an exception since we can not add bigint to number
console.log ( 1n + one ) // 2n
console.log ( -one ) // -1n
console.log( '1' + one) // '11', correct
```
@bigmontz bigmontz merged commit f0646a0 into neo4j:5.0 Jan 3, 2023
@bigmontz bigmontz deleted the 5.x-primitive-type-conversion branch January 3, 2023 11:06
bigmontz added a commit to bigmontz/neo4j-javascript-driver that referenced this pull request Jan 9, 2023
Given the scenario where:

```javascript
const one = neo4j.int(1)
const two = neo4j.int(2)
```

The following operations was resulting in string concatenation:

```javascript
console.log( one + two ) // '12'
console.log( 1 + one ) // '11'

console.log( one.add(two).toInt() ) // 3, which is correct
console.log( '1' + one ) // '11', correct
```

This is issue is solved by defining the method `Integer.valueOf` which defines the primitive type representation. The primitive representation of `Integer` is set as `BigInt`.

This way:

```javascript
console.log( one + two ) // 3n
console.log( 1 + one ) // thrown an exception since we can not add bigint to number
console.log( 1n + one ) // 2n
console.log( -one ) // -1n
console.log( '1' + one ) // '11', correct
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants