Skip to content

Commit

Permalink
fix: iterator response length
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jul 8, 2021
1 parent a15434d commit acf324d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .changeset/silent-peas-fold.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"alcaeus": patch
---

Iterating response would return same resource multiple times
Iterating response and resource count would return same resource multiple times
32 changes: 22 additions & 10 deletions src/ResourceRepresentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,31 @@ export default class <D extends DatasetCore, T extends RdfResourceCore<D>> imple
private __graph: AnyPointer<AnyContext, D>
private __factory: ResourceFactory
private readonly rootNode: GraphPointer<ResourceIdentifier>
private __uniqueResources: () => Map<Term, Hydra.Resource<D>>

public constructor(graph: AnyPointer<AnyContext, D>, factory: ResourceFactory, rootResource: NamedNode) {
this.__graph = graph
this.__factory = factory
this.rootNode = graph.node(rootResource)

this.__uniqueResources = (() => {
let map: Map<Term, Hydra.Resource<D>>

return () => {
if (!map) {
map = this.__graph.in().toArray()
.reduce((uniq, pointer) => {
if (!uniq.has(pointer.term)) {
return uniq.set(pointer.term, this._createEntity(pointer))
}

return uniq
}, new TermMap<Term, Hydra.Resource<D>>())
}

return map
}
})()
}

public get<T>(uri: string): (T & Hydra.Resource<D>) | undefined {
Expand All @@ -64,7 +84,7 @@ export default class <D extends DatasetCore, T extends RdfResourceCore<D>> imple
}

public get length(): number {
return this.__graph.in().terms.length
return this.__uniqueResources().size
}

public ofType<T>(classId: string | NamedNode) {
Expand All @@ -74,15 +94,7 @@ export default class <D extends DatasetCore, T extends RdfResourceCore<D>> imple
}

public [Symbol.iterator]() {
return this.__graph.in().toArray()
.reduce((uniq, pointer) => {
if (!uniq.has(pointer.term)) {
return uniq.set(pointer.term, this._createEntity(pointer))
}

return uniq
}, new TermMap<Term, Hydra.Resource<D>>())
.values()
return this.__uniqueResources().values()
}

private _createEntity<T>(node: GraphPointer<ResourceIdentifier>) {
Expand Down
15 changes: 15 additions & 0 deletions tests/ResourceRepresentation-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ describe('ResourceRepresentation', () => {
)
})

describe('length', () => {
it('should count unique resources', () => {
// given
const dataset = $rdf.dataset()
cf({ dataset, graph: ex.a })
.namedNode(ex.a).addOut(rdf.type, ex.Res).addOut(schema.knows, [ex.b, ex.c, ex.d])
.namedNode(ex.c).addOut(schema.knows, [ex.a, ex.d])
.namedNode(ex.d).addIn(schema.knows, ex.a)
const r12n = new ResourceRepresentation(cf({ dataset, graph: ex.a }), factory, ex.a)

// then
expect(r12n.length).toEqual(2)
})
})

describe('root', () => {
it('should use selection root resource as specified by parameter', () => {
// given
Expand Down

0 comments on commit acf324d

Please sign in to comment.