@@ -151,6 +151,37 @@ it("bisector(comparator).right(array, value) handles large sparse d3", () => {
151151 assert . strictEqual ( bisectRight ( boxes , box ( 6 ) , i - 5 , i ) , i - 0 ) ;
152152} ) ;
153153
154+ it ( "bisector(comparator).left(array, value) supports an asymmetric (object, value) comparator" , ( ) => {
155+ const boxes = [ 1 , 2 , 3 ] . map ( box ) ;
156+ const bisectLeft = bisector ( ascendingBoxValue ) . left ;
157+ assert . strictEqual ( bisectLeft ( boxes , 1 ) , 0 ) ;
158+ assert . strictEqual ( bisectLeft ( boxes , 2 ) , 1 ) ;
159+ assert . strictEqual ( bisectLeft ( boxes , 3 ) , 2 ) ;
160+ } ) ;
161+
162+ // This is not possible because the bisector has no way of knowing whether the
163+ // given comparator is symmetric or asymmetric, and if the comparator is
164+ // asymmetric it cannot be used to test the search value for orderability.
165+ it . skip ( "bisector(comparator).left(array, value) keeps non-comparable values to the right" , ( ) => {
166+ const boxes = [ 1 , 2 , null , undefined , NaN ] . map ( box ) ;
167+ const bisectLeft = bisector ( ascendingBox ) . left ;
168+ assert . strictEqual ( bisectLeft ( boxes , box ( 1 ) ) , 0 ) ;
169+ assert . strictEqual ( bisectLeft ( boxes , box ( 2 ) ) , 1 ) ;
170+ assert . strictEqual ( bisectLeft ( boxes , box ( null ) ) , 5 ) ;
171+ assert . strictEqual ( bisectLeft ( boxes , box ( undefined ) ) , 5 ) ;
172+ assert . strictEqual ( bisectLeft ( boxes , box ( NaN ) ) , 5 ) ;
173+ } ) ;
174+
175+ it ( "bisector(accessor).left(array, value) keeps non-comparable values to the right" , ( ) => {
176+ const boxes = [ 1 , 2 , null , undefined , NaN ] . map ( box ) ;
177+ const bisectLeft = bisector ( unbox ) . left ;
178+ assert . strictEqual ( bisectLeft ( boxes , 1 ) , 0 ) ;
179+ assert . strictEqual ( bisectLeft ( boxes , 2 ) , 1 ) ;
180+ assert . strictEqual ( bisectLeft ( boxes , null ) , 5 ) ;
181+ assert . strictEqual ( bisectLeft ( boxes , undefined ) , 5 ) ;
182+ assert . strictEqual ( bisectLeft ( boxes , NaN ) , 5 ) ;
183+ } ) ;
184+
154185it ( "bisector(accessor).left(array, value) returns the index of an exact match" , ( ) => {
155186 const boxes = [ 1 , 2 , 3 ] . map ( box ) ;
156187 const bisectLeft = bisector ( unbox ) . left ;
@@ -346,3 +377,7 @@ function unbox(box) {
346377function ascendingBox ( a , b ) {
347378 return ascending ( a . value , b . value ) ;
348379}
380+
381+ function ascendingBoxValue ( a , value ) {
382+ return ascending ( a . value , value ) ;
383+ }
0 commit comments