Skip to content

Commit 13b3a06

Browse files
committed
Transform base direction from/toRdf. Uses i18n-datatype value for rdfDirection option only.
1 parent 7b2b803 commit 13b3a06

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
### Added
2323
- Support for expansion and compaction of values container `"@direction"`.
24+
- Support for RDF transformation of `@direction`
25+
when `rdfDirection` is 'i18n-datatype'.
2426

2527
## 2.0.2 - 2020-01-17
2628

lib/fromRdf.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const {
2929
XSD_STRING,
3030
} = require('./constants');
3131

32+
const REGEX_BCP47 = /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/;
33+
3234
const api = {};
3335
module.exports = api;
3436

@@ -41,7 +43,13 @@ module.exports = api;
4143
* @return a Promise that resolves to the JSON-LD output.
4244
*/
4345
api.fromRDF = async (
44-
dataset, {useRdfType = false, useNativeTypes = false}) => {
46+
dataset,
47+
{
48+
useRdfType = false,
49+
useNativeTypes = false,
50+
rdfDirection = null
51+
}
52+
) => {
4553
const defaultGraph = {};
4654
const graphMap = {'@default': defaultGraph};
4755
const referencedOnce = {};
@@ -79,7 +87,7 @@ api.fromRDF = async (
7987
continue;
8088
}
8189

82-
const value = _RDFToObject(o, useNativeTypes);
90+
const value = _RDFToObject(o, useNativeTypes, rdfDirection);
8391
util.addValue(node, p, value, {propertyIsArray: true});
8492

8593
// object may be an RDF list/partial list node but we can't know easily
@@ -270,7 +278,7 @@ api.fromRDF = async (
270278
*
271279
* @return the JSON-LD object.
272280
*/
273-
function _RDFToObject(o, useNativeTypes) {
281+
function _RDFToObject(o, useNativeTypes, rdfDirection) {
274282
// convert NamedNode/BlankNode object to JSON-LD
275283
if(o.termType.endsWith('Node')) {
276284
return {'@id': o.value};
@@ -320,6 +328,17 @@ function _RDFToObject(o, useNativeTypes) {
320328
if(![XSD_BOOLEAN, XSD_INTEGER, XSD_DOUBLE, XSD_STRING].includes(type)) {
321329
rval['@type'] = type;
322330
}
331+
} else if(rdfDirection === 'i18n-datatype' &&
332+
type.startsWith('https://www.w3.org/ns/i18n#'))
333+
{
334+
const [, language, direction] = type.split(/[#_]/);
335+
if(language.length > 0) {
336+
rval['@language'] = language;
337+
if(!language.match(REGEX_BCP47)) {
338+
console.warn(`@language must be valid BCP47: ${language}`);
339+
}
340+
}
341+
rval['@direction'] = direction;
323342
} else if(type !== XSD_STRING) {
324343
rval['@type'] = type;
325344
}

lib/toRdf.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
128128
}
129129

130130
// convert list, value or node object to triple
131-
const object = _objectToRDF(item, issuer, dataset, graphTerm);
131+
const object =
132+
_objectToRDF(item, issuer, dataset, graphTerm, options.rdfDirection);
132133
// skip null objects (they are relative IRIs)
133134
if(object) {
134135
dataset.push({
@@ -154,7 +155,7 @@ function _graphToRDF(dataset, graph, graphTerm, issuer, options) {
154155
*
155156
* @return the head of the list.
156157
*/
157-
function _listToRDF(list, issuer, dataset, graphTerm) {
158+
function _listToRDF(list, issuer, dataset, graphTerm, rdfDirection) {
158159
const first = {termType: 'NamedNode', value: RDF_FIRST};
159160
const rest = {termType: 'NamedNode', value: RDF_REST};
160161
const nil = {termType: 'NamedNode', value: RDF_NIL};
@@ -165,7 +166,7 @@ function _listToRDF(list, issuer, dataset, graphTerm) {
165166
let subject = result;
166167

167168
for(const item of list) {
168-
const object = _objectToRDF(item, issuer, dataset, graphTerm);
169+
const object = _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection);
169170
const next = {termType: 'BlankNode', value: issuer.getId()};
170171
dataset.push({
171172
subject,
@@ -184,7 +185,7 @@ function _listToRDF(list, issuer, dataset, graphTerm) {
184185

185186
// Tail of list
186187
if(last) {
187-
const object = _objectToRDF(last, issuer, dataset, graphTerm);
188+
const object = _objectToRDF(last, issuer, dataset, graphTerm, rdfDirection);
188189
dataset.push({
189190
subject,
190191
predicate: first,
@@ -213,7 +214,7 @@ function _listToRDF(list, issuer, dataset, graphTerm) {
213214
*
214215
* @return the RDF literal or RDF resource.
215216
*/
216-
function _objectToRDF(item, issuer, dataset, graphTerm) {
217+
function _objectToRDF(item, issuer, dataset, graphTerm, rdfDirection) {
217218
const object = {};
218219

219220
// convert value object to RDF
@@ -243,6 +244,14 @@ function _objectToRDF(item, issuer, dataset, graphTerm) {
243244
} else if(types.isNumber(value)) {
244245
object.value = value.toFixed(0);
245246
object.datatype.value = datatype || XSD_INTEGER;
247+
} else if(rdfDirection === 'i18n-datatype' &&
248+
'@direction' in item)
249+
{
250+
const datatype = 'https://www.w3.org/ns/i18n#' +
251+
(item['@language'] || '') +
252+
`_${item['@direction']}`;
253+
object.datatype.value = datatype;
254+
object.value = value;
246255
} else if('@language' in item) {
247256
object.value = value;
248257
object.datatype.value = datatype || RDF_LANGSTRING;
@@ -252,7 +261,8 @@ function _objectToRDF(item, issuer, dataset, graphTerm) {
252261
object.datatype.value = datatype || XSD_STRING;
253262
}
254263
} else if(graphTypes.isList(item)) {
255-
const _list = _listToRDF(item['@list'], issuer, dataset, graphTerm);
264+
const _list =
265+
_listToRDF(item['@list'], issuer, dataset, graphTerm, rdfDirection);
256266
object.termType = _list.termType;
257267
object.value = _list.value;
258268
} else {

tests/test-common.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ const TEST_TYPES = {
262262
specVersion: ['json-ld-1.0'],
263263
// FIXME
264264
idRegex: [
265-
// direction
266-
/fromRdf-manifest.jsonld#tdi05$/,
267-
/fromRdf-manifest.jsonld#tdi06$/,
265+
// direction (compound-literal)
268266
/fromRdf-manifest.jsonld#tdi11$/,
269267
/fromRdf-manifest.jsonld#tdi12$/,
270268
]
@@ -353,17 +351,7 @@ const TEST_TYPES = {
353351
/toRdf-manifest.jsonld#tpr36$/,
354352
/toRdf-manifest.jsonld#tpr37$/,
355353
/toRdf-manifest.jsonld#tpr39$/,
356-
// direction
357-
/toRdf-manifest.jsonld#tdi01$/,
358-
/toRdf-manifest.jsonld#tdi02$/,
359-
/toRdf-manifest.jsonld#tdi03$/,
360-
/toRdf-manifest.jsonld#tdi04$/,
361-
/toRdf-manifest.jsonld#tdi05$/,
362-
/toRdf-manifest.jsonld#tdi06$/,
363-
/toRdf-manifest.jsonld#tdi07$/,
364-
/toRdf-manifest.jsonld#tdi08$/,
365-
/toRdf-manifest.jsonld#tdi09$/,
366-
/toRdf-manifest.jsonld#tdi10$/,
354+
// direction (compound-literal)
367355
/toRdf-manifest.jsonld#tdi11$/,
368356
/toRdf-manifest.jsonld#tdi12$/,
369357
// unused scoped context

0 commit comments

Comments
 (0)