File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -567,6 +567,13 @@ export class CompactSelection {
567
567
return CompactSelection . empty ( ) . add ( selection ) ;
568
568
} ;
569
569
570
+ static fromArray = ( items : readonly number [ ] ) : CompactSelection => {
571
+ if ( items . length === 0 ) return CompactSelection . empty ( ) ;
572
+ const slices = items . map ( s => [ s , s + 1 ] as Slice ) ;
573
+ const newItems = mergeRanges ( slices ) ;
574
+ return new CompactSelection ( newItems ) ;
575
+ } ;
576
+
570
577
public offset ( amount : number ) : CompactSelection {
571
578
if ( amount === 0 ) return this ;
572
579
const newItems = this . items . map ( x => [ x [ 0 ] + amount , x [ 1 ] + amount ] as Slice ) ;
@@ -575,9 +582,7 @@ export class CompactSelection {
575
582
576
583
public add ( selection : number | Slice ) : CompactSelection {
577
584
const slice : Slice = typeof selection === "number" ? [ selection , selection + 1 ] : selection ;
578
-
579
585
const newItems = mergeRanges ( [ ...this . items , slice ] ) ;
580
-
581
586
return new CompactSelection ( newItems ) ;
582
587
}
583
588
Original file line number Diff line number Diff line change @@ -79,6 +79,17 @@ describe("data-grid-types", () => {
79
79
expect ( sel . toArray ( ) ) . toEqual ( [ 3 , 4 ] ) ;
80
80
} ) ;
81
81
82
+ test ( "Compact selection fromArray" , ( ) => {
83
+ const sel = CompactSelection . fromArray ( [ 3 , 4 , 6 ] ) ;
84
+ expect ( [ ...sel ] ) . toEqual ( [ 3 , 4 , 6 ] ) ;
85
+ } ) ;
86
+
87
+ test ( "Compact selection array roundtrip" , ( ) => {
88
+ const sel = CompactSelection . fromSingleSelection ( [ 2 , 7 ] ) . add ( 8 ) ;
89
+ const altSel = CompactSelection . fromArray ( sel . toArray ( ) ) ;
90
+ expect ( altSel . equals ( sel ) ) . toBe ( true ) ;
91
+ } ) ;
92
+
82
93
test ( "Smoke test compact selection remove" , ( ) => {
83
94
const sel = CompactSelection . fromSingleSelection ( [ 3 , 8 ] ) ;
84
95
You can’t perform that action at this time.
0 commit comments