Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,105 +1,25 @@
type ArraySliceProxyHandler<T> = {
get(
target: ArraySlice<T>,
property: string | symbol,
receiver: ArraySlice<T>,
): unknown;
};

class ArraySlice<T> {
private constructor(
public readonly array: ReadonlyArray<T>,
public readonly start: number,
public readonly end: number,
) {}

get length(): number {
return this.end - this.start + 1;
}

at(index: number): T | undefined {
const adjustedIndex = index < 0 ? index + this.length : index;
return this.array[this.start + adjustedIndex];
}

*[Symbol.iterator](this: ArraySlice<T>): Generator<T, void, void> {
for (let i = this.start; i <= this.end; ++i) {
yield this.array[i];
}
}

slide(delta: number = 1): IndexableArraySlice<T> {
return ArraySlice.get(this.array, this.start + delta, this.end + delta);
}

isPrefix(): boolean {
return this.start === 0;
}

isSuffix(): boolean {
return this.end === this.array.length - 1;
}

private declare static proxyHandler?: ArraySliceProxyHandler<unknown>;

static get<T>(
array: ReadonlyArray<T>,
start: number,
end: number,
): IndexableArraySlice<T> {
ArraySlice.proxyHandler ??= {
get(
target: ArraySlice<unknown>,
property: string | symbol,
receiver: ArraySlice<unknown>,
): unknown {
if (typeof property === "string") {
const index = parseInt(property, 10);
if (String(index) === property) {
if (index < 0 || index >= receiver.length) {
return undefined;
}
return receiver.at(index);
}
}

return (target as unknown as Record<string | symbol, unknown>)[
property
];
},
};

return new Proxy(
new ArraySlice(array, start, end),
ArraySlice.proxyHandler as ArraySliceProxyHandler<T>,
) as IndexableArraySlice<T>;
}
}

type IndexableArraySlice<T> = ArraySlice<T> & {
[index: number]: T | undefined;
};
import { type IndexableArraySlice, ArraySlice } from "../ArraySlice";

declare global {
interface ReadonlyArray<T> {
slidingWindows(
this: ReadonlyArray<T>,
windowSize: number,
): Generator<ArraySlice<T>, void, void>;
): Generator<IndexableArraySlice<T>, void, void>;
}

interface Array<T> {
slidingWindows(
this: ReadonlyArray<T>,
windowSize: number,
): Generator<ArraySlice<T>, void, void>;
): Generator<IndexableArraySlice<T>, void, void>;
}
}

Array.prototype.slidingWindows = function* <T>(
this: ReadonlyArray<T>,
windowSize: number,
): Generator<ArraySlice<T>, void, void> {
): Generator<IndexableArraySlice<T>, void, void> {
for (
let win: IndexableArraySlice<T> | null = ArraySlice.get(
this,
Expand All @@ -112,7 +32,3 @@ Array.prototype.slidingWindows = function* <T>(
yield win;
}
};

// Needed to fix the error "Augmentations for the global scope can only be directly nested in external modules or ambient module declarations. ts(2669)"
// See: https://stackoverflow.com/questions/57132428/augmentations-for-the-global-scope-can-only-be-directly-nested-in-external-modul
export {};
79 changes: 79 additions & 0 deletions tools/adventure-pack/goodies/typescript/ArraySlice/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
type ArraySliceProxyHandler<T> = {
get(
target: ArraySlice<T>,
property: PropertyKey,
receiver: ArraySlice<T>,
): unknown;
};

export type IndexableArraySlice<T> = ArraySlice<T> & {
[index: number]: T | undefined;
};

export class ArraySlice<T> {
private constructor(
public readonly array: ReadonlyArray<T>,
public readonly start: number,
public readonly end: number,
) {}

get length(): number {
return this.end - this.start + 1;
}

at(index: number): T | undefined {
const adjustedIndex = index < 0 ? index + this.length : index;
return this.array[this.start + adjustedIndex];
}

*[Symbol.iterator](this: ArraySlice<T>): Generator<T, void, void> {
for (let i = this.start; i <= this.end; ++i) {
yield this.array[i];
}
}

slide(delta: number = 1): IndexableArraySlice<T> {
return ArraySlice.get(this.array, this.start + delta, this.end + delta);
}

isPrefix(): boolean {
return this.start === 0;
}

isSuffix(): boolean {
return this.end === this.array.length - 1;
}

private declare static proxyHandler?: ArraySliceProxyHandler<unknown>;

static get<T>(
array: ReadonlyArray<T>,
start: number,
end: number,
): IndexableArraySlice<T> {
ArraySlice.proxyHandler ??= {
get(
target: ArraySlice<unknown>,
property: PropertyKey,
receiver: ArraySlice<unknown>,
): unknown {
if (typeof property === "string" || typeof property === "number") {
const index = parseInt(String(property), 10);
if (String(index) === String(property)) {
if (index < 0 || index >= receiver.length) {
return undefined;
}
return receiver.at(index);
}
}

return (target as unknown as Record<PropertyKey, unknown>)[property];
},
};

return new Proxy(
new ArraySlice(array, start, end),
ArraySlice.proxyHandler as ArraySliceProxyHandler<T>,
) as IndexableArraySlice<T>;
}
}
Loading