Skip to content

Commit 7b8a07c

Browse files
committed
Don't rely on dataset size for validating singular properties
Resolves #19
1 parent 16ed377 commit 7b8a07c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/TermWrapper.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ export class TermWrapper {
2424

2525
protected singular<T>(p: string, valueMapping: IValueMapping<T>): T {
2626
const predicate = this.factory.namedNode(p)
27-
const matches = this.dataset.match(this.term, predicate)
27+
const matches = this.dataset.match(this.term, predicate)[Symbol.iterator]()
2828

2929
// TODO: Expose standard errors
30-
if (matches.size > 1) {
30+
const {value: first, done: none} = matches.next()
31+
32+
if (none) {
3133
throw new Error(`More than one value for predicate ${p} on term ${this.term.value}`)
3234
}
3335

34-
for (const q of matches) {
35-
return valueMapping(new TermWrapper(q.object, this.dataset, this.factory))
36+
if (!matches.next().done) {
37+
throw new Error(`No value found for predicate ${p} on term ${this.term.value}`)
3638
}
3739

38-
throw new Error(`No value found for predicate ${p} on term ${this.term.value}`)
40+
return valueMapping(new TermWrapper(first.object, this.dataset, this.factory))
3941
}
4042

4143
protected singularNullable<T>(p: string, valueMapping: IValueMapping<T>): T | undefined {

0 commit comments

Comments
 (0)