@@ -359,6 +359,81 @@ describe(LookupByPath.prototype.deleteItem.name, () => {
359
359
} ) ;
360
360
} ) ;
361
361
362
+ describe ( LookupByPath . prototype . deleteSubtree . name , ( ) => {
363
+ it ( 'returns false for an empty tree' , ( ) => {
364
+ expect ( new LookupByPath ( ) . deleteSubtree ( 'foo' ) ) . toEqual ( false ) ;
365
+ } ) ;
366
+
367
+ it ( 'deletes the matching node in a trivial tree' , ( ) => {
368
+ const tree = new LookupByPath ( [ [ 'foo' , 1 ] ] ) ;
369
+ expect ( tree . deleteSubtree ( 'foo' ) ) . toEqual ( true ) ;
370
+ expect ( tree . size ) . toEqual ( 0 ) ;
371
+ expect ( tree . get ( 'foo' ) ) . toEqual ( undefined ) ;
372
+ } ) ;
373
+
374
+ it ( 'returns false for non-matching paths in a single-layer tree' , ( ) => {
375
+ const tree : LookupByPath < number > = new LookupByPath ( [
376
+ [ 'foo' , 1 ] ,
377
+ [ 'bar' , 2 ] ,
378
+ [ 'baz' , 3 ]
379
+ ] ) ;
380
+
381
+ expect ( tree . deleteSubtree ( 'buzz' ) ) . toEqual ( false ) ;
382
+ expect ( tree . size ) . toEqual ( 3 ) ;
383
+ } ) ;
384
+
385
+ it ( 'deletes the matching node in a single-layer tree' , ( ) => {
386
+ const tree : LookupByPath < number > = new LookupByPath ( [
387
+ [ 'foo' , 1 ] ,
388
+ [ 'bar' , 2 ] ,
389
+ [ 'baz' , 3 ]
390
+ ] ) ;
391
+
392
+ expect ( tree . deleteSubtree ( 'bar' ) ) . toEqual ( true ) ;
393
+ expect ( tree . size ) . toEqual ( 2 ) ;
394
+ expect ( tree . get ( 'bar' ) ) . toEqual ( undefined ) ;
395
+ } ) ;
396
+
397
+ it ( 'deletes the matching subtree in a multi-layer tree' , ( ) => {
398
+ const tree : LookupByPath < number > = new LookupByPath ( [
399
+ [ 'foo' , 1 ] ,
400
+ [ 'foo/bar' , 2 ] ,
401
+ [ 'foo/bar/baz' , 3 ]
402
+ ] ) ;
403
+
404
+ expect ( tree . deleteSubtree ( 'foo/bar' ) ) . toEqual ( true ) ;
405
+ expect ( tree . size ) . toEqual ( 1 ) ;
406
+ expect ( tree . get ( 'foo/bar' ) ) . toEqual ( undefined ) ;
407
+ expect ( tree . get ( 'foo/bar/baz' ) ) . toEqual ( undefined ) ; // child nodes are deleted
408
+ } ) ;
409
+
410
+ it ( 'returns false for non-matching paths in a multi-layer tree' , ( ) => {
411
+ const tree : LookupByPath < number > = new LookupByPath ( [
412
+ [ 'foo' , 1 ] ,
413
+ [ 'foo/bar' , 2 ] ,
414
+ [ 'foo/bar/baz' , 3 ]
415
+ ] ) ;
416
+
417
+ expect ( tree . deleteSubtree ( 'foo/baz' ) ) . toEqual ( false ) ;
418
+ expect ( tree . size ) . toEqual ( 3 ) ;
419
+ } ) ;
420
+
421
+ it ( 'handles custom delimiters' , ( ) => {
422
+ const tree : LookupByPath < number > = new LookupByPath (
423
+ [
424
+ [ 'foo,bar' , 1 ] ,
425
+ [ 'foo,bar,baz' , 2 ]
426
+ ] ,
427
+ ','
428
+ ) ;
429
+
430
+ expect ( tree . deleteSubtree ( 'foo\0bar' , '\0' ) ) . toEqual ( true ) ;
431
+ expect ( tree . size ) . toEqual ( 0 ) ;
432
+ expect ( tree . get ( 'foo\0bar' , '\0' ) ) . toEqual ( undefined ) ;
433
+ expect ( tree . get ( 'foo\0bar\0baz' , '\0' ) ) . toEqual ( undefined ) ; // child nodes are deleted
434
+ } ) ;
435
+ } ) ;
436
+
362
437
describe ( LookupByPath . prototype . findChildPath . name , ( ) => {
363
438
it ( 'returns empty for an empty tree' , ( ) => {
364
439
expect ( new LookupByPath ( ) . findChildPath ( 'foo' ) ) . toEqual ( undefined ) ;
0 commit comments