@@ -13,7 +13,7 @@ var __extends = (this && this.__extends) || (function () {
13
13
} ;
14
14
} ) ( ) ;
15
15
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
16
- exports . EmptyBTree = exports . compareFiniteNumbersOrStringOrArray = exports . compareStrings = exports . compareFiniteNumbers = exports . defaultComparator = void 0 ;
16
+ exports . EmptyBTree = exports . compareStrings = exports . compareFiniteNumbers = exports . defaultComparator = void 0 ;
17
17
/**
18
18
* Compares DefaultComparables to form a strict partial ordering.
19
19
*
@@ -24,9 +24,17 @@ exports.EmptyBTree = exports.compareFiniteNumbersOrStringOrArray = exports.compa
24
24
* Two objects with equal valueOf compare the same, but compare unequal to primitives that have the same value.
25
25
*/
26
26
function defaultComparator ( a , b ) {
27
- // Compare types first.
27
+ // Special case finite numbers first for performance .
28
28
// Note that the trick of using 'a - b' the checking for NaN to detect non numbers values does not work if the strings are numeric (ex: "5"),
29
29
// leading most comparison functions using that approach to fail to have transitivity.
30
+ if ( Number . isFinite ( a ) && Number . isFinite ( b ) ) {
31
+ // Does not partially order NaNs or infinite values, but thats fine since they can't reach here.
32
+ // This will handle -0 and 0 as equal.
33
+ return a - b ;
34
+ }
35
+ // Compare types and order values of different types by type.
36
+ // This prevents implicit conversion of strings to numbers from causing invaliding ordering,
37
+ // and generally simplifies which cases need to be considered below.
30
38
var ta = typeof a ;
31
39
var tb = typeof b ;
32
40
if ( ta !== tb ) {
@@ -82,21 +90,6 @@ function compareStrings(a, b) {
82
90
}
83
91
exports . compareStrings = compareStrings ;
84
92
;
85
- /**
86
- * If a and b are arrays, they are compared using '<' and '>', which may cause unexpected equality, for example [1] will be considered equal to ['1'].
87
- */
88
- function compareFiniteNumbersOrStringOrArray ( a , b ) {
89
- // Strings can not be ordered relative to numbers using '<' and '>' since no matter the order, the comparison will return false.
90
- var ta = typeof a ;
91
- var tb = typeof b ;
92
- if ( ta !== tb ) {
93
- return ta < tb ? - 1 : 1 ;
94
- }
95
- // Use < and > instead of < and === so arrays work correctly.
96
- return a > b ? 1 : a < b ? - 1 : 0 ;
97
- }
98
- exports . compareFiniteNumbersOrStringOrArray = compareFiniteNumbersOrStringOrArray ;
99
- ;
100
93
/**
101
94
* A reasonably fast collection of key-value pairs with a powerful API.
102
95
* Largely compatible with the standard Map. BTree is a B+ tree data structure,
0 commit comments