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

Merged
merged 5 commits into from
Dec 5, 2024
Merged
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
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 @@ -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 (18)

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

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

View workflow job for this annotation

GitHub Actions / build (20)

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

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

View workflow job for this annotation

GitHub Actions / build (lts/*)

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,26 @@

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 @@
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 @@
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
Loading