@@ -4,17 +4,21 @@ export function count<T>(col: Iterable<T>): number {
44 if ( typeof ( col as any ) [ "System.Collections.Generic.ICollection`1.get_Count" ] === "function" ) {
55 return ( col as any ) [ "System.Collections.Generic.ICollection`1.get_Count" ] ( ) ; // collection
66 } else {
7- if ( isArrayLike ( col ) ) {
8- return col . length ; // resize array
7+ if ( typeof ( col as any ) [ "System.Collections.Generic.IReadOnlyCollection`1.get_Count" ] === "function" ) {
8+ return ( col as any ) [ "System.Collections.Generic.IReadOnlyCollection`1.get_Count" ] ( ) ; // collection
99 } else {
10- if ( typeof ( col as any ) . size === "number" ) {
11- return ( col as any ) . size ; // map, set
10+ if ( isArrayLike ( col ) ) {
11+ return col . length ; // array, resize array
1212 } else {
13- let count = 0 ;
14- for ( const _ of col ) {
15- count ++ ;
13+ if ( typeof ( col as any ) . size === "number" ) {
14+ return ( col as any ) . size ; // map, set
15+ } else {
16+ let count = 0 ;
17+ for ( const _ of col ) {
18+ count ++ ;
19+ }
20+ return count ; // other collections
1621 }
17- return count ;
1822 }
1923 }
2024 }
@@ -24,7 +28,16 @@ export function isReadOnly<T>(col: Iterable<T>): boolean {
2428 if ( typeof ( col as any ) [ "System.Collections.Generic.ICollection`1.get_IsReadOnly" ] === "function" ) {
2529 return ( col as any ) [ "System.Collections.Generic.ICollection`1.get_IsReadOnly" ] ( ) ; // collection
2630 } else {
27- return false ;
31+ if ( isArrayLike ( col ) ) {
32+ return ArrayBuffer . isView ( col ) ; // true for typed arrays, false for other arrays
33+ } else {
34+ if ( typeof ( col as any ) . size === "number" ) {
35+ return false ; // map, set
36+ } else {
37+ return true ; // other collections
38+ }
39+ }
40+
2841 }
2942}
3043
@@ -45,7 +58,7 @@ export function contains<T>(col: Iterable<T>, item: T): boolean {
4558 return ( col as any ) [ "System.Collections.Generic.ICollection`1.Contains2B595" ] ( item ) ; // collection
4659 } else {
4760 if ( isArrayLike ( col ) ) {
48- let i = col . findIndex ( x => equals ( x , item ) ) ; // resize array
61+ let i = col . findIndex ( x => equals ( x , item ) ) ; // array, resize array
4962 return i >= 0 ;
5063 } else {
5164 if ( typeof ( col as any ) . has === "function" ) {
@@ -55,7 +68,7 @@ export function contains<T>(col: Iterable<T>, item: T): boolean {
5568 return ( col as any ) . has ( item ) ; // set
5669 }
5770 } else {
58- return false ; // unknown collection
71+ return false ; // other collections
5972 }
6073 }
6174 }
@@ -66,7 +79,11 @@ export function add<T>(col: Iterable<T>, item: T): void {
6679 return ( col as any ) [ "System.Collections.Generic.ICollection`1.Add2B595" ] ( item ) ; // collection
6780 } else {
6881 if ( isArrayLike ( col ) ) {
69- col . push ( item ) ; // resize array
82+ if ( ArrayBuffer . isView ( col ) ) {
83+ // TODO: throw for typed arrays?
84+ } else {
85+ col . push ( item ) ; // array, resize array
86+ }
7087 } else {
7188 if ( typeof ( col as any ) . add === "function" ) {
7289 return ( col as any ) . add ( item ) ; // set
@@ -80,7 +97,7 @@ export function add<T>(col: Iterable<T>, item: T): void {
8097 throw new Error ( "An item with the same key has already been added. Key: " + item [ 0 ] ) ;
8198 }
8299 } else {
83- // unknown collection
100+ // TODO: throw for other collections?
84101 }
85102 }
86103 }
@@ -92,12 +109,17 @@ export function remove<T>(col: Iterable<T>, item: T): boolean {
92109 return ( col as any ) [ "System.Collections.Generic.ICollection`1.Remove2B595" ] ( item ) ; // collection
93110 } else {
94111 if ( isArrayLike ( col ) ) {
95- let i = col . findIndex ( x => equals ( x , item ) ) ;
96- if ( i >= 0 ) {
97- col . splice ( i , 1 ) ; // resize array
98- return true ;
99- } else {
112+ if ( ArrayBuffer . isView ( col ) ) {
113+ // TODO: throw for typed arrays
100114 return false ;
115+ } else {
116+ let i = col . findIndex ( x => equals ( x , item ) ) ;
117+ if ( i >= 0 ) {
118+ col . splice ( i , 1 ) ; // array, resize array
119+ return true ;
120+ } else {
121+ return false ;
122+ }
101123 }
102124 } else {
103125 if ( typeof ( col as any ) . delete === "function" ) {
@@ -111,7 +133,8 @@ export function remove<T>(col: Iterable<T>, item: T): boolean {
111133 return ( col as any ) . delete ( item ) ; // set
112134 }
113135 } else {
114- return false ; // unknown collection
136+ // TODO: throw for other collections?
137+ return false ; // other collections
115138 }
116139 }
117140 }
@@ -122,12 +145,16 @@ export function clear<T>(col: Iterable<T>): void {
122145 return ( col as any ) [ "System.Collections.Generic.ICollection`1.Clear" ] ( ) ; // collection
123146 } else {
124147 if ( isArrayLike ( col ) ) {
125- col . splice ( 0 ) ; // resize array
148+ if ( ArrayBuffer . isView ( col ) ) {
149+ // TODO: throw for typed arrays?
150+ } else {
151+ col . splice ( 0 ) ; // array, resize array
152+ }
126153 } else {
127154 if ( typeof ( col as any ) . clear === "function" ) {
128155 ( col as any ) . clear ( ) ; // map, set
129156 } else {
130- // unknown collection
157+ // TODO: throw for other collections?
131158 }
132159 }
133160 }
0 commit comments