Skip to content

Commit 46acfbb

Browse files
authored
Add optional direction for literals (#38)
* Add optional direction for literals * Add changeset * Also allow null directions
1 parent d66a6e6 commit 46acfbb

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

.changeset/famous-parrots-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rdfjs/types": minor
3+
---
4+
5+
Add optional direction for literals

data-model.d.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export interface Literal {
7373
* @link http://tools.ietf.org/html/bcp47
7474
*/
7575
language: string;
76+
/**
77+
* the direction of the language-tagged string.
78+
*/
79+
direction?: 'ltr' | 'rtl' | '' | null;
7680
/**
7781
* A NamedNode whose IRI represents the datatype of the literal.
7882
*/
@@ -81,7 +85,7 @@ export interface Literal {
8185
/**
8286
* @param other The term to compare with.
8387
* @return True if and only if other has termType "Literal"
84-
* and the same `value`, `language`, and `datatype`.
88+
* and the same `value`, `language`, `direction`, and `datatype`.
8589
*/
8690
equals(other: Term | null | undefined): boolean;
8791
}
@@ -254,16 +258,19 @@ export interface DataFactory<OutQuad extends BaseQuad = Quad, InQuad extends Bas
254258
blankNode(value?: string): BlankNode;
255259

256260
/**
257-
* @param value The literal value.
258-
* @param languageOrDatatype The optional language or datatype.
259-
* If `languageOrDatatype` is a NamedNode,
260-
* then it is used for the value of `NamedNode.datatype`.
261-
* Otherwise `languageOrDatatype` is used for the value
262-
* of `NamedNode.language`.
261+
* @param value The literal value.
262+
* @param languageOrDatatype The optional language, datatype, or directional language.
263+
* If `languageOrDatatype` is a NamedNode,
264+
* then it is used for the value of `NamedNode.datatype`.
265+
* If `languageOrDatatype` is a NamedNode, it is used for the value
266+
* of `NamedNode.language`.
267+
* Otherwise, it is used as a directional language,
268+
* from which the language is set to `languageOrDatatype.language`
269+
* and the direction to `languageOrDatatype.direction`.
263270
* @return A new instance of Literal.
264271
* @see Literal
265272
*/
266-
literal(value: string, languageOrDatatype?: string | NamedNode): Literal;
273+
literal(value: string, languageOrDatatype?: string | NamedNode | DirectionalLanguage): Literal;
267274

268275
/**
269276
* This method is optional.
@@ -308,3 +315,8 @@ export interface DataFactory<OutQuad extends BaseQuad = Quad, InQuad extends Bas
308315
*/
309316
fromQuad(original: InQuad): OutQuad;
310317
}
318+
319+
export interface DirectionalLanguage {
320+
language: string;
321+
direction?: 'ltr' | 'rtl' | '' | null;
322+
}

rdf-js-tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function test_terms() {
3636
const termType3: string = literal.termType;
3737
const value3: string = literal.value;
3838
const language3: string = literal.language;
39+
const dir3: 'ltr' | 'rtl' | '' | null | undefined = literal.direction;
3940
const datatype3: NamedNode = literal.datatype;
4041
let literalEqual: boolean = literal.equals(someTerm);
4142
literalEqual = literal.equals(null);
@@ -81,6 +82,12 @@ function test_datafactory() {
8182
const literal1: Literal = dataFactory.literal('abc');
8283
const literal2: Literal = dataFactory.literal('abc', 'en-us');
8384
const literal3: Literal = dataFactory.literal('abc', namedNode);
85+
const literal4: Literal = dataFactory.literal('abc', { language: 'en-us' });
86+
const literal5: Literal = dataFactory.literal('abc', { language: 'en-us', direction: 'ltr' });
87+
const literal6: Literal = dataFactory.literal('abc', { language: 'en-us', direction: 'rtl' });
88+
const literal7: Literal = dataFactory.literal('abc', { language: 'en-us', direction: '' });
89+
// @ts-expect-error
90+
const literal8: Literal = dataFactory.literal('abc', { language: 'en-us', direction: 'wrong' });
8491

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

0 commit comments

Comments
 (0)