Skip to content

Commit 08d36e0

Browse files
Craig MacomberCraig Macomber
authored andcommitted
Document lack of support for NaN in simpleComparator and extend simpleComparator tests
1 parent f527ebe commit 08d36e0

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

b+tree.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export declare function defaultComparator(a: DefaultComparable, b: DefaultCompar
2929
* Unlike defaultComparator, this comparator doesn't support mixed types correctly,
3030
* i.e. use it with `BTree<string>` or `BTree<number>` but not `BTree<string|number>`.
3131
*
32+
* NaN is not supported.
33+
*
3234
* Note: null is treated like 0 when compared with numbers or Date, but in general
3335
* null is not ordered with respect to strings (neither greater nor less), and
3436
* undefined is not ordered with other types.

b+tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function defaultComparator(a, b) {
6868
return Number.isNaN(b) ? 0 : -1;
6969
else if (Number.isNaN(b))
7070
return 1;
71-
// This could be two objects (e.g. [7] and ['7']), that aren't greater or less
71+
// This could be two objects (e.g. [7] and ['7']) that aren't ordered
7272
return Array.isArray(a) ? 0 : Number.NaN;
7373
}
7474
exports.defaultComparator = defaultComparator;

b+tree.test.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ describe('defaultComparator', () =>
4848
testComparison(defaultComparator, sorted, values, [[dateA, dateA2], [0, -0], [[1], ['1']]]);
4949
});
5050

51-
describe('compareFiniteNumbers', () =>
51+
describe('simpleComparator with non-NaN numbers and null', () =>
5252
{
53-
const sorted = [-Infinity, -10, -1, -0, 0, 1, 2, 10, Infinity];
54-
testComparison(simpleComparator, sorted, sorted, [[-0, 0]]);
53+
const sorted = [-Infinity, -10, -1, -0, 0, null, 1, 2, 10, Infinity];
54+
testComparison<number | null>(simpleComparator, sorted, sorted, [[-0, 0], [-0, null], [0, null]]);
5555
});
5656

57-
describe('compareStrings', () =>
57+
describe('simpleComparator with strings', () =>
5858
{
5959
const values = [
6060
'24x',
@@ -68,15 +68,40 @@ describe('compareStrings', () =>
6868
'10',
6969
"NaN",
7070
];;
71-
testComparison(simpleComparator, [], values, []);
71+
testComparison<string>(simpleComparator, [], values, []);
72+
});
73+
74+
describe('simpleComparator with Date', () =>
75+
{
76+
const dateA = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
77+
const dateA2 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
78+
const dateB = new Date(Date.UTC(96, 1, 2, 3, 4, 6));
79+
const values = [
80+
dateA,
81+
dateA2,
82+
dateB,
83+
null,
84+
];
85+
testComparison<Date>(simpleComparator, [], values, [[dateA, dateA2]]);
86+
});
87+
88+
describe('simpleComparator arrays', () =>
89+
{
90+
const values = [
91+
[],
92+
[1],
93+
['1'],
94+
[2],
95+
];
96+
testComparison<(number|string)[] >(simpleComparator, [], values, [[[1], ['1']]]);
7297
});
7398

7499
/**
75100
* Tests a comparison function, ensuring it produces a strict partial order over the provided values.
76101
* Additionally confirms that the comparison function has the correct definition of equality via expectedDuplicates.
77102
*/
78-
function testComparison(comparison: (a: any, b: any) => number, inOrder: any[], values: any[], expectedDuplicates: [any, any][] = []) {
79-
function compare(a: any, b: any): number {
103+
function testComparison<T>(comparison: (a: T, b: T) => number, inOrder: T[], values: T[], expectedDuplicates: [T, T][] = []) {
104+
function compare(a: T, b: T): number {
80105
const v = comparison(a, b);
81106
expect(typeof v).toEqual('number');
82107
if (v !== v)

b+tree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export function defaultComparator(a: DefaultComparable, b: DefaultComparable): n
102102
* Unlike defaultComparator, this comparator doesn't support mixed types correctly,
103103
* i.e. use it with `BTree<string>` or `BTree<number>` but not `BTree<string|number>`.
104104
*
105+
* NaN is not supported.
106+
*
105107
* Note: null is treated like 0 when compared with numbers or Date, but in general
106108
* null is not ordered with respect to strings (neither greater nor less), and
107109
* undefined is not ordered with other types.

0 commit comments

Comments
 (0)