Skip to content

Remove shortcut from compactIri when iri is a keyword #218

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

Merged
merged 3 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 21 additions & 20 deletions lib/compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ api.compact = ({
}

// use keyword alias and add value
const alias = api.compactIri({activeCtx, iri: expandedProperty});
const alias = api.compactIri({activeCtx, iri: expandedProperty, relativeTo: {vocab: true}});
const isArray = _isArray(compactedValue) && expandedValue.length === 0;
_addValue(rval, alias, compactedValue, {propertyIsArray: isArray});
continue;
Expand Down Expand Up @@ -194,7 +194,7 @@ api.compact = ({

if(Object.keys(compactedValue).length > 0) {
// use keyword alias and add value
const alias = api.compactIri({activeCtx, iri: expandedProperty});
const alias = api.compactIri({activeCtx, iri: expandedProperty, relativeTo: {vocab: true}});
_addValue(rval, alias, compactedValue);
}

Expand All @@ -211,7 +211,7 @@ api.compact = ({
}

// use keyword alias and add value
const alias = api.compactIri({activeCtx, iri: expandedProperty});
const alias = api.compactIri({activeCtx, iri: expandedProperty, relativeTo: {vocab: true}});
_addValue(rval, alias, expandedValue);
continue;
}
Expand All @@ -220,7 +220,7 @@ api.compact = ({
if(expandedProperty !== '@graph' && expandedProperty !== '@list' &&
_isKeyword(expandedProperty)) {
// use keyword alias and add value as is
const alias = api.compactIri({activeCtx, iri: expandedProperty});
const alias = api.compactIri({activeCtx, iri: expandedProperty, relativeTo: {vocab: true}});
_addValue(rval, alias, expandedValue);
continue;
}
Expand Down Expand Up @@ -289,12 +289,12 @@ api.compact = ({
if(!container.includes('@list')) {
// wrap using @list alias
compactedItem = {
[api.compactIri({activeCtx, iri: '@list'})]: compactedItem
[api.compactIri({activeCtx, iri: '@list', relativeTo: {vocab: true}})]: compactedItem
};

// include @index from expanded @list, if any
if('@index' in expandedItem) {
compactedItem[api.compactIri({activeCtx, iri: '@index'})] =
compactedItem[api.compactIri({activeCtx, iri: '@index', relativeTo: {vocab: true}})] =
expandedItem['@index'];
}
} else if(itemActiveProperty in rval) {
Expand All @@ -312,12 +312,12 @@ api.compact = ({
if(isSimpleGraph && !container.includes('@graph')) {
// wrap using @graph alias
compactedItem = {
[api.compactIri({activeCtx, iri: '@graph'})]: compactedItem
[api.compactIri({activeCtx, iri: '@graph', relativeTo: {vocab: true}})]: compactedItem
};

// include @index from expanded @graph, if any
if('@index' in expandedItem) {
compactedItem[api.compactIri({activeCtx, iri: '@index'})] =
compactedItem[api.compactIri({activeCtx, iri: '@index', relativeTo: {vocab: true}})] =
expandedItem['@index'];
}
}
Expand Down Expand Up @@ -393,12 +393,13 @@ api.compactIri = ({

const inverseCtx = activeCtx.getInverse();

// if term is a keyword, it can only be compacted to a simple alias
if(_isKeyword(iri)) {
if(iri in inverseCtx) {
return inverseCtx[iri]['@none']['@type']['@none'];
}
return iri;
// if term is a keyword, it can may be compacted to a simple alias
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type: it can may be => it may be compacted

if(_isKeyword(iri) &&
iri in inverseCtx &&
'@none' in inverseCtx[iri] &&
'@type' in inverseCtx[iri]['@none'] &&
'@none' in inverseCtx[iri]['@none']['@type']) {
return inverseCtx[iri]['@none']['@type']['@none'];
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to leave this in place, and exit quickly if an iri is found, and otherwise continue. That would allow for more complicated behavior, while keeping the typical case fast.

// use inverse context to pick a term if iri is relative to vocab
Expand Down Expand Up @@ -621,20 +622,20 @@ api.compactValue = ({activeCtx, activeProperty, value}) => {

// preserve @index
if(preserveIndex) {
rval[api.compactIri({activeCtx, iri: '@index'})] = value['@index'];
rval[api.compactIri({activeCtx, iri: '@index', relativeTo: {vocab: true}})] = value['@index'];
}

if('@type' in value) {
// compact @type IRI
rval[api.compactIri({activeCtx, iri: '@type'})] = api.compactIri(
rval[api.compactIri({activeCtx, iri: '@type', relativeTo: {vocab: true}})] = api.compactIri(
{activeCtx, iri: value['@type'], relativeTo: {vocab: true}});
} else if('@language' in value) {
// alias @language
rval[api.compactIri({activeCtx, iri: '@language'})] = value['@language'];
rval[api.compactIri({activeCtx, iri: '@language', relativeTo: {vocab: true}})] = value['@language'];
}

// alias @value
rval[api.compactIri({activeCtx, iri: '@value'})] = value['@value'];
rval[api.compactIri({activeCtx, iri: '@value', relativeTo: {vocab: true}})] = value['@value'];

return rval;
}
Expand All @@ -651,7 +652,7 @@ api.compactValue = ({activeCtx, activeProperty, value}) => {
}

return {
[api.compactIri({activeCtx, iri: '@id'})]: compacted
[api.compactIri({activeCtx, iri: '@id', relativeTo: {vocab: true}})]: compacted
};
};

Expand Down Expand Up @@ -698,7 +699,7 @@ api.removePreserve = (ctx, input, options) => {
}

// handle in-memory linked nodes
const idAlias = api.compactIri({activeCtx: ctx, iri: '@id'});
const idAlias = api.compactIri({activeCtx: ctx, iri: '@id', relativeTo: {vocab: true}});
if(idAlias in input) {
const id = input[idAlias];
if(id in options.link) {
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonld.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ jsonld.compact = util.callbackify(async function(input, ctx, options) {
// add context and/or @graph
if(_isArray(compacted)) {
// use '@graph' keyword
const graphAlias = _compactIri({activeCtx, iri: '@graph'});
const graphAlias = _compactIri({activeCtx, iri: '@graph', relativeTo: {vocab: true}});
const graph = compacted;
compacted = {};
if(hasContext) {
Expand All @@ -224,7 +224,7 @@ jsonld.compact = util.callbackify(async function(input, ctx, options) {

if(options.framing) {
// get graph alias
const graph = _compactIri({activeCtx, iri: '@graph'});
const graph = _compactIri({activeCtx, iri: '@graph', relativeTo: {vocab: true}});
// remove @preserve from results
options.link = {};
compacted[graph] = _removePreserve(activeCtx, compacted[graph], options);
Expand Down
1 change: 0 additions & 1 deletion tests/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const manifest = options.manifest || {
const TEST_TYPES = {
'jld:CompactTest': {
skip: {
regex: [/#t0073/],
specVersion: ['json-ld-1.1']
},
fn: 'compact',
Expand Down