Skip to content

Commit

Permalink
Add optional direction for literals
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 9, 2023
1 parent 183bda7 commit 9d27bde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
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' | '';
}
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' | '' | 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

0 comments on commit 9d27bde

Please sign in to comment.