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

Merged
merged 3 commits into from
Jan 7, 2025
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/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' | '' | null;
Copy link

Choose a reason for hiding this comment

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

JS noob question: The specification states direction?: DOMString. Does it allow null?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not too sure about the semantics of direction?: DOMString in ReSpec.
But at least the text in rdfjs/data-model-spec#175 says where <code>direction</code> is optional or can be falsy.
Falsy includes null and undefined in JavaScript.

Copy link

Choose a reason for hiding this comment

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

Good point! Should we ensure that the WebIDL definition is synchronized with the specification text before merging both MRs?

Copy link
Member Author

Choose a reason for hiding this comment

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

null and undefined are both implicitly included in other parts of the spec, such as Term.equals().
So this would probably be part of a broader change.
Might be good to open a separate issue for this?

Copy link

Choose a reason for hiding this comment

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

Ok! Make sense! Thank you!

/**
* 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' | '' | null;
}
7 changes: 7 additions & 0 deletions rdf-js-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function test_terms() {
const termType3: string = literal.termType;
const value3: string = literal.value;
const language3: string = literal.language;
const dir3: 'ltr' | 'rtl' | '' | null | 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 @@ function test_datafactory() {
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