diff --git a/.changeset/afraid-donuts-retire.md b/.changeset/afraid-donuts-retire.md new file mode 100644 index 0000000..23e13d9 --- /dev/null +++ b/.changeset/afraid-donuts-retire.md @@ -0,0 +1,5 @@ +--- +"@rdfjs/types": major +--- + +Add missing methods `fromTerm` and `fromQuad` to the `DataFactory` interface diff --git a/data-model.d.ts b/data-model.d.ts index 6fff406..50ca6d5 100644 --- a/data-model.d.ts +++ b/data-model.d.ts @@ -287,4 +287,24 @@ export interface DataFactory(original: T): NamedNode; + fromTerm(original: T): BlankNode; + fromTerm(original: T): Literal; + fromTerm(original: T): Variable; + fromTerm(original: T): DefaultGraph; + fromTerm(original: T): OutQuad; + fromTerm(original: T): T; + + /** + * @param original The original quad. + * @return A new instance of the quad such that newQuadInstance.equals(original) returns true. + * @see Quad + */ + fromQuad(original: InQuad): OutQuad; } diff --git a/rdf-js-tests.ts b/rdf-js-tests.ts index af12b23..220016f 100644 --- a/rdf-js-tests.ts +++ b/rdf-js-tests.ts @@ -84,6 +84,26 @@ function test_datafactory() { const variable: Variable = dataFactory.variable ? dataFactory.variable('v1') : {}; + const defaultGraph: DefaultGraph = dataFactory.defaultGraph(); + + type NamedNodeExt = NamedNode & { someProp: string }; + const term1: NamedNode = dataFactory.fromTerm({}); + type BlankNodeExt = BlankNode & { someProp: string }; + const term2: BlankNode = dataFactory.fromTerm({}); + type LiteralExt = Literal & { someProp: string }; + const term3: Literal = dataFactory.fromTerm({}); + type VariableExt = Variable & { someProp: string }; + const term4: Variable = dataFactory.fromTerm( {}); + type DefaultGraphExt = DefaultGraph & { someProp: string }; + const term5: DefaultGraph = dataFactory.fromTerm({}); + + const quadFromQuad: Term = dataFactory.fromQuad(dataFactory.quad( + dataFactory.namedNode("x"), + dataFactory.namedNode("y"), + dataFactory.literal(""), + dataFactory.defaultGraph() + )); + const term: NamedNode = {}; interface QuadBnode extends BaseQuad { subject: Term; @@ -119,6 +139,8 @@ function test_datafactory_star() { const equalToSelf: boolean = quadBobAge2.equals(quadBobAge); const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else')); } + + const quadTerm: Quad = dataFactory.fromTerm(quadBobAge); } function test_datafactory_star_basequad() { @@ -143,6 +165,21 @@ function test_datafactory_star_basequad() { const equalToSelf: boolean = quadBobAge2.equals(quadBobAge); const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else')); } + + const baseQuadTerm: BaseQuad = dataFactory.fromTerm(quadBobAge); + + const baseQuad: BaseQuad = dataFactory.fromQuad(quadBobAge); + + // Test with regular Quad + const quadDataFactory: DataFactory = {}; + + const regularQuadBobAge: Quad = quadDataFactory.quad( + dataFactory.namedNode('ex:bob'), + dataFactory.namedNode('ex:age'), + dataFactory.literal('23'), + ); + + const quadTerm: Quad = quadDataFactory.fromTerm(regularQuadBobAge); } function test_stream() {