Skip to content

Commit bd5094f

Browse files
committed
Make @direction output RDF 1.2 literals with base direction
1 parent 011dc1e commit bd5094f

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Optionally, the following parameters can be set in the `JsonLdParser` constructo
175175
* `allowSubjectList`: If RDF lists can appear in the subject position. _(Default: `false`)_
176176
* `validateValueIndexes`: If @index inside array nodes should be validated. I.e., nodes inside the same array with the same @id, should have equal @index values. This is not applicable to this parser as we don't do explicit flattening, but it is required to be spec-compliant. _(Default: `false`)_
177177
* `defaultGraph`: The default graph for constructing [quads](http://rdf.js.org/#dom-datafactory-quad). _(Default: `defaultGraph()`)_
178-
* `rdfDirection`: The [mode](https://w3c.github.io/json-ld-api/#dom-jsonldoptions-rdfdirection) under which `@direction` should be handled. If undefined, `@direction` is ignored. Alternatively, it can be set to either `'i18n-datatype'` or `'compound-literal'` _(Default: `undefined`)_
178+
* `rdfDirection`: The [mode](https://w3c.github.io/json-ld-api/#dom-jsonldoptions-rdfdirection) under which `@direction` should be handled. If undefined, `@direction` is considered a regular RDF 1.2 literal with a base direction. Alternatively, it can be set to either `'i18n-datatype'` or `'compound-literal'` _(Default: `undefined`)_
179179
* `normalizeLanguageTags`: Whether or not language tags should be normalized to lowercase. _(Default: `false` for JSON-LD 1.1 (and higher), `true` for JSON-LD 1.0)_
180180
* `streamingProfileAllowOutOfOrderPlainType`: When the streaming profile flag is enabled, `@type` entries MUST come before other properties since they may defined a type-scoped context. However, when this flag is enabled, `@type` entries that do NOT define a type-scoped context may appear anywhere just like a regular property.. _(Default: `false`)_
181181
* `skipContextValidation`: If JSON-LD context validation should be skipped. This is useful when parsing large contexts that are known to be valid. _(Default: `false`)_
@@ -197,7 +197,7 @@ new JsonLdParser({
197197
allowSubjectList: false,
198198
validateValueIndexes: false,
199199
defaultGraph: namedNode('http://example.org/graph'),
200-
rdfDirection: 'i18n-datatype',
200+
rdfDirection: undefined,
201201
normalizeLanguageTags: true,
202202
rdfstar: true,
203203
});

lib/Util.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export class Util {
344344
}
345345

346346
// Check @language and @direction
347-
if (valueLanguage && valueDirection && this.parsingContext.rdfDirection) {
347+
if (valueLanguage && valueDirection) {
348348
if (valueType) {
349349
throw new ErrorCoded(`Can not have '@language', '@direction' and '@type' in a value: '${JSON
350350
.stringify(value)}'`,
@@ -360,7 +360,7 @@ export class Util {
360360
}
361361

362362
return [ this.dataFactory.literal(val, valueLanguage) ];
363-
} else if (valueDirection && this.parsingContext.rdfDirection) { // Check @direction
363+
} else if (valueDirection) { // Check @direction
364364
if (valueType) {
365365
throw new ErrorCoded(`Can not have both '@direction' and '@type' in a value: '${JSON.stringify(value)}'`,
366366
ERROR_CODES.INVALID_VALUE_OBJECT);
@@ -645,7 +645,7 @@ export class Util {
645645
if (!defaultDatatype) {
646646
const contextLanguage = Util.getContextValueLanguage(context, key);
647647
const contextDirection = Util.getContextValueDirection(context, key);
648-
if (contextDirection && this.parsingContext.rdfDirection) {
648+
if (contextDirection) {
649649
return this.createLanguageDirectionLiteral(depth, this.intToString(value, defaultDatatype),
650650
contextLanguage, contextDirection);
651651
} else {
@@ -676,7 +676,7 @@ export class Util {
676676
}
677677
return this.dataFactory.literal(value,
678678
this.dataFactory.namedNode(`https://www.w3.org/ns/i18n#${language}_${direction}`));
679-
} else {
679+
} else if (this.parsingContext.rdfDirection === 'compound-literal') {
680680
// Reify the literal.
681681
const valueNode = this.dataFactory.blankNode();
682682
const graph = this.getDefaultGraph();
@@ -689,6 +689,8 @@ export class Util {
689689
this.parsingContext.emitQuad(depth, this.dataFactory.quad(valueNode,
690690
this.dataFactory.namedNode(Util.RDF + 'direction'), this.dataFactory.literal(direction), graph));
691691
return valueNode;
692+
} else {
693+
return this.dataFactory.literal(value, { language: language || '', direction: <'ltr' | 'rtl' | ''> direction });
692694
}
693695
}
694696

test/JsonLdParser-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ describe('JsonLdParser', () => {
795795
}`);
796796
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
797797
DF.quad(DF.namedNode('http://ex.org/myid'), DF.namedNode('http://ex.org/pred1'),
798-
DF.literal('my value')),
798+
DF.literal('my value', { language: '', direction: 'rtl' })),
799799
]);
800800
});
801801

@@ -812,7 +812,7 @@ describe('JsonLdParser', () => {
812812
}`);
813813
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
814814
DF.quad(DF.namedNode('http://ex.org/myid'), DF.namedNode('http://ex.org/pred1'),
815-
DF.literal('my value')),
815+
DF.literal('my value', { language: '', direction: 'rtl' })),
816816
]);
817817
});
818818

@@ -829,7 +829,7 @@ describe('JsonLdParser', () => {
829829
}`);
830830
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
831831
DF.quad(DF.namedNode('http://ex.org/myid'), DF.namedNode('http://ex.org/pred1'),
832-
DF.literal('my value')),
832+
DF.literal('my value', { language: '', direction: 'ltr' })),
833833
]);
834834
});
835835

@@ -2122,7 +2122,7 @@ describe('JsonLdParser', () => {
21222122
}]`);
21232123
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
21242124
DF.quad(DF.namedNode('http://ex.org/myid'), DF.namedNode('http://ex.org/pred1'),
2125-
DF.literal('my value', 'en-us')),
2125+
DF.literal('my value', { language: 'en-us', direction: 'rtl' })),
21262126
]);
21272127
});
21282128

@@ -2137,7 +2137,7 @@ describe('JsonLdParser', () => {
21372137
}]`);
21382138
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
21392139
DF.quad(DF.namedNode('http://ex.org/myid'), DF.namedNode('http://ex.org/pred1'),
2140-
DF.literal('my value')),
2140+
DF.literal('my value', { language: '', direction: 'rtl' })),
21412141
]);
21422142
});
21432143

test/Util-test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,12 @@ describe('Util', () => {
469469

470470
it('with a @value and valid @direction rtl should return a plain literal', async () => {
471471
return expect(util.valueToTerm(context, 'key', { '@value': 'abc', '@direction': 'rtl' }, 0, []))
472-
.resolves.toEqualRdfTermArray([DF.literal('abc')]);
472+
.resolves.toEqualRdfTermArray([DF.literal('abc', { language: '', direction: 'rtl' })]);
473473
});
474474

475475
it('with a @value and valid @direction ltr should return a plain literal', async () => {
476476
return expect(util.valueToTerm(context, 'key', { '@value': 'abc', '@direction': 'ltr' }, 0, []))
477-
.resolves.toEqualRdfTermArray([DF.literal('abc')]);
477+
.resolves.toEqualRdfTermArray([DF.literal('abc', { language: '', direction: 'ltr' })]);
478478
});
479479

480480
it('with a @value and boolean @type should throw an error', async () => {
@@ -649,13 +649,13 @@ describe('Util', () => {
649649
it('with an @direction: rtl and rdfDirection undefined should return a plain literal', async () => {
650650
context = new JsonLdContextNormalized({ key: { '@direction': 'rtl' } });
651651
return expect(await util.valueToTerm(context, 'key', 'abc', 0, []))
652-
.toEqualRdfTermArray([DF.literal('abc')]);
652+
.toEqualRdfTermArray([DF.literal('abc', { language: '', direction: 'rtl' })]);
653653
});
654654

655655
it('with an @direction: rtl, language and rdfDirection undefined should return a plain literal', async () => {
656656
context = new JsonLdContextNormalized({ key: { '@direction': 'rtl', '@language': 'en-us' } });
657657
return expect(await util.valueToTerm(context, 'key', 'abc', 0, []))
658-
.toEqualRdfTermArray([DF.literal('abc', 'en-us')]);
658+
.toEqualRdfTermArray([DF.literal('abc', { language: 'en-us', direction: 'rtl' })]);
659659
});
660660

661661
it('with an @direction: rtl and rdfDirection i18n-datatype should return a plain literal', async () => {
@@ -695,14 +695,14 @@ describe('Util', () => {
695695
it('with a raw value and @direction in the root context should return a plain literal', async () => {
696696
context = new JsonLdContextNormalized({ '@direction': 'rtl' });
697697
return expect(await util.valueToTerm(context, 'key', 'abc', 0, []))
698-
.toEqualRdfTermArray([DF.literal('abc')]);
698+
.toEqualRdfTermArray([DF.literal('abc', { language: '', direction: 'rtl' })]);
699699
});
700700

701701
it('with a raw value and @direction+@language in the root context should return a language literal',
702702
async () => {
703703
context = new JsonLdContextNormalized({ '@direction': 'rtl', '@language': 'en-us' });
704704
return expect(await util.valueToTerm(context, 'key', 'abc', 0, []))
705-
.toEqualRdfTermArray([DF.literal('abc', 'en-us')]);
705+
.toEqualRdfTermArray([DF.literal('abc', { language: 'en-us', direction: 'rtl' })]);
706706
});
707707

708708
it('with a raw value and @direction in the root context for rdfDirection i18n-datatype ' +
@@ -725,7 +725,7 @@ describe('Util', () => {
725725
it('with a raw value and @direction in the context entry should return a plain literal', async () => {
726726
context = new JsonLdContextNormalized({ 'key': { '@direction': 'rtl' }, '@direction': 'ltr' });
727727
return expect(await util.valueToTerm(context, 'key', 'abc', 0, []))
728-
.toEqualRdfTermArray([DF.literal('abc')]);
728+
.toEqualRdfTermArray([DF.literal('abc', { language: '', direction: 'rtl' })]);
729729
});
730730

731731
it('with a raw value and null @direction in the context entry should return a plain literal',
@@ -740,7 +740,7 @@ describe('Util', () => {
740740
context = new JsonLdContextNormalized(
741741
{ 'key': { '@direction': 'rtl', '@language': 'en-us' }, '@direction': 'ltr' });
742742
return expect(await util.valueToTerm(context, 'key', 'abc', 0, []))
743-
.toEqualRdfTermArray([DF.literal('abc', 'en-us')]);
743+
.toEqualRdfTermArray([DF.literal('abc', { language: 'en-us', direction: 'rtl' })]);
744744
});
745745

746746
it('with a raw value and null @direction+@language in the context entry should return a language literal',

0 commit comments

Comments
 (0)