|
| 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 | +import { RDF } from "./vocabulary/RDF.js" |
| 7 | +import type { Term } from "@rdfjs/types" |
| 8 | +import { Overwriter } from "./Overwriter.js" |
| 9 | + |
| 10 | +export class RdfList<T> implements Array<T> { |
| 11 | + private root: ListItem<T> |
| 12 | + |
| 13 | + constructor(root: Term, private readonly subject: TermWrapper, private readonly predicate: string, private readonly valueMapping: IValueMapping<T>, private readonly termMapping: ITermMapping<T>) { |
| 14 | + this.root = new ListItem(root, this.subject.dataset, this.subject.factory, valueMapping, termMapping) |
| 15 | + |
| 16 | + // TODO: Singleton interceptor? |
| 17 | + return new Proxy(this, new IndexerInterceptor<T>) |
| 18 | + } |
| 19 | + |
| 20 | + // Never invoked, intercepted by proxy |
| 21 | + [n: number]: T |
| 22 | + |
| 23 | + get [Symbol.unscopables](): { [K in keyof any[]]?: boolean } { |
| 24 | + return Array.prototype[Symbol.unscopables] |
| 25 | + } |
| 26 | + |
| 27 | + get length(): number { |
| 28 | + return [...this.items].length |
| 29 | + } |
| 30 | + |
| 31 | + set length(_: number) { |
| 32 | + throw new Error("this array is based on an RDF Collection. Its length cannot be modified like this.") |
| 33 | + } |
| 34 | + |
| 35 | + [Symbol.iterator](): ArrayIterator<T> { |
| 36 | + return this.values() |
| 37 | + } |
| 38 | + |
| 39 | + at(index: number): T | undefined { |
| 40 | + // TODO: Don't materialize all, only up to index |
| 41 | + return [...this.items].at(index)?.first |
| 42 | + } |
| 43 | + |
| 44 | + concat(...items: Array<ConcatArray<T> | T>): T[] { |
| 45 | + return [...this].concat(...items) |
| 46 | + } |
| 47 | + |
| 48 | + copyWithin(target: number, start: number, end?: number): this { |
| 49 | + throw new Error("not implemented") |
| 50 | + } |
| 51 | + |
| 52 | + entries(): ArrayIterator<[number, T]> { |
| 53 | + // TODO: Don't materlialize all upfront |
| 54 | + return [...this].entries() |
| 55 | + } |
| 56 | + |
| 57 | + every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[] { |
| 58 | + return [...this].every(predicate, thisArg) |
| 59 | + } |
| 60 | + |
| 61 | + fill(value: T, start?: number, end?: number): this { |
| 62 | + throw new Error("not implemented") |
| 63 | + } |
| 64 | + |
| 65 | + filter<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[] { |
| 66 | + return [...this].filter(predicate, thisArg) |
| 67 | + } |
| 68 | + |
| 69 | + find<S extends T>(predicate: (value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined { |
| 70 | + return [...this].find(predicate, thisArg) |
| 71 | + } |
| 72 | + |
| 73 | + findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number { |
| 74 | + return [...this].findIndex(predicate, thisArg) |
| 75 | + } |
| 76 | + |
| 77 | + flat<A, D extends number = 1>(depth?: D): FlatArray<A, D>[] { |
| 78 | + throw new Error("not implemented") |
| 79 | + } |
| 80 | + |
| 81 | + flatMap<U, This = undefined>(callback: (this: This, value: T, index: number, array: T[]) => (ReadonlyArray<U> | U), thisArg?: This): U[] { |
| 82 | + return [...this].flatMap(callback, thisArg) |
| 83 | + } |
| 84 | + |
| 85 | + forEach(callback: (value: T, index: number, array: T[]) => void, thisArg?: any): void { |
| 86 | + [...this].forEach(callback, thisArg) |
| 87 | + } |
| 88 | + |
| 89 | + includes(searchElement: T, fromIndex?: number): boolean { |
| 90 | + return [...this].includes(searchElement, fromIndex) |
| 91 | + } |
| 92 | + |
| 93 | + indexOf(searchElement: T, fromIndex?: number): number { |
| 94 | + return [...this].indexOf(searchElement, fromIndex) |
| 95 | + } |
| 96 | + |
| 97 | + join(separator?: string): string { |
| 98 | + return [...this].join(separator) |
| 99 | + } |
| 100 | + |
| 101 | + keys(): ArrayIterator<number> { |
| 102 | + // TODO: Don't materialize all upfront |
| 103 | + return [...this.items].keys() |
| 104 | + } |
| 105 | + |
| 106 | + lastIndexOf(searchElement: T, fromIndex?: number): number { |
| 107 | + return [...this].lastIndexOf(searchElement, fromIndex) |
| 108 | + } |
| 109 | + |
| 110 | + map<U>(callback: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] { |
| 111 | + return [...this].map(callback, thisArg) |
| 112 | + } |
| 113 | + |
| 114 | + pop(): T | undefined { |
| 115 | + return [...this.items].at(-1)?.pop() |
| 116 | + } |
| 117 | + |
| 118 | + push(...items: T[]): number { |
| 119 | + const nil = this.subject.factory.namedNode(RDF.nil) |
| 120 | + |
| 121 | + for (const item of items) { |
| 122 | + // A node will be needed either to replace rdf:nil in an empty list or to add a new one to the end of an existing list |
| 123 | + const newNode = new ListItem(this.subject.factory.blankNode(), this.subject.dataset, this.subject.factory, this.valueMapping, this.termMapping) |
| 124 | + |
| 125 | + const lastNode = this.root.isNil ? |
| 126 | + // The statement representing an empty list is replaced by a new one whose object is the new node |
| 127 | + // The representation of the first item (root, currently rdf:nil, the empty list) is overwritten by the new node |
| 128 | + this.root = new Overwriter<T>(this.subject, this.predicate).listNode = newNode : |
| 129 | + |
| 130 | + // replace rest of current last with new and return is because it's the new last |
| 131 | + [...this.items].at(-1)!.rest = newNode; |
| 132 | + |
| 133 | + lastNode.first = item |
| 134 | + lastNode.restRaw = nil |
| 135 | + } |
| 136 | + |
| 137 | + return this.length |
| 138 | + } |
| 139 | + |
| 140 | + reduce<U>(callback: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue?: U): U { |
| 141 | + return [...this].reduce(callback, initialValue!) |
| 142 | + } |
| 143 | + |
| 144 | + reduceRight<U>(callback: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue?: U): U { |
| 145 | + return [...this].reduceRight(callback, initialValue!) |
| 146 | + } |
| 147 | + |
| 148 | + reverse(): T[] { |
| 149 | + throw new Error("not implemented") |
| 150 | + } |
| 151 | + |
| 152 | + shift(): T | undefined { |
| 153 | + if (this.root.isNil) { |
| 154 | + return undefined |
| 155 | + } |
| 156 | + |
| 157 | + const value = this.root.first |
| 158 | + |
| 159 | + if (this.root.rest.isNil) { |
| 160 | + new Overwriter<T>(this.subject, this.predicate).listNode = this.root.rest |
| 161 | + this.root.firstRaw = undefined |
| 162 | + this.root.restRaw = undefined |
| 163 | + } else { |
| 164 | + this.root.firstRaw = this.root.rest.firstRaw |
| 165 | + this.root.restRaw = this.root.rest.restRaw |
| 166 | + } |
| 167 | + |
| 168 | + return value |
| 169 | + } |
| 170 | + |
| 171 | + slice(start?: number, end?: number): T[] { |
| 172 | + // TODO: Probably no need to materialize all |
| 173 | + return [...this].slice(start, end) |
| 174 | + } |
| 175 | + |
| 176 | + some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean { |
| 177 | + return [...this].some(predicate, thisArg) |
| 178 | + } |
| 179 | + |
| 180 | + sort(compareFn?: (a: T, b: T) => number): this { |
| 181 | + throw new Error("not implemented") |
| 182 | + } |
| 183 | + |
| 184 | + splice(start: number, deleteCount?: number, ...items: T[]): T[] { |
| 185 | + throw new Error("not implemented") |
| 186 | + } |
| 187 | + |
| 188 | + unshift(...items: T[]): number { |
| 189 | + for (const item of items.reverse()) { |
| 190 | + const firstNode = this.root |
| 191 | + this.root = new Overwriter<T>(this.subject, this.predicate).listNode = new ListItem(this.subject.factory.blankNode(), this.subject.dataset, this.subject.factory, this.valueMapping, this.termMapping) |
| 192 | + this.root.first = item |
| 193 | + this.root.rest = firstNode |
| 194 | + } |
| 195 | + |
| 196 | + return this.length |
| 197 | + } |
| 198 | + |
| 199 | + * values(): ArrayIterator<T> { |
| 200 | + for (const item of this.items) { |
| 201 | + yield item.first |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private get items(): Iterable<ListItem<T>> { |
| 206 | + return this.root.items() |
| 207 | + } |
| 208 | +} |
0 commit comments