Skip to content

Commit db3703e

Browse files
author
Elodie Thiéblin
committed
feat(compat): take into account native xsd types in "@type" in context
fix digitalbazaar#466
1 parent b7bc6d6 commit db3703e

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

lib/compact.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const {
99
isArray: _isArray,
1010
isObject: _isObject,
1111
isString: _isString,
12-
isUndefined: _isUndefined
12+
isUndefined: _isUndefined,
13+
isBoolean,
14+
isDouble,
15+
isNumber
1316
} = require('./types');
1417

1518
const {
@@ -38,6 +41,12 @@ const {
3841
asArray: _asArray,
3942
compareShortestLeast: _compareShortestLeast
4043
} = require('./util');
44+
const {
45+
XSD_BOOLEAN,
46+
XSD_DOUBLE,
47+
XSD_INTEGER,
48+
XSD_STRING
49+
} = require('./constants');
4150

4251
const api = {};
4352
module.exports = api;
@@ -996,6 +1005,10 @@ api.compactValue = ({activeCtx, activeProperty, value, options}) => {
9961005
if('@direction' in value && value['@direction'] === direction) {
9971006
return value['@value'];
9981007
}
1008+
if(!('@type' in value) && type === XSD_STRING) {
1009+
return value['@value'];
1010+
}
1011+
9991012
}
10001013

10011014
// return just the value of @value if all are true:
@@ -1139,6 +1152,7 @@ function _selectTerm(
11391152
prefs.push('@none');
11401153

11411154
const containerMap = activeCtx.inverse[iri];
1155+
11421156
for(const container of containers) {
11431157
// if container not available in the map, continue
11441158
if(!(container in containerMap)) {
@@ -1155,6 +1169,32 @@ function _selectTerm(
11551169
// select term
11561170
return typeOrLanguageValueMap[pref];
11571171
}
1172+
1173+
// special case of @type from native types in context and no @type and no
1174+
// @language specified in value
1175+
if(containerMap[container].hasOwnProperty('@type') &&
1176+
value !== null &&
1177+
!value.hasOwnProperty('@type') &&
1178+
!value.hasOwnProperty('@language')
1179+
) {
1180+
const containerType = containerMap[container]['@type'];
1181+
const valueLiteralValue = value['@value'];
1182+
if(isBoolean(valueLiteralValue) &&
1183+
containerType.hasOwnProperty(XSD_BOOLEAN)
1184+
) {
1185+
return containerType[XSD_BOOLEAN];
1186+
} else if(isDouble(valueLiteralValue) &&
1187+
containerType.hasOwnProperty(XSD_DOUBLE)
1188+
) {
1189+
return containerType[XSD_DOUBLE];
1190+
} else if(isNumber(valueLiteralValue) &&
1191+
containerType.hasOwnProperty(XSD_INTEGER)
1192+
) {
1193+
return containerType[XSD_INTEGER];
1194+
} else if(containerType.hasOwnProperty(XSD_STRING)) {
1195+
return containerType[XSD_STRING];
1196+
}
1197+
}
11581198
}
11591199

11601200
return null;

0 commit comments

Comments
 (0)