Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #45: Add missing spec methods to DataFactory interface #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions data-model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,19 @@ 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(original: InQuad): 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;
}
50 changes: 50 additions & 0 deletions rdf-js-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const namedNodeConstant: NamedNode<'http://example.org'> = <any> {};
const constantIri: 'http://example.org' = namedNodeConstant.value;
// @ts-expect-error

Check warning on line 22 in rdf-js-tests.ts

View workflow job for this annotation

GitHub Actions / build

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
const otherConstantIri: 'http://not-example.org' = namedNodeConstant.value;
// @ts-expect-error
const otherNamedNodeConstant: NamedNode<'http://not-example.org'> = namedNodeConstant;
Expand Down Expand Up @@ -84,6 +84,34 @@

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>{});
const term1ext: NamedNodeExt = dataFactory.fromTerm(<NamedNodeExt>{});
type BlankNodeExt = BlankNode & { someProp: string };
const term2: BlankNode = dataFactory.fromTerm(<BlankNodeExt>{});
const term2ext: BlankNodeExt = dataFactory.fromTerm(<BlankNodeExt>{});
type LiteralExt = Literal & { someProp: string };
const term3: Literal = dataFactory.fromTerm(<LiteralExt>{});
const term3ext: LiteralExt = dataFactory.fromTerm(<LiteralExt>{});
type VariableExt = Variable & { someProp: string };
const term4: Variable = dataFactory.fromTerm(<VariableExt> {});
const term4ext: VariableExt = dataFactory.fromTerm(<VariableExt> {});
type DefaultGraphExt = DefaultGraph & { someProp: string };
const term5: DefaultGraph = dataFactory.fromTerm(<DefaultGraphExt>{});
const term5ext: DefaultGraphExt = dataFactory.fromTerm(<DefaultGraphExt>{});
type OutQuadExt = Quad & { someProp: string };
const term6: Quad = dataFactory.fromTerm(<OutQuadExt>{});
const term6ext: OutQuadExt = dataFactory.fromTerm<OutQuadExt>(<OutQuadExt>{});

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 +147,8 @@
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 +173,26 @@
const equalToSelf: boolean = quadBobAge2.equals(quadBobAge);
const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else'));
}

const datafactoryBaseQuadToQuad: DataFactory<BaseQuad, Quad> = <any> {};

const quadToBaseQuad1: BaseQuad = datafactoryBaseQuadToQuad.fromTerm(quadBobAge);

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);
const quadTerm2: Quad = datafactoryBaseQuadToQuad.fromTerm<Quad>(regularQuadBobAge);
}

function test_stream() {
Expand Down
Loading