Skip to content

Commit 6185daf

Browse files
Copilotjeswr
andauthored
refactor: accept Quad_Graph instead of Term | string in namedGraph
Agent-Logs-Url: https://github.com/rdfjs/wrapper/sessions/05cfefef-18c2-4e8e-a838-9ef95107a56d Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com>
1 parent 9404683 commit 6185daf

3 files changed

Lines changed: 11 additions & 22 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ import { namedGraph, DatasetWrapper } from "@rdfjs/wrapper"
187187
// <ex:person2> <ex:name> "Bob" <ex:graph1> .
188188
// <ex:person1> <ex:name> "Charlie" . (default graph)
189189

190-
const graphView = namedGraph("https://example.org/graph1", dataset, DataFactory)
190+
const graphView = namedGraph(DataFactory.namedNode("https://example.org/graph1"), dataset, DataFactory)
191191

192192
// graphView behaves as a DatasetCore containing only default graph quads:
193193
// <ex:person1> <ex:name> "Alice" .

src/namedGraph.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import type { DataFactory, DatasetCore, Quad, Quad_Graph, Term } from "@rdfjs/ty
22
import { NamedGraphError } from "./errors/NamedGraphError.js"
33

44
class NamedGraphDataset implements DatasetCore {
5-
private readonly graph: Quad_Graph
6-
7-
constructor(graph: Term | string, private readonly dataset: DatasetCore, private readonly factory: DataFactory) {
8-
this.graph = typeof graph === "string" ? factory.namedNode(graph) : graph as Quad_Graph
5+
constructor(private readonly graph: Quad_Graph, private readonly dataset: DatasetCore, private readonly factory: DataFactory) {
96
}
107

118
get size(): number {
@@ -58,19 +55,19 @@ class NamedGraphDataset implements DatasetCore {
5855
* underlying dataset. Any attempt to use a non-default graph on the returned dataset throws a
5956
* {@link NamedGraphError}.
6057
*
61-
* @param graph - The named graph to project, either as a {@link Term} or a string IRI.
58+
* @param graph - The graph to project as a {@link Quad_Graph}.
6259
* @param dataset - The underlying dataset containing quads in one or more named graphs.
6360
* @param factory - A {@link DataFactory} used to construct quads.
6461
* @returns A {@link DatasetCore} view scoped to the specified named graph.
6562
*
6663
* @example
6764
* ```ts
68-
* const view = namedGraph("https://example.org/graph1", dataset, DataFactory)
65+
* const view = namedGraph(DataFactory.namedNode("https://example.org/graph1"), dataset, DataFactory)
6966
* for (const quad of view) {
7067
* console.log(quad.graph.termType) // "DefaultGraph"
7168
* }
7269
* ```
7370
*/
74-
export function namedGraph(graph: Term | string, dataset: DatasetCore, factory: DataFactory): DatasetCore {
71+
export function namedGraph(graph: Quad_Graph, dataset: DatasetCore, factory: DataFactory): DatasetCore {
7572
return new NamedGraphDataset(graph, dataset, factory)
7673
}

test/unit/named_graph.test.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { describe, it } from "node:test"
33
import { DataFactory, Store } from "n3"
44
import { namedGraph, NamedGraphError } from "@rdfjs/wrapper"
55

6-
const graph = "https://example.org/graph"
6+
const graph = DataFactory.namedNode("https://example.org/graph")
77
const s = DataFactory.namedNode("https://example.org/s")
88
const p = DataFactory.namedNode("https://example.org/p")
99
const o = DataFactory.literal("value")
1010

1111
function storeWithNamedGraph(): Store {
1212
const store = new Store()
13-
store.addQuad(DataFactory.quad(s, p, o, DataFactory.namedNode(graph)))
13+
store.addQuad(DataFactory.quad(s, p, o, graph))
1414
store.addQuad(DataFactory.quad(s, p, DataFactory.literal("default")))
1515
return store
1616
}
@@ -27,14 +27,6 @@ await describe("namedGraph", async () => {
2727
assert.equal(quads[0]!.graph.termType, "DefaultGraph")
2828
})
2929

30-
await it("accepts a Term as the graph parameter", () => {
31-
const ds = namedGraph(DataFactory.namedNode(graph), storeWithNamedGraph(), DataFactory)
32-
const quads = Array.from(ds)
33-
34-
assert.equal(quads.length, 1)
35-
assert.equal(quads[0]!.graph.termType, "DefaultGraph")
36-
})
37-
3830
await it("reports correct size", () => {
3931
const ds = namedGraph(graph, storeWithNamedGraph(), DataFactory)
4032
assert.equal(ds.size, 1)
@@ -58,7 +50,7 @@ await describe("namedGraph", async () => {
5850
ds.add(DataFactory.quad(s, p, newObj))
5951

6052
assert.equal(ds.size, 2)
61-
assert.equal(store.has(DataFactory.quad(s, p, newObj, DataFactory.namedNode(graph))), true)
53+
assert.equal(store.has(DataFactory.quad(s, p, newObj, graph)), true)
6254
})
6355

6456
await it("delete removes from the named graph of the underlying dataset", () => {
@@ -68,14 +60,14 @@ await describe("namedGraph", async () => {
6860
ds.delete(DataFactory.quad(s, p, o))
6961

7062
assert.equal(ds.size, 0)
71-
assert.equal(store.has(DataFactory.quad(s, p, o, DataFactory.namedNode(graph))), false)
63+
assert.equal(store.has(DataFactory.quad(s, p, o, graph)), false)
7264
})
7365

7466
await it("match filters by subject/predicate/object within the named graph", () => {
7567
const store = new Store()
7668
const p2 = DataFactory.namedNode("https://example.org/p2")
77-
store.addQuad(DataFactory.quad(s, p, o, DataFactory.namedNode(graph)))
78-
store.addQuad(DataFactory.quad(s, p2, DataFactory.literal("other"), DataFactory.namedNode(graph)))
69+
store.addQuad(DataFactory.quad(s, p, o, graph))
70+
store.addQuad(DataFactory.quad(s, p2, DataFactory.literal("other"), graph))
7971

8072
const ds = namedGraph(graph, store, DataFactory)
8173
const matched = Array.from(ds.match(undefined, p2))

0 commit comments

Comments
 (0)