Skip to content

Commit fc3eea9

Browse files
committed
expose static integer functions
Expose `inSafeRange`, `toNumber`, and `toString` in order to facilitate integer handling.
1 parent f207842 commit fc3eea9

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

src/v1/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
import {int, isInt} from './integer';
20+
import {int, isInt, inSafeRange, toNumber, toString} from './integer';
2121
import {Node, Relationship, UnboundRelationship, PathSegment, Path} from './graph-types'
2222
import {Neo4jError, SERVICE_UNAVAILABLE, SESSION_EXPIRED} from './error';
2323
import Result from './result';
@@ -138,11 +138,17 @@ const error = {
138138
SERVICE_UNAVAILABLE,
139139
SESSION_EXPIRED
140140
};
141+
const integer = {
142+
toNumber,
143+
toString,
144+
inSafeRange
145+
};
141146

142147
const forExport = {
143148
driver,
144149
int,
145150
isInt,
151+
integer,
146152
Neo4jError,
147153
auth,
148154
types,
@@ -154,6 +160,7 @@ export {
154160
driver,
155161
int,
156162
isInt,
163+
integer,
157164
Neo4jError,
158165
auth,
159166
types,

src/v1/integer.js

+80-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Integer {
6767
// Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
6868
// methods on which they depend.
6969

70+
71+
inSafeRange() {return this.greaterThanOrEqual(Integer.MIN_SAFE_VALUE) && this.lessThanOrEqual(Integer.MAX_SAFE_VALUE)}
7072
/**
7173
* Converts the Integer to an exact javascript Number, assuming it is a 32 bit integer.
7274
* @returns {number}
@@ -718,6 +720,41 @@ Integer.fromValue = function(val) {
718720
return new Integer(val.low, val.high);
719721
};
720722

723+
/**
724+
* Converts the specified value to a number.
725+
* @access private
726+
* @param {!Integer|number|string|!{low: number, high: number}} val Value
727+
* @returns {number}
728+
* @expose
729+
*/
730+
Integer.toNumber = function(val) {
731+
return Integer.fromValue(val).toNumber();
732+
};
733+
734+
/**
735+
* Converts the specified value to a string.
736+
* @access private
737+
* @param {!Integer|number|string|!{low: number, high: number}} val Value
738+
* @param {number} radix optional radix for string conversion, defaults to 10
739+
* @returns {String}
740+
* @expose
741+
*/
742+
Integer.toString = function(val, radix) {
743+
return Integer.fromValue(val).toString(radix)
744+
};
745+
746+
/**
747+
* Checks if the given value is in the safe range in order to be converted to a native number
748+
* @access private
749+
* @param {!Integer|number|string|!{low: number, high: number}} val Value
750+
* @param {number} radix optional radix for string conversion, defaults to 10
751+
* @returns {boolean}
752+
* @expose
753+
*/
754+
Integer.inSafeRange = function(val) {
755+
return Integer.fromValue(val).inSafeRange();
756+
};
757+
721758
/**
722759
* @type {number}
723760
* @const
@@ -801,6 +838,20 @@ Integer.MAX_VALUE = Integer.fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
801838
*/
802839
Integer.MIN_VALUE = Integer.fromBits(0, 0x80000000|0, false);
803840

841+
/**
842+
* Minimum safe value.
843+
* @type {!Integer}
844+
* @private
845+
*/
846+
Integer.MIN_SAFE_VALUE = Integer.fromNumber(Number.MIN_SAFE_INTEGER);
847+
848+
/**
849+
* Maximum safe value.
850+
* @type {!Integer}
851+
* @private
852+
*/
853+
Integer.MAX_SAFE_VALUE = Integer.fromNumber(Number.MAX_SAFE_INTEGER);
854+
804855
/**
805856
* Cast value to Integer type.
806857
* @access public
@@ -812,14 +863,42 @@ let int = Integer.fromValue;
812863
/**
813864
* Check if a variable is of Integer type.
814865
* @access public
815-
* @param {Mixed} value - The varaible to check.
866+
* @param {Mixed} value - The variable to check.
816867
* @return {Boolean} - Is it of the Integer type?
817868
*/
818869
let isInt = Integer.isInteger;
819870

871+
/**
872+
* Check if a variable can be safely converted to a number
873+
* @access public
874+
* @param {Mixed} value - The variable to check
875+
* @return {Boolean} - true if it is safe to call toNumber on variable otherwise false
876+
*/
877+
let inSafeRange = Integer.inSafeRange;
878+
879+
/**
880+
* Converts a variable to a number
881+
* @access public
882+
* @param {Mixed} value - The variable to convert
883+
* @return {number} - the variable as a number
884+
*/
885+
let toNumber = Integer.toNumber;
886+
887+
/**
888+
* Converts the integer to a string representation
889+
* @access public
890+
* @param {Mixed} value - The variable to convert
891+
* @param {number} radix - radix to use in string conversion, defaults to 10
892+
* @return {String} - returns a string representation of the integer
893+
*/
894+
let toString = Integer.toString;
895+
820896
export {
821897
int,
822898
isInt,
899+
inSafeRange,
900+
toNumber,
901+
toString
823902
}
824903

825904
export default Integer

test/v1/integer.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
var v1 = require('../../lib/v1');
21+
var int = v1.int;
22+
var integer = v1.integer;
23+
24+
describe('Pool', function() {
25+
it('exposes inSafeRange function', function () {
26+
expect(integer.inSafeRange(int("9007199254740991"))).toBeTruthy();
27+
expect(integer.inSafeRange(int("9007199254740992"))).toBeFalsy();
28+
expect(integer.inSafeRange(int("-9007199254740991"))).toBeTruthy();
29+
expect(integer.inSafeRange(int("-9007199254740992"))).toBeFalsy();
30+
});
31+
32+
it('exposes toNumber function', function () {
33+
expect(integer.toNumber(int("9007199254740991"))).toEqual(9007199254740991);
34+
expect(integer.toNumber(int("-9007199254740991"))).toEqual(-9007199254740991);
35+
});
36+
37+
it('exposes toString function', function () {
38+
expect(integer.toString(int("9007199254740991"))).toEqual("9007199254740991");
39+
expect(integer.toString(int("9007199254740992"))).toEqual("9007199254740992");
40+
expect(integer.toString(int("-9007199254740991"))).toEqual("-9007199254740991");
41+
expect(integer.toString(int("-9007199254740992"))).toEqual("-9007199254740992");
42+
});
43+
44+
});

0 commit comments

Comments
 (0)