1- type ArraySliceProxyHandler < T > = {
2- get (
3- target : ArraySlice < T > ,
4- property : string | symbol ,
5- receiver : ArraySlice < T > ,
6- ) : unknown ;
7- } ;
8-
9- class ArraySlice < T > {
10- private constructor (
11- public readonly array : ReadonlyArray < T > ,
12- public readonly start : number ,
13- public readonly end : number ,
14- ) { }
15-
16- get length ( ) : number {
17- return this . end - this . start + 1 ;
18- }
19-
20- at ( index : number ) : T | undefined {
21- const adjustedIndex = index < 0 ? index + this . length : index ;
22- return this . array [ this . start + adjustedIndex ] ;
23- }
24-
25- * [ Symbol . iterator ] ( this : ArraySlice < T > ) : Generator < T , void , void > {
26- for ( let i = this . start ; i <= this . end ; ++ i ) {
27- yield this . array [ i ] ;
28- }
29- }
30-
31- slide ( delta : number = 1 ) : IndexableArraySlice < T > {
32- return ArraySlice . get ( this . array , this . start + delta , this . end + delta ) ;
33- }
34-
35- isPrefix ( ) : boolean {
36- return this . start === 0 ;
37- }
38-
39- isSuffix ( ) : boolean {
40- return this . end === this . array . length - 1 ;
41- }
42-
43- private declare static proxyHandler ?: ArraySliceProxyHandler < unknown > ;
44-
45- static get < T > (
46- array : ReadonlyArray < T > ,
47- start : number ,
48- end : number ,
49- ) : IndexableArraySlice < T > {
50- ArraySlice . proxyHandler ??= {
51- get (
52- target : ArraySlice < unknown > ,
53- property : string | symbol ,
54- receiver : ArraySlice < unknown > ,
55- ) : unknown {
56- if ( typeof property === "string" ) {
57- const index = parseInt ( property , 10 ) ;
58- if ( String ( index ) === property ) {
59- if ( index < 0 || index >= receiver . length ) {
60- return undefined ;
61- }
62- return receiver . at ( index ) ;
63- }
64- }
65-
66- return ( target as unknown as Record < string | symbol , unknown > ) [
67- property
68- ] ;
69- } ,
70- } ;
71-
72- return new Proxy (
73- new ArraySlice ( array , start , end ) ,
74- ArraySlice . proxyHandler as ArraySliceProxyHandler < T > ,
75- ) as IndexableArraySlice < T > ;
76- }
77- }
78-
79- type IndexableArraySlice < T > = ArraySlice < T > & {
80- [ index : number ] : T | undefined ;
81- } ;
1+ import { type IndexableArraySlice , ArraySlice } from "../ArraySlice" ;
822
833declare global {
844 interface ReadonlyArray < T > {
855 slidingWindows (
866 this : ReadonlyArray < T > ,
877 windowSize : number ,
88- ) : Generator < ArraySlice < T > , void , void > ;
8+ ) : Generator < IndexableArraySlice < T > , void , void > ;
899 }
9010
9111 interface Array < T > {
9212 slidingWindows (
9313 this : ReadonlyArray < T > ,
9414 windowSize : number ,
95- ) : Generator < ArraySlice < T > , void , void > ;
15+ ) : Generator < IndexableArraySlice < T > , void , void > ;
9616 }
9717}
9818
9919Array . prototype . slidingWindows = function * < T > (
10020 this : ReadonlyArray < T > ,
10121 windowSize : number ,
102- ) : Generator < ArraySlice < T > , void , void > {
22+ ) : Generator < IndexableArraySlice < T > , void , void > {
10323 for (
10424 let win : IndexableArraySlice < T > | null = ArraySlice . get (
10525 this ,
@@ -112,7 +32,3 @@ Array.prototype.slidingWindows = function* <T>(
11232 yield win ;
11333 }
11434} ;
115-
116- // Needed to fix the error "Augmentations for the global scope can only be directly nested in external modules or ambient module declarations. ts(2669)"
117- // See: https://stackoverflow.com/questions/57132428/augmentations-for-the-global-scope-can-only-be-directly-nested-in-external-modul
118- export { } ;
0 commit comments