Skip to content

Commit c6345dc

Browse files
gkelloggdavidlehn
authored andcommitted
* Separate processing @id and @type in expansion, due to differing rules.
1 parent fe25ca8 commit c6345dc

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

lib/compact.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,31 +219,47 @@ api.compact = async ({
219219
for(const expandedProperty of keys) {
220220
const expandedValue = element[expandedProperty];
221221

222-
// compact @id and @type(s)
223-
if(expandedProperty === '@id' || expandedProperty === '@type') {
224-
// if using a type-scoped context, resolve type values against previous
225-
// context
226-
const isType = expandedProperty === '@type';
227-
const valueContext = isType ?
228-
(activeCtx.previousContext || activeCtx) : activeCtx;
222+
// compact @id
223+
if(expandedProperty === '@id') {
229224
let compactedValue = _asArray(expandedValue).map(
230225
expandedIri => api.compactIri({
231-
activeCtx: valueContext,
226+
activeCtx,
227+
iri: expandedIri,
228+
relativeTo: {vocab: false}
229+
}));
230+
if(compactedValue.length === 1) {
231+
compactedValue = compactedValue[0];
232+
}
233+
234+
// use keyword alias and add value
235+
const alias = api.compactIri(
236+
{activeCtx, iri: '@id', relativeTo: {vocab: true}});
237+
238+
rval[alias] = compactedValue;
239+
continue;
240+
}
241+
242+
// compact @type(s)
243+
if(expandedProperty === '@type') {
244+
// resolve type values against previous context
245+
let compactedValue = _asArray(expandedValue).map(
246+
expandedIri => api.compactIri({
247+
activeCtx: inputCtx,
232248
iri: expandedIri,
233-
relativeTo: {vocab: isType}
249+
relativeTo: {vocab: true}
234250
}));
235251
if(compactedValue.length === 1) {
236252
compactedValue = compactedValue[0];
237253
}
238254

239255
// use keyword alias and add value
240256
const alias = api.compactIri(
241-
{activeCtx, iri: expandedProperty, relativeTo: {vocab: true}});
257+
{activeCtx, iri: '@type', relativeTo: {vocab: true}});
242258
const container = _getContextValue(
243259
activeCtx, alias, '@container') || [];
244260

245261
// treat as array for @type if @container includes @set
246-
const typeAsSet = isType &&
262+
const typeAsSet =
247263
container.includes('@set') &&
248264
_processingMode(activeCtx, 1.1);
249265
const isArray =

lib/expand.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ api.expand = async ({
209209
{activeCtx, localCtx: element['@context'], options});
210210
}
211211

212+
// set the type-scoped context to the context on input, for use later
213+
typeScopedContext = activeCtx;
214+
212215
// look for scoped contexts on `@type`
213216
for(const key of keys) {
214217
const expandedProperty = _expandIri(activeCtx, key, {vocab: true}, options);
@@ -220,14 +223,13 @@ api.expand = async ({
220223
Array.isArray(value) ?
221224
(value.length > 1 ? value.slice().sort() : value) : [value];
222225
for(const type of types) {
223-
const ctx = _getContextValue(
224-
activeCtx.previousContext || activeCtx, type, '@context');
226+
const ctx = _getContextValue(typeScopedContext, type, '@context');
225227
if(!_isUndefined(ctx)) {
226228
activeCtx = await _processContext({
227229
activeCtx,
228230
localCtx: ctx,
229231
options,
230-
isTypeScopedContext: true
232+
propagate: false
231233
});
232234
}
233235
}
@@ -494,7 +496,7 @@ async function _expandObject({
494496
expandedParent, '@type',
495497
_asArray(value).map(v =>
496498
_isString(v) ?
497-
_expandIri(activeCtx.previousContext || activeCtx, v,
499+
_expandIri(typeScopedContext, v,
498500
{base: true, vocab: true}, options) : v),
499501
{propertyIsArray: options.isFrame});
500502
continue;

0 commit comments

Comments
 (0)