@@ -19,7 +19,25 @@ import * as sinon from 'sinon';
1919import { Datastore } from '../src' ;
2020import { Entity , entity , ValueProto } from '../src/entity' ;
2121import { IntegerTypeCastOptions } from '../src/query' ;
22- import { AnyARecord } from 'dns' ;
22+
23+ export function outOfBoundsError ( opts : {
24+ propertyName ?: string ;
25+ integerValue : string | number ;
26+ } ) {
27+ return new Error (
28+ 'We attempted to return all of the numeric values, but ' +
29+ ( opts . propertyName ? opts . propertyName + ' ' : '' ) +
30+ 'value ' +
31+ opts . integerValue +
32+ " is out of bounds of 'Number.MAX_SAFE_INTEGER'.\n" +
33+ "To prevent this error, please consider passing 'options.wrapNumbers=true' or\n" +
34+ "'options.wrapNumbers' as\n" +
35+ '{\n' +
36+ ' integerTypeCastFunction: provide <your_custom_function>\n' +
37+ ' properties: optionally specify property name(s) to be custom casted\n' +
38+ '}\n'
39+ ) ;
40+ }
2341
2442describe ( 'entity' , ( ) => {
2543 let entity : Entity ;
@@ -101,24 +119,6 @@ describe('entity', () => {
101119 } ) ;
102120
103121 describe ( 'integerTypeCastFunction is not provided' , ( ) => {
104- const expectedError = ( opts : {
105- integerValue ?: number ;
106- propertyName ?: string ;
107- } ) => {
108- return new Error (
109- 'We attempted to return all of the numeric values, but ' +
110- ( opts . propertyName ? opts . propertyName + ' ' : '' ) +
111- 'value ' +
112- opts . integerValue +
113- " is out of bounds of 'Number.MAX_SAFE_INTEGER'.\n" +
114- "To prevent this error, please consider passing 'options.wrapNumbers=true' or\n" +
115- "'options.wrapNumbers' as\n" +
116- '{\n' +
117- ' integerTypeCastFunction: provide <your_custom_function>\n' +
118- ' properties: optionally specify property name(s) to be cutom casted' +
119- '}\n'
120- ) ;
121- } ;
122122 it ( 'should throw if integerTypeCastOptions is provided but integerTypeCastFunction is not' , ( ) => {
123123 assert . throws (
124124 ( ) => new entity . Int ( valueProto , { } ) . valueOf ( ) ,
@@ -142,11 +142,11 @@ describe('entity', () => {
142142
143143 assert . throws ( ( ) => {
144144 new entity . Int ( valueProto ) . valueOf ( ) ;
145- } , expectedError ( valueProto ) ) ;
145+ } , outOfBoundsError ( valueProto ) ) ;
146146
147147 assert . throws ( ( ) => {
148148 new entity . Int ( valueProto2 ) . valueOf ( ) ;
149- } , expectedError ( valueProto2 ) ) ;
149+ } , outOfBoundsError ( valueProto2 ) ) ;
150150 } ) ;
151151
152152 it ( 'should throw if integer value is outside of bounds passing strings or Numbers' , ( ) => {
@@ -156,12 +156,12 @@ describe('entity', () => {
156156 // should throw when Number is passed
157157 assert . throws ( ( ) => {
158158 new entity . Int ( largeIntegerValue ) . valueOf ( ) ;
159- } , expectedError ( { integerValue : largeIntegerValue } ) ) ;
159+ } , outOfBoundsError ( { integerValue : largeIntegerValue } ) ) ;
160160
161161 // should throw when string is passed
162162 assert . throws ( ( ) => {
163163 new entity . Int ( smallIntegerValue . toString ( ) ) . valueOf ( ) ;
164- } , expectedError ( { integerValue : smallIntegerValue } ) ) ;
164+ } , outOfBoundsError ( { integerValue : smallIntegerValue } ) ) ;
165165 } ) ;
166166
167167 it ( 'should not auto throw on initialization' , ( ) => {
@@ -559,24 +559,6 @@ describe('entity', () => {
559559 } ) ;
560560
561561 it ( 'should throw if integer value is outside of bounds' , ( ) => {
562- const expectedError = ( opts : {
563- integerValue : number ;
564- propertyName : string ;
565- } ) => {
566- return new Error (
567- 'We attempted to return all of the numeric values, but ' +
568- ( opts . propertyName ? opts . propertyName + ' ' : '' ) +
569- 'value ' +
570- opts . integerValue +
571- " is out of bounds of 'Number.MAX_SAFE_INTEGER'.\n" +
572- "To prevent this error, please consider passing 'options.wrapNumbers=true' or\n" +
573- "'options.wrapNumbers' as\n" +
574- '{\n' +
575- ' integerTypeCastFunction: provide <your_custom_function>\n' +
576- ' properties: optionally specify property name(s) to be cutom casted' +
577- '}\n'
578- ) ;
579- } ;
580562 const largeIntegerValue = Number . MAX_SAFE_INTEGER + 1 ;
581563 const smallIntegerValue = Number . MIN_SAFE_INTEGER - 1 ;
582564
@@ -594,11 +576,11 @@ describe('entity', () => {
594576
595577 assert . throws ( ( ) => {
596578 entity . decodeValueProto ( valueProto ) ;
597- } , expectedError ( valueProto ) ) ;
579+ } , outOfBoundsError ( valueProto ) ) ;
598580
599581 assert . throws ( ( ) => {
600582 entity . decodeValueProto ( valueProto2 ) ;
601- } , expectedError ( valueProto2 ) ) ;
583+ } , outOfBoundsError ( valueProto2 ) ) ;
602584 } ) ;
603585 } ) ;
604586
0 commit comments