|
| 1 | +import { TermWrapper } from "./TermWrapper.js" |
| 2 | +import type { IValueMapping } from "./type/IValueMapping.js" |
| 3 | +import type { ITermMapping } from "./type/ITermMapping.js" |
| 4 | +import { IndexerInterceptor } from "./IndexerInterceptor.js" |
| 5 | +import { ListItem } from "./ListItem.js" |
| 6 | + |
| 7 | +export class RdfList<T> implements Array<T> { |
| 8 | + constructor(private readonly anchor: TermWrapper, private readonly valueMapping: IValueMapping<T>, private readonly termMapping: ITermMapping<T>) { |
| 9 | + // TODO: Singleton interceptor? |
| 10 | + return new Proxy(this, new IndexerInterceptor<T>) |
| 11 | + } |
| 12 | + |
| 13 | + // Never invoked, intercepted by proxy |
| 14 | + [n: number]: T |
| 15 | + |
| 16 | + get [Symbol.unscopables](): { [K in keyof any[]]?: boolean } { |
| 17 | + throw new Error("not implemented") |
| 18 | + } |
| 19 | + |
| 20 | + get length(): number { |
| 21 | + return [...this.listItems()].length |
| 22 | + } |
| 23 | + |
| 24 | + set length(_: number) { |
| 25 | + throw new Error("this array is based on an RDF Collection. Its length cannot be modified like this.") |
| 26 | + } |
| 27 | + |
| 28 | + [Symbol.iterator](): ArrayIterator<T> { |
| 29 | + return this.values() |
| 30 | + } |
| 31 | + |
| 32 | + at(index: number): T | undefined { |
| 33 | + return [...this].at(index) |
| 34 | + } |
| 35 | + |
| 36 | + concat(...items: Array<ConcatArray<T> | T>): T[] { |
| 37 | + throw new Error("not implemented") |
| 38 | + } |
| 39 | + |
| 40 | + copyWithin(target: number, start: number, end?: number): this { |
| 41 | + throw new Error("not implemented") |
| 42 | + } |
| 43 | + |
| 44 | + entries(): ArrayIterator<[number, T]> { |
| 45 | + return [...this].entries() |
| 46 | + } |
| 47 | + |
| 48 | + every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[] { |
| 49 | + return [...this].every(predicate, thisArg) |
| 50 | + } |
| 51 | + |
| 52 | + fill(value: T, start?: number, end?: number): this { |
| 53 | + throw new Error("not implemented") |
| 54 | + } |
| 55 | + |
| 56 | + filter<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[] { |
| 57 | + return [...this].filter(predicate, thisArg) |
| 58 | + } |
| 59 | + |
| 60 | + find<S extends T>(predicate: (value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined { |
| 61 | + return [...this].find(predicate, thisArg) |
| 62 | + } |
| 63 | + |
| 64 | + findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number { |
| 65 | + return [...this].findIndex(predicate, thisArg) |
| 66 | + } |
| 67 | + |
| 68 | + flat<A, D extends number = 1>(depth?: D): FlatArray<A, D>[] { |
| 69 | + throw new Error("not implemented") |
| 70 | + } |
| 71 | + |
| 72 | + flatMap<U, This = undefined>(callback: (this: This, value: T, index: number, array: T[]) => (ReadonlyArray<U> | U), thisArg?: This): U[] { |
| 73 | + return [...this].flatMap(callback, thisArg) |
| 74 | + } |
| 75 | + |
| 76 | + forEach(callback: (value: T, index: number, array: T[]) => void, thisArg?: any): void { |
| 77 | + [...this].forEach(callback, thisArg) |
| 78 | + } |
| 79 | + |
| 80 | + includes(searchElement: T, fromIndex?: number): boolean { |
| 81 | + return [...this].includes(searchElement, fromIndex) |
| 82 | + } |
| 83 | + |
| 84 | + indexOf(searchElement: T, fromIndex?: number): number { |
| 85 | + return [...this].indexOf(searchElement, fromIndex) |
| 86 | + } |
| 87 | + |
| 88 | + join(separator?: string): string { |
| 89 | + return [...this].join(separator) |
| 90 | + } |
| 91 | + |
| 92 | + keys(): ArrayIterator<number> { |
| 93 | + return [...this.listItems()].keys() |
| 94 | + } |
| 95 | + |
| 96 | + lastIndexOf(searchElement: T, fromIndex?: number): number { |
| 97 | + return [...this].lastIndexOf(searchElement, fromIndex) |
| 98 | + } |
| 99 | + |
| 100 | + map<U>(callback: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] { |
| 101 | + return [...this].map(callback, thisArg) |
| 102 | + } |
| 103 | + |
| 104 | + pop(): T | undefined { |
| 105 | + return [...this.listItems()].at(-1)?.pop() |
| 106 | + } |
| 107 | + |
| 108 | + push(...items: T[]): number { |
| 109 | + if (this.length === 0) { |
| 110 | + throw new Error("Adding to empty is not implemented yet") |
| 111 | + } |
| 112 | + |
| 113 | + return [...this.listItems()].at(-1)!.push(...items) |
| 114 | + } |
| 115 | + |
| 116 | + reduce<U>(callback: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue?: U): U { |
| 117 | + throw new Error("not implemented") |
| 118 | + } |
| 119 | + |
| 120 | + reduceRight<U>(callback: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue?: U): U { |
| 121 | + throw new Error("not implemented") |
| 122 | + } |
| 123 | + |
| 124 | + reverse(): T[] { |
| 125 | + throw new Error("not implemented") |
| 126 | + } |
| 127 | + |
| 128 | + shift(): T | undefined { |
| 129 | + throw new Error("not implemented") |
| 130 | + } |
| 131 | + |
| 132 | + slice(start?: number, end?: number): T[] { |
| 133 | + return [...this].slice(start, end) |
| 134 | + } |
| 135 | + |
| 136 | + some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean { |
| 137 | + return [...this].some(predicate, thisArg) |
| 138 | + } |
| 139 | + |
| 140 | + sort(compareFn?: (a: T, b: T) => number): this { |
| 141 | + throw new Error("not implemented") |
| 142 | + } |
| 143 | + |
| 144 | + splice(start: number, deleteCount?: number, ...items: T[]): T[] { |
| 145 | + throw new Error("not implemented") |
| 146 | + } |
| 147 | + |
| 148 | + unshift(...items: T[]): number { |
| 149 | + throw new Error("not implemented") |
| 150 | + } |
| 151 | + |
| 152 | + * values(): ArrayIterator<T> { |
| 153 | + for (const item of this.listItems()) { |
| 154 | + yield item.first |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private* listItems(): Iterable<ListItem<T>> { |
| 159 | + let item = new ListItem(this.anchor, this.valueMapping, this.termMapping) |
| 160 | + while (item.isListItem) { |
| 161 | + yield item |
| 162 | + |
| 163 | + item = item.rest |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments