Skip to content

Commit ce1c070

Browse files
committed
Pass type-scoped context for value expansion.
1 parent d6fd37c commit ce1c070

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/expand.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ module.exports = api;
5252
* @param insideList true if the element is a list, false if not.
5353
* @param insideIndex true if the element is inside an index container,
5454
* false if not.
55+
* @param typeScopedContext an optional type-scoped active context for
56+
* expanding values of nodes that were expressed according to
57+
* a type-scoped context.
5558
* @param expansionMap(info) a function that can be used to custom map
5659
* unmappable values (or to throw an error when they are detected);
5760
* if this function returns `undefined` then the default behavior
@@ -66,6 +69,7 @@ api.expand = ({
6669
options = {},
6770
insideList = false,
6871
insideIndex = false,
72+
typeScopedContext = null,
6973
expansionMap = () => undefined
7074
}) => {
7175
// nothing to expand
@@ -152,11 +156,16 @@ api.expand = ({
152156

153157
// recursively expand object:
154158

155-
// First determine if any type-scoped context should be reverted; it should
156-
// only be reverted when the following are all true:
159+
// first, expand the active property
160+
const expandedActiveProperty = _expandIri(
161+
activeCtx, activeProperty, {vocab: true}, options);
162+
163+
// second, determine if any type-scoped context should be reverted; it
164+
// should only be reverted when the following are all true:
157165
// 1. `element` is not a value or subject reference
158166
// 2. `insideIndex` is false
159-
const typeScopedContext = activeCtx.previousContext ? activeCtx : null;
167+
typeScopedContext = typeScopedContext ||
168+
(activeCtx.previousContext ? activeCtx : null);
160169
let keys = Object.keys(element).sort();
161170
let mustRevert = !insideIndex;
162171
if(mustRevert && typeScopedContext && keys[0] !== '@context') {
@@ -165,7 +174,7 @@ api.expand = ({
165174
const expandedProperty = _expandIri(
166175
typeScopedContext, keys[0], {vocab: true}, options);
167176
if(expandedProperty === '@id') {
168-
// subject reference found, use type-scoped context to expand it
177+
// subject reference found, do not revert
169178
mustRevert = false;
170179
}
171180
} else if(keys.length <= 2) {
@@ -174,8 +183,9 @@ api.expand = ({
174183
const expandedProperty = _expandIri(
175184
typeScopedContext, key, {vocab: true}, options);
176185
if(expandedProperty === '@value') {
177-
// value found, use type-scoped context to expand it
186+
// value found, ensure type-scoped context is used to expand it
178187
mustRevert = false;
188+
activeCtx = typeScopedContext;
179189
break;
180190
}
181191
}
@@ -218,10 +228,6 @@ api.expand = ({
218228
}
219229
}
220230

221-
// expand the active property
222-
const expandedActiveProperty = _expandIri(
223-
activeCtx, activeProperty, {vocab: true}, options);
224-
225231
// process each key and value in element, ignoring @nest content
226232
let rval = {};
227233
_expandObject({
@@ -232,6 +238,7 @@ api.expand = ({
232238
expandedParent: rval,
233239
options,
234240
insideList,
241+
typeScopedContext,
235242
expansionMap});
236243

237244
// get property count on expanded output

0 commit comments

Comments
 (0)