Skip to content

Commit 80ed19a

Browse files
DatasetWrapper API design improvements
Co-authored-by: Matthieu Bosquet <matthieubosquet@gmail.com>
1 parent 0b6262f commit 80ed19a

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/DatasetWrapper.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,30 @@ import type { DataFactory, DatasetCore, Term } from "@rdfjs/types"
44
type TermWrapperConstructor<T> = new (term: Term, dataset: DatasetCore, factory: DataFactory) => T
55

66
export class DatasetWrapper extends DatasetCoreBase {
7-
protected* subjectsOf<T>(termWrapper: TermWrapperConstructor<T>, predicate?: Term, object?: Term, graph?: Term): Iterable<T> {
7+
protected subjectsOf<T>(predicate: Term, termWrapper: TermWrapperConstructor<T>): Iterable<T> {
8+
return this.matchSubjectsOf(termWrapper, predicate)
9+
}
10+
11+
protected* matchSubjectsOf<T>(termWrapper: TermWrapperConstructor<T>, predicate?: Term, object?: Term, graph?: Term): Iterable<T> {
812
for (const q of this.match(undefined, predicate, object, graph)) {
913
yield new termWrapper(q.subject, this, this.factory)
1014
}
1115
}
1216

13-
protected* objectsOf<T>(termWrapper: TermWrapperConstructor<T>, subject?: Term, predicate?: Term, graph?: Term): Iterable<T> {
14-
for (const q of this.match(subject, predicate, undefined, graph)) {
15-
yield new termWrapper(q.object, this, this.factory)
16-
}
17+
protected* objectsOf<T>(predicate: Term, termWrapper: TermWrapperConstructor<T>): Iterable<T> {
18+
return this.matchObjectsOf(termWrapper, undefined, predicate)
1719
}
1820

19-
protected* objectsOfX<T>([s, p, g]: [Term?, Term?, Term?], termWrapper: TermWrapperConstructor<T>): Iterable<T> {
20-
for (const q of this.match(s, p, undefined, g)) {
21+
protected* matchObjectsOf<T>(termWrapper: TermWrapperConstructor<T>, subject?: Term, predicate?: Term, graph?: Term): Iterable<T> {
22+
for (const q of this.match(subject, predicate, undefined, graph)) {
2123
yield new termWrapper(q.object, this, this.factory)
2224
}
2325
}
2426

25-
protected* subjectsOfX<T>([p, o, g]: [Term?, Term?, Term?], termWrapper: TermWrapperConstructor<T>): Iterable<T> {
26-
for (const q of this.match(undefined, p, o, g)) {
27-
yield new termWrapper(q.subject, this, this.factory)
28-
}
29-
}
30-
31-
protected instancesOf<T>(constructor: TermWrapperConstructor<T>, classTerm: Term): Iterable<T> {
27+
protected instancesOf<T>(classTerm: Term, constructor: TermWrapperConstructor<T>): Iterable<T> {
3228
// TODO: Vocab
3329
const rdfType = this.factory.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
3430

35-
return this.subjectsOf(constructor, rdfType, classTerm)
31+
return this.matchSubjectsOf(constructor, rdfType, classTerm)
3632
}
3733
}

test/unit/model/MyDataset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { Vocabulary } from "../Vocabulary.js"
44

55
export class MyDataset extends DatasetWrapper {
66
public get parents(): Iterable<Parent> {
7-
return this.subjectsOf(Parent, Vocabulary.hasChild)
7+
return this.subjectsOf(Vocabulary.hasChild, Parent)
88
}
99
}

0 commit comments

Comments
 (0)