@@ -9,7 +9,7 @@ import {Interval} from "../index";
9
9
// import IntervalTree from '../dist/interval-tree.esm';
10
10
11
11
describe ( '#IntervalTree' , function ( ) {
12
- it ( 'Create new instanse of IntervalTree ' , function ( ) {
12
+ it ( 'Create new instance of IntervalTree ' , function ( ) {
13
13
let tree = new IntervalTree ( ) ;
14
14
expect ( tree ) . to . be . an . instanceof ( IntervalTree ) ;
15
15
} ) ;
@@ -310,4 +310,69 @@ describe('#IntervalTree', function() {
310
310
const resp2 = tree . search ( [ 4 , 11 ] ) ;
311
311
expect ( resp2 ) . to . deep . equal ( [ [ 5 , 7 ] , [ 7 , 8 ] ] ) ;
312
312
} )
313
+ describe ( '##Iterator' , function ( ) {
314
+ it ( 'May find first intersecting interval' , function ( ) {
315
+ let tree = new IntervalTree ( ) ;
316
+ let ints = [ [ 6 , 8 ] , [ 1 , 3 ] , [ 5 , 12 ] , [ 1 , 1 ] , [ 5 , 7 ] ] ;
317
+ for ( let int of ints ) tree . insert ( int ) ;
318
+ let iterator = tree . iterate ( [ 3 , 8 ] ) ;
319
+ expect ( iterator . next ( ) . value ) . to . deep . equal ( [ 1 , 3 ] ) ;
320
+ } ) ;
321
+ it ( 'May find first forward interval when there is no intersection' , function ( ) {
322
+ let tree = new IntervalTree ( ) ;
323
+ let ints = [ [ 6 , 8 ] , [ 1 , 3 ] , [ 5 , 12 ] , [ 1 , 1 ] , [ 5 , 7 ] ] ;
324
+ for ( let int of ints ) tree . insert ( int ) ;
325
+ let iterator = tree . iterate ( [ 4 , 4 ] ) ;
326
+ expect ( iterator . next ( ) . value ) . to . deep . equal ( [ 5 , 7 ] ) ;
327
+ } ) ;
328
+ it ( 'May find first interval when no interval is passed' , function ( ) {
329
+ let tree = new IntervalTree ( ) ;
330
+ let ints = [ [ 6 , 8 ] , [ 1 , 3 ] , [ 5 , 12 ] , [ 1 , 1 ] , [ 5 , 7 ] ] ;
331
+ for ( let int of ints ) tree . insert ( int ) ;
332
+ let iterator = tree . iterate ( ) ;
333
+ expect ( iterator . next ( ) . value ) . to . deep . equal ( [ 1 , 1 ] ) ;
334
+ } ) ;
335
+ it ( 'Supports iteration with `for .. of` syntax' , function ( ) {
336
+ let tree = new IntervalTree ( ) ;
337
+ let ints = [ [ 6 , 8 ] , [ 1 , 3 ] , [ 5 , 12 ] , [ 1 , 1 ] , [ 5 , 7 ] ] ;
338
+ for ( let int of ints ) tree . insert ( int ) ;
339
+ let results = [ ] ;
340
+ for ( let val of tree . iterate ( [ 4 , 4 ] ) ) {
341
+ results . push ( val ) ;
342
+ }
343
+ expect ( results ) . to . deep . equal ( [ [ 5 , 7 ] , [ 5 , 12 ] , [ 6 , 8 ] ] ) ;
344
+ } ) ;
345
+ it ( 'Returns `{done: true}` when it reaches the end' , function ( ) {
346
+ let tree = new IntervalTree ( ) ;
347
+ let ints = [ [ 6 , 8 ] , [ 1 , 3 ] , [ 5 , 12 ] , [ 1 , 1 ] , [ 5 , 7 ] ] ;
348
+ for ( let int of ints ) tree . insert ( int ) ;
349
+ let iterator = tree . iterate ( [ 8 , 8 ] ) ;
350
+ expect ( iterator . next ( ) . value ) . to . deep . equal ( [ 5 , 12 ] ) ;
351
+ expect ( iterator . next ( ) . value ) . to . deep . equal ( [ 6 , 8 ] ) ;
352
+ let res = iterator . next ( ) ;
353
+ expect ( res . value ) . to . equal ( undefined ) ;
354
+ expect ( res . done ) . to . equal ( true ) ;
355
+ } ) ;
356
+ it ( 'Supports custom transformed objects' , function ( ) {
357
+ const composers = [
358
+ { name : "Ludwig van Beethoven" , period : [ 1770 , 1827 ] } ,
359
+ { name : "Johann Sebastian Bach" , period : [ 1685 , 1750 ] } ,
360
+ { name : "Wolfgang Amadeus Mozart" , period : [ 1756 , 1791 ] } ,
361
+ { name : "Johannes Brahms" , period : [ 1833 , 1897 ] } ,
362
+ { name : "Richard Wagner" , period : [ 1813 , 1883 ] } ,
363
+ { name : "Claude Debussy" , period : [ 1862 , 1918 ] } ,
364
+ { name : "Pyotr Ilyich Tchaikovsky" , period : [ 1840 , 1893 ] } ,
365
+ { name : "Frédéric Chopin" , period : [ 1810 , 1849 ] } ,
366
+ { name : "Joseph Haydn" , period : [ 1732 , 1809 ] } ,
367
+ { name : "Antonio Vivaldi" , period : [ 1678 , 1741 ] }
368
+ ] ;
369
+ const tree = new IntervalTree ( ) ;
370
+ for ( let composer of composers )
371
+ tree . insert ( composer . period , composer . name ) ;
372
+ let iterator = tree . iterate ( [ 1600 , 1700 ] ,
373
+ ( name , period ) => { return `${ name } (${ period . low } -${ period . high } )` } ) ;
374
+ expect ( iterator . next ( ) . value ) . to . equal ( "Antonio Vivaldi (1678-1741)" ) ;
375
+ expect ( iterator . next ( ) . value ) . to . equal ( "Johann Sebastian Bach (1685-1750)" ) ;
376
+ } ) ;
377
+ } ) ;
313
378
} ) ;
0 commit comments