@@ -252,17 +252,11 @@ export class Long extends BSONValue {
252
252
* @param str - The textual representation of the Long
253
253
* @param radix - The radix in which the text is written (2-36), defaults to 10
254
254
*/
255
- static validateStringCharacters ( str : string , radix ?: number ) : false | string {
255
+ private static validateStringCharacters ( str : string , radix ?: number ) : false | string {
256
256
radix = radix ?? 10 ;
257
-
258
- let regexInputString = '' ;
259
- if ( radix <= 10 ) {
260
- regexInputString = `[^-0-${ radix - 1 } +]` ;
261
- } else {
262
- const validCharRangeEnd = String . fromCharCode ( 'a' . charCodeAt ( 0 ) + ( radix - 11 ) ) ;
263
- regexInputString = `[^-0-9+(a-${ validCharRangeEnd } )]` ;
264
- }
265
- const regex = new RegExp ( regexInputString , 'i' ) ;
257
+ const validCharacters = '0123456789abcdefghijklmnopqrstuvwxyz' . slice ( 0 , radix ) ;
258
+ // regex is case insensitive and checks that each character within the string is one of the validCharacters
259
+ const regex = new RegExp ( `[^-+${ validCharacters } ]` , 'i' ) ;
266
260
return regex . test ( str ) ? false : str ;
267
261
}
268
262
@@ -273,16 +267,16 @@ export class Long extends BSONValue {
273
267
* - the string contains invalid characters for the given radix
274
268
* - the string contains whitespace
275
269
* @param str - The textual representation of the Long
270
+ * @param validateStringCharacters - Whether or not invalid characters should throw an error
276
271
* @param unsigned - Whether unsigned or not, defaults to signed
277
272
* @param radix - The radix in which the text is written (2-36), defaults to 10
278
- * @param throwsError - Whether or not throwing an error is permitted
279
273
* @returns The corresponding Long value
280
274
*/
281
- static fromStringHelper (
275
+ private static _fromString (
282
276
str : string ,
277
+ validateStringCharacters : boolean ,
283
278
unsigned ?: boolean ,
284
279
radix ?: number ,
285
- throwsError ?: boolean
286
280
) : Long {
287
281
if ( str . length === 0 ) throw new BSONError ( 'empty string' ) ;
288
282
if ( str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity' )
@@ -297,17 +291,17 @@ export class Long extends BSONValue {
297
291
298
292
if ( radix < 2 || 36 < radix ) throw new BSONError ( 'radix' ) ;
299
293
300
- if ( throwsError && ! Long . validateStringCharacters ( str , radix ) ) {
294
+ if ( validateStringCharacters && ! Long . validateStringCharacters ( str , radix ) ) {
301
295
throw new BSONError ( `Input: '${ str } ' contains invalid characters for radix: ${ radix } ` ) ;
302
296
}
303
- if ( throwsError && str . trim ( ) !== str ) {
304
- throw new BSONError ( `Input: '${ str } ' contains whitespace.` ) ;
297
+ if ( validateStringCharacters && str . trim ( ) !== str ) {
298
+ throw new BSONError ( `Input: '${ str } ' contains leading and/or trailing whitespace.` ) ;
305
299
}
306
300
307
301
let p ;
308
302
if ( ( p = str . indexOf ( '-' ) ) > 0 ) throw new BSONError ( 'interior hyphen' ) ;
309
303
else if ( p === 0 ) {
310
- return Long . fromStringHelper ( str . substring ( 1 ) , unsigned , radix , throwsError ) . neg ( ) ;
304
+ return Long . _fromString ( str . substring ( 1 ) , validateStringCharacters , unsigned , radix ) . neg ( ) ;
311
305
}
312
306
313
307
// Do several (8) digits each time through the loop, so as to
@@ -345,7 +339,7 @@ export class Long extends BSONValue {
345
339
// remove leading zeros (for later string comparison and to make math faster)
346
340
const cleanedStr = removeLeadingZeros ( str ) ;
347
341
// doing this check outside of recursive function so cleanedStr value is consistent
348
- const result = Long . fromStringHelper ( cleanedStr , unsigned , radix , true ) ;
342
+ const result = Long . _fromString ( cleanedStr , true , unsigned , radix ) ;
349
343
if ( result . toString ( radix ) . toLowerCase ( ) !== cleanedStr . toLowerCase ( ) ) {
350
344
throw new BSONError (
351
345
`Input: ${ str } is not representable as ${ result . unsigned ? 'an unsigned' : 'a signed' } 64-bit Long with radix: ${ radix } `
@@ -362,7 +356,7 @@ export class Long extends BSONValue {
362
356
* @returns The corresponding Long value
363
357
*/
364
358
static fromString ( str : string , unsigned ?: boolean , radix ?: number ) : Long {
365
- return Long . fromStringHelper ( str , unsigned , radix , false ) ;
359
+ return Long . _fromString ( str , false , unsigned , radix ) ;
366
360
}
367
361
368
362
/**
0 commit comments