@@ -371,26 +371,58 @@ export class Long extends BSONValue {
371
371
372
372
/**
373
373
* Returns a signed Long representation of the given string, written using radix 10.
374
+ *
375
+ * If the input string is empty, this function will throw a BSONError.
376
+ *
377
+ * If input string does not have valid signed 64-bit Long representation, this method will return a coerced value:
378
+ * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively
379
+ * - 'NaN' or '+/-Infinity' are coerced to Long.ZERO
380
+ * - other invalid characters sequences have variable behavior
381
+ *
374
382
* @param str - The textual representation of the Long
375
383
* @returns The corresponding Long value
376
384
*/
377
385
static fromString ( str : string ) : Long ;
378
386
/**
379
- * Returns a signed Long representation of the given string, written using radix 10.
387
+ * Returns a signed Long representation of the given string, written using the provided radix.
388
+ *
389
+ * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError.
390
+ *
391
+ * If input parameters do not have valid signed 64-bit Long representation, this method will return a coerced value:
392
+ * - inputs that overflow 64-bit signed long will be coerced to Long.MAX_VALUE and Long.MIN_VALUE respectively
393
+ * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO
394
+ * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
395
+ * - other invalid characters sequences have variable behavior
380
396
* @param str - The textual representation of the Long
381
397
* @param radix - The radix in which the text is written (2-36), defaults to 10
382
398
* @returns The corresponding Long value
383
399
*/
384
400
static fromString ( str : string , radix ?: number ) : Long ;
385
401
/**
386
402
* Returns a Long representation of the given string, written using radix 10.
403
+ *
404
+ * If the input string is empty, this function will throw a BSONError.
405
+ *
406
+ * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value:
407
+ * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values
408
+ * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO
409
+ * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
410
+ * - other invalid characters sequences have variable behavior
387
411
* @param str - The textual representation of the Long
388
412
* @param unsigned - Whether unsigned or not, defaults to signed
389
413
* @returns The corresponding Long value
390
414
*/
391
415
static fromString ( str : string , unsigned ?: boolean ) : Long ;
392
416
/**
393
417
* Returns a Long representation of the given string, written using the specified radix.
418
+ *
419
+ * If the input string is empty or a provided radix is not within (2-36), this function will throw a BSONError.
420
+ *
421
+ * If input parameters do not have a valid 64-bit Long representation, this method will return a coerced value:
422
+ * - inputs that overflow 64-bit long will be coerced to max or min (if signed) values
423
+ * - if the radix is less than 24, 'NaN' is coerced to Long.ZERO
424
+ * - if the radix is less than 35, '+/-Infinity' inputs are coerced to Long.ZERO
425
+ * - other invalid characters sequences have variable behavior
394
426
* @param str - The textual representation of the Long
395
427
* @param unsigned - Whether unsigned or not, defaults to signed
396
428
* @param radix - The radix in which the text is written (2-36), defaults to 10
@@ -406,7 +438,11 @@ export class Long extends BSONValue {
406
438
unsigned = ! ! unsignedOrRadix ;
407
439
}
408
440
radix ??= 10 ;
409
- if ( str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity' ) {
441
+ if ( str === 'NaN' && radix < 24 ) {
442
+ // radix does not support n, so coerce to zero
443
+ return Long . ZERO ;
444
+ } else if ( ( str === 'Infinity' || str === '+Infinity' || str === '-Infinity' ) && radix < 35 ) {
445
+ // radix does not support y, so coerce to zero
410
446
return Long . ZERO ;
411
447
}
412
448
return Long . _fromString ( str , unsigned , radix ) ;
0 commit comments