Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 26 additions & 6 deletions lib/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,16 @@ api.expand = async ({
// set the type-scoped context to the context on input, for use later
typeScopedContext = activeCtx;

// Remember the first key found expanding to @type
let typeKey = null;

// look for scoped contexts on `@type`
for(const key of keys) {
const expandedProperty = _expandIri(activeCtx, key, {vocab: true}, options);
if(expandedProperty === '@type') {
// set scoped contexts from @type
// avoid sorting if possible
typeKey = typeKey || key;
const value = element[key];
const types =
Array.isArray(value) ?
Expand Down Expand Up @@ -248,6 +252,7 @@ api.expand = async ({
options,
insideList,
typeScopedContext,
typeKey,
expansionMap});

// get property count on expanded output
Expand Down Expand Up @@ -283,7 +288,10 @@ api.expand = async ({
const types = _getValues(rval, '@type');

// drop null @values unless custom mapped
if(values.length === 0) {
if(_processingMode(activeCtx, 1.1) && types.includes('@json') &&
types.length === 1) {
// Any value of @value is okay if @type: @json
} else if(values.length === 0) {
const mapped = await expansionMap({
unmappedValue: rval,
activeCtx,
Expand All @@ -304,9 +312,6 @@ api.expand = async ({
'Invalid JSON-LD syntax; only strings may be language-tagged.',
'jsonld.SyntaxError',
{code: 'invalid language-tagged value', element: rval});
} else if(_processingMode(activeCtx, 1.1) && types.includes('@json') &&
types.length === 1) {
// Any value of @value is okay if @type: @json
} else if(!types.every(t =>
(_isAbsoluteIri(t) && !(_isString(t) && t.indexOf('_:') === 0) ||
_isEmptyObject(t)))) {
Expand Down Expand Up @@ -402,11 +407,20 @@ async function _expandObject({
options = {},
insideList,
typeScopedContext,
typeKey,
expansionMap
}) {
const keys = Object.keys(element).sort();
const nests = [];
let unexpandedValue;

// Remember inputType to aid with JSON literals
let inputType = element[typeKey];
if(inputType) {
inputType = _asArray(inputType).slice(-1)[0];
inputType = _expandIri(activeCtx, inputType, {vocab: true}, options);
}

for(const key of keys) {
let value = element[key];
let expandedValue;
Expand Down Expand Up @@ -517,8 +531,13 @@ async function _expandObject({
// capture value for later
// "colliding keywords" check prevents this from being set twice
unexpandedValue = value;
_addValue(
expandedParent, '@value', value, {propertyIsArray: options.isFrame});
if(inputType === '@json' && _processingMode(activeCtx, 1.1)) {
// no coercion to array, and retain all values
expandedParent['@value'] = value;
} else {
_addValue(
expandedParent, '@value', value, {propertyIsArray: options.isFrame});
}
continue;
}

Expand Down Expand Up @@ -803,6 +822,7 @@ async function _expandObject({
expandedParent,
options,
insideList,
typeKey,
expansionMap});
}
}
Expand Down
7 changes: 0 additions & 7 deletions tests/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const TEST_TYPES = {
/compact-manifest.jsonld#tin03$/,
/compact-manifest.jsonld#tin04$/,
/compact-manifest.jsonld#tin05$/,
// @json null
/compact-manifest.jsonld#tjs11$/,
// direction
/compact-manifest.jsonld#tdi01$/,
/compact-manifest.jsonld#tdi02$/,
Expand Down Expand Up @@ -113,8 +111,6 @@ const TEST_TYPES = {
/expand-manifest.jsonld#te044$/,
/expand-manifest.jsonld#te048$/,
/expand-manifest.jsonld#te049$/,
/expand-manifest.jsonld#tjs22$/,
/expand-manifest.jsonld#tjs23$/,
// protected null IRI mapping
/expand-manifest.jsonld#tpr28$/,
// remote
Expand Down Expand Up @@ -440,9 +436,6 @@ const TEST_TYPES = {
/toRdf-manifest.jsonld#tdi10$/,
/toRdf-manifest.jsonld#tdi11$/,
/toRdf-manifest.jsonld#tdi12$/,
// JSON literal
/toRdf-manifest.jsonld#tjs22$/,
/toRdf-manifest.jsonld#tjs23$/,
]
},
fn: 'toRDF',
Expand Down