Skip to content

Commit

Permalink
Merge pull request #46 from matthieubosquet/datafactory
Browse files Browse the repository at this point in the history
Issue #45: Add missing spec methods to DataFactory interface
  • Loading branch information
tpluscode authored Dec 5, 2024
2 parents 0ed985c + cfc4d28 commit 66afd0e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-donuts-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rdfjs/types": major
---

Add missing methods `fromTerm` and `fromQuad` to the `DataFactory` interface
20 changes: 20 additions & 0 deletions data-model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,24 @@ export interface DataFactory<OutQuad extends BaseQuad = Quad, InQuad extends Bas
* @see Quad
*/
quad(subject: InQuad['subject'], predicate: InQuad['predicate'], object: InQuad['object'], graph?: InQuad['graph']): OutQuad;

/**
* @param original The original term.
* @return A new instance of the term such that newTermInstance.equals(original) returns true.
* @see Term
*/
fromTerm<T extends NamedNode>(original: T): NamedNode;
fromTerm<T extends BlankNode>(original: T): BlankNode;
fromTerm<T extends Literal>(original: T): Literal;
fromTerm<T extends Variable>(original: T): Variable;
fromTerm<T extends DefaultGraph>(original: T): DefaultGraph;
fromTerm<T extends BaseQuad>(original: T): OutQuad;
fromTerm<T extends Term>(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;
}
37 changes: 37 additions & 0 deletions rdf-js-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ function test_datafactory() {

const variable: Variable = dataFactory.variable ? dataFactory.variable('v1') : <any> {};

const defaultGraph: DefaultGraph = dataFactory.defaultGraph();

type NamedNodeExt = NamedNode & { someProp: string };
const term1: NamedNode = dataFactory.fromTerm(<NamedNodeExt>{});
type BlankNodeExt = BlankNode & { someProp: string };
const term2: BlankNode = dataFactory.fromTerm(<BlankNodeExt>{});
type LiteralExt = Literal & { someProp: string };
const term3: Literal = dataFactory.fromTerm(<LiteralExt>{});
type VariableExt = Variable & { someProp: string };
const term4: Variable = dataFactory.fromTerm(<VariableExt> {});
type DefaultGraphExt = DefaultGraph & { someProp: string };
const term5: DefaultGraph = dataFactory.fromTerm(<DefaultGraphExt>{});

const quadFromQuad: Term = dataFactory.fromQuad(dataFactory.quad(
dataFactory.namedNode("x"),
dataFactory.namedNode("y"),
dataFactory.literal(""),
dataFactory.defaultGraph()
));

const term: NamedNode = <any> {};
interface QuadBnode extends BaseQuad {
subject: Term;
Expand Down Expand Up @@ -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() {
Expand All @@ -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<Quad> = <any> {};

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() {
Expand Down

0 comments on commit 66afd0e

Please sign in to comment.