Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ For example, you can write a `People` dataset wrapper to find each `Person` in a
```javascript
class People extends DatasetWrapper {
[Symbol.iterator]() {
return this.subjectsOf("http://example.com/name", Person)
return this.subjectsOf(Person, "http://example.com/name")
}
}
```
Expand Down
30 changes: 9 additions & 21 deletions src/DatasetWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,14 @@ import type { DataFactory, DatasetCore, Term } from "@rdfjs/types"
type TermWrapperConstructor<T> = new (term: Term, dataset: DatasetCore, factory: DataFactory) => T

export class DatasetWrapper extends DatasetCoreBase {
protected subjectsOf<T>(predicate: string, termWrapper: TermWrapperConstructor<T>): Iterable<T> {
return this.matchSubjectsOf(termWrapper, predicate)
}

protected* matchSubjectsOf<T>(termWrapper: TermWrapperConstructor<T>, predicate?: string, object?: string, graph?: string): Iterable<T> {
const p = predicate ? this.factory.namedNode(predicate) : undefined
const o = object ? this.factory.namedNode(object) : undefined
const g = graph ? this.factory.namedNode(graph) : undefined

for (const q of this.match(undefined, p, o, g)) {
protected* subjectsOf<T>(termWrapper: TermWrapperConstructor<T>, predicate?: string, object?: string | Term, graph?: string): Iterable<T> {
for (const q of this.match(undefined, toTerm(predicate, this.factory), toTerm(object, this.factory), toTerm(graph, this.factory))) {
yield new termWrapper(q.subject, this, this.factory)
}
}

protected* objectsOf<T>(predicate: string, termWrapper: TermWrapperConstructor<T>): Iterable<T> {
return this.matchObjectsOf(termWrapper, undefined, predicate)
}

protected* matchObjectsOf<T>(termWrapper: TermWrapperConstructor<T>, subject?: string, predicate?: string, graph?: string): Iterable<T> {
const s = subject ? this.factory.namedNode(subject) : undefined
const p = predicate ? this.factory.namedNode(predicate) : undefined
const g = graph ? this.factory.namedNode(graph) : undefined

for (const q of this.match(s, p, undefined, g)) {
protected* objectsOf<T>(termWrapper: TermWrapperConstructor<T>, predicate?: string, subject?: string | Term, graph?: string): Iterable<T> {
for (const q of this.match(toTerm(subject, this.factory), toTerm(predicate, this.factory), undefined, toTerm(graph, this.factory))) {
yield new termWrapper(q.object, this, this.factory)
}
}
Expand All @@ -36,6 +20,10 @@ export class DatasetWrapper extends DatasetCoreBase {
// TODO: Vocab
const rdfType = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"

return this.matchSubjectsOf(constructor, rdfType, classTerm)
return this.subjectsOf(constructor, rdfType, classTerm)
}
}

function toTerm(value: string | Term | undefined, factory: DataFactory): Term | undefined {
return typeof value === "string" ? factory.namedNode(value) : value
}
2 changes: 1 addition & 1 deletion test/unit/model/ParentDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Vocabulary } from "../Vocabulary.js"

export class ParentDataset extends DatasetWrapper {
public get subjectsOfHasChild(): Iterable<Parent> {
return this.subjectsOf(Vocabulary.hasChild, Parent)
return this.subjectsOf(Parent, Vocabulary.hasChild)
}
}
Loading