Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compat): take into account native xsd types in "@type" in context #487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion lib/compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const {
isArray: _isArray,
isObject: _isObject,
isString: _isString,
isUndefined: _isUndefined
isUndefined: _isUndefined,
isBoolean,
isDouble,
isNumber
} = require('./types');

const {
Expand Down Expand Up @@ -38,6 +41,12 @@ const {
asArray: _asArray,
compareShortestLeast: _compareShortestLeast
} = require('./util');
const {
XSD_BOOLEAN,
XSD_DOUBLE,
XSD_INTEGER,
XSD_STRING
} = require('./constants');

const api = {};
module.exports = api;
Expand Down Expand Up @@ -996,6 +1005,10 @@ api.compactValue = ({activeCtx, activeProperty, value, options}) => {
if('@direction' in value && value['@direction'] === direction) {
return value['@value'];
}
if(!('@type' in value) && type === XSD_STRING) {
return value['@value'];
}

}

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

const containerMap = activeCtx.inverse[iri];

for(const container of containers) {
// if container not available in the map, continue
if(!(container in containerMap)) {
Expand All @@ -1155,6 +1169,32 @@ function _selectTerm(
// select term
return typeOrLanguageValueMap[pref];
}

// special case of @type from native types in context and no @type and no
// @language specified in value
if(containerMap[container].hasOwnProperty('@type') &&
value !== null &&
!value.hasOwnProperty('@type') &&
!value.hasOwnProperty('@language')
) {
const containerType = containerMap[container]['@type'];
const valueLiteralValue = value['@value'];
if(isBoolean(valueLiteralValue) &&
containerType.hasOwnProperty(XSD_BOOLEAN)
) {
return containerType[XSD_BOOLEAN];
} else if(isDouble(valueLiteralValue) &&
containerType.hasOwnProperty(XSD_DOUBLE)
) {
return containerType[XSD_DOUBLE];
} else if(isNumber(valueLiteralValue) &&
containerType.hasOwnProperty(XSD_INTEGER)
) {
return containerType[XSD_INTEGER];
} else if(containerType.hasOwnProperty(XSD_STRING)) {
return containerType[XSD_STRING];
}
}
}

return null;
Expand Down