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

Add optional direction for literals #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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/famous-parrots-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rdfjs/types": minor
---

Add optional direction for literals
28 changes: 20 additions & 8 deletions data-model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export interface Literal {
* @link http://tools.ietf.org/html/bcp47
*/
language: string;
/**
* the direction of the language-tagged string.
*/
direction?: 'ltr' | 'rtl' | '';
/**
* A NamedNode whose IRI represents the datatype of the literal.
*/
Expand All @@ -81,7 +85,7 @@ export interface Literal {
/**
* @param other The term to compare with.
* @return True if and only if other has termType "Literal"
* and the same `value`, `language`, and `datatype`.
* and the same `value`, `language`, `direction`, and `datatype`.
*/
equals(other: Term | null | undefined): boolean;
}
Expand Down Expand Up @@ -254,16 +258,19 @@ export interface DataFactory<OutQuad extends BaseQuad = Quad, InQuad extends Bas
blankNode(value?: string): BlankNode;

/**
* @param value The literal value.
* @param languageOrDatatype The optional language or datatype.
* If `languageOrDatatype` is a NamedNode,
* then it is used for the value of `NamedNode.datatype`.
* Otherwise `languageOrDatatype` is used for the value
* of `NamedNode.language`.
* @param value The literal value.
* @param languageOrDatatype The optional language, datatype, or directional language.
* If `languageOrDatatype` is a NamedNode,
* then it is used for the value of `NamedNode.datatype`.
* If `languageOrDatatype` is a NamedNode, it is used for the value
* of `NamedNode.language`.
* Otherwise, it is used as a directional language,
* from which the language is set to `languageOrDatatype.language`
* and the direction to `languageOrDatatype.direction`.
* @return A new instance of Literal.
* @see Literal
*/
literal(value: string, languageOrDatatype?: string | NamedNode): Literal;
literal(value: string, languageOrDatatype?: string | NamedNode | DirectionalLanguage): Literal;

/**
* This method is optional.
Expand All @@ -288,3 +295,8 @@ export interface DataFactory<OutQuad extends BaseQuad = Quad, InQuad extends Bas
*/
quad(subject: InQuad['subject'], predicate: InQuad['predicate'], object: InQuad['object'], graph?: InQuad['graph']): OutQuad;
}

export interface DirectionalLanguage {
language: string;
direction?: 'ltr' | 'rtl' | '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases would '' be used as input over undefined

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because direction can be a falsy value, as discussed in the PR here: rdfjs/data-model-spec#175

That actually reminds me, we should also allow null here...

}
7 changes: 7 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

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 All @@ -36,6 +36,7 @@
const termType3: string = literal.termType;
const value3: string = literal.value;
const language3: string = literal.language;
const dir3: 'ltr' | 'rtl' | '' | undefined = literal.direction;
const datatype3: NamedNode = literal.datatype;
let literalEqual: boolean = literal.equals(someTerm);
literalEqual = literal.equals(null);
Expand Down Expand Up @@ -81,6 +82,12 @@
const literal1: Literal = dataFactory.literal('abc');
const literal2: Literal = dataFactory.literal('abc', 'en-us');
const literal3: Literal = dataFactory.literal('abc', namedNode);
const literal4: Literal = dataFactory.literal('abc', { language: 'en-us' });
const literal5: Literal = dataFactory.literal('abc', { language: 'en-us', direction: 'ltr' });
const literal6: Literal = dataFactory.literal('abc', { language: 'en-us', direction: 'rtl' });
const literal7: Literal = dataFactory.literal('abc', { language: 'en-us', direction: '' });
// @ts-expect-error
const literal8: Literal = dataFactory.literal('abc', { language: 'en-us', direction: 'wrong' });

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

Expand Down
Loading