Skip to content

Commit 94f5c8f

Browse files
gkelloggdavidlehn
authored andcommitted
Transform base direction from/toRdf. Uses i18n-datatype value for rdfDirection option only.
1 parent 0648ef6 commit 94f5c8f

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
@@ -26,6 +26,8 @@
2626
- Added support for `@included` blocks
2727
- Skip things that have the form of a keyword, with warning.
2828
- Support for expansion and compaction of values container `"@direction"`.
29+
- Support for RDF transformation of `@direction`
30+
when `rdfDirection` is 'i18n-datatype'.
2931

3032
## 2.0.2 - 2020-01-17
3133

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
@@ -212,9 +212,7 @@ const TEST_TYPES = {
212212
specVersion: ['json-ld-1.0'],
213213
// FIXME
214214
idRegex: [
215-
// direction
216-
/fromRdf-manifest.jsonld#tdi05$/,
217-
/fromRdf-manifest.jsonld#tdi06$/,
215+
// direction (compound-literal)
218216
/fromRdf-manifest.jsonld#tdi11$/,
219217
/fromRdf-manifest.jsonld#tdi12$/,
220218
]
@@ -268,17 +266,7 @@ const TEST_TYPES = {
268266
/toRdf-manifest.jsonld#te075$/,
269267
/toRdf-manifest.jsonld#te111$/,
270268
/toRdf-manifest.jsonld#te112$/,
271-
// direction
272-
/toRdf-manifest.jsonld#tdi01$/,
273-
/toRdf-manifest.jsonld#tdi02$/,
274-
/toRdf-manifest.jsonld#tdi03$/,
275-
/toRdf-manifest.jsonld#tdi04$/,
276-
/toRdf-manifest.jsonld#tdi05$/,
277-
/toRdf-manifest.jsonld#tdi06$/,
278-
/toRdf-manifest.jsonld#tdi07$/,
279-
/toRdf-manifest.jsonld#tdi08$/,
280-
/toRdf-manifest.jsonld#tdi09$/,
281-
/toRdf-manifest.jsonld#tdi10$/,
269+
// direction (compound-literal)
282270
/toRdf-manifest.jsonld#tdi11$/,
283271
/toRdf-manifest.jsonld#tdi12$/,
284272
// unused scoped context

0 commit comments

Comments
 (0)