Skip to content

Commit 31e8c88

Browse files
Explicit explaining the Integer range.
Better explanation of the Neo4j Integer range and the range that JavaScript can safely express as an integer.
1 parent 486d4ec commit 31e8c88

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,10 @@ rxSession
421421

422422
### Numbers and the Integer type
423423

424-
The Neo4j type system includes 64-bit integer values.
425-
However, JavaScript can only safely represent integers between `-(2`<sup>`53`</sup>`- 1)` and `(2`<sup>`53`</sup>`- 1)`.
424+
The Neo4j type system uses 64-bit signed integer values. The range of values is between `-(2`<sup>`64`</sup>`- 1)` and `(2`<sup>`63`</sup>`- 1)`.
425+
426+
However, JavaScript can only safely represent integers between `Number.MIN_SAFE_INTEGER` `-(2`<sup>`53`</sup>`- 1)` and `Number.MAX_SAFE_INTEGER` `(2`<sup>`53`</sup>`- 1)`.
427+
426428
In order to support the full Neo4j type system, the driver will not automatically convert to javascript integers.
427429
Any time the driver receives an integer value from Neo4j, it will be represented with an internal integer type by the driver.
428430

@@ -431,6 +433,7 @@ _**Any javascript number value passed as a parameter will be recognized as `Floa
431433
#### Writing integers
432434

433435
Numbers written directly e.g. `session.run("CREATE (n:Node {age: $age})", {age: 22})` will be of type `Float` in Neo4j.
436+
434437
To write the `age` as an integer the `neo4j.int` method should be used:
435438

436439
```javascript
@@ -439,7 +442,7 @@ var neo4j = require('neo4j-driver')
439442
session.run('CREATE (n {age: $myIntParam})', { myIntParam: neo4j.int(22) })
440443
```
441444

442-
To write integers larger than can be represented as JavaScript numbers, use a string argument to `neo4j.int`:
445+
To write an integer value that are not within the range of `Number.MIN_SAFE_INTEGER` `-(2`<sup>`53`</sup>`- 1)` and `Number.MAX_SAFE_INTEGER` `(2`<sup>`53`</sup>`- 1)`, use a string argument to `neo4j.int`:
443446

444447
```javascript
445448
session.run('CREATE (n {age: $myIntParam})', {
@@ -449,22 +452,25 @@ session.run('CREATE (n {age: $myIntParam})', {
449452

450453
#### Reading integers
451454

452-
Since Integers can be larger than can be represented as JavaScript numbers, it is only safe to convert to JavaScript numbers if you know that they will not exceed `(2`<sup>`53`</sup>`- 1)` in size.
455+
In Neo4j, the type Integer can be larger what can be represented safely as an integer with JavaScript Number.
456+
457+
It is only safe to convert to a JavaScript Number if you know that the number will be in the range `Number.MIN_SAFE_INTEGER` `-(2`<sup>`53`</sup>`- 1)` and `Number.MAX_SAFE_INTEGER` `(2`<sup>`53`</sup>`- 1)`.
458+
453459
In order to facilitate working with integers the driver include `neo4j.isInt`, `neo4j.integer.inSafeRange`, `neo4j.integer.toNumber`, and `neo4j.integer.toString`.
454460

455461
```javascript
456-
var aSmallInteger = neo4j.int(123)
457-
if (neo4j.integer.inSafeRange(aSmallInteger)) {
458-
var aNumber = aSmallInteger.toNumber()
462+
var smallInteger = neo4j.int(123)
463+
if (neo4j.integer.inSafeRange(smallInteger)) {
464+
var aNumber = smallInteger.toNumber()
459465
}
460466
```
461467

462-
If you will be handling integers larger than that, you should convert them to strings:
468+
If you will be handling integers that is not within the JavaScript safe range of integers, you should convert the value to a string:
463469

464470
```javascript
465-
var aLargerInteger = neo4j.int('9223372036854775807')
466-
if (!neo4j.integer.inSafeRange(aLargerInteger)) {
467-
var integerAsString = aLargerInteger.toString()
471+
var largeInteger = neo4j.int('9223372036854775807')
472+
if (!neo4j.integer.inSafeRange(largeInteger)) {
473+
var integerAsString = largeInteger.toString()
468474
}
469475
```
470476

0 commit comments

Comments
 (0)