Skip to content

Commit 2636300

Browse files
committed
Pass type-scoped context for value expansion.
1 parent 2ee232a commit 2636300

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/expand.js

Lines changed: 14 additions & 7 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,15 @@ 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 || activeCtx.previousContext;
160168
let keys = Object.keys(element).sort();
161169
let mustRevert = !insideIndex;
162170
if(mustRevert && typeScopedContext && keys[0] !== '@context') {
@@ -167,6 +175,7 @@ api.expand = ({
167175
if(expandedProperty === '@id') {
168176
// subject reference found, use type-scoped context to expand it
169177
mustRevert = false;
178+
activeCtx = typeScopedContext;
170179
}
171180
} else if(keys.length <= 2) {
172181
// check if element is a value
@@ -176,6 +185,7 @@ api.expand = ({
176185
if(expandedProperty === '@value') {
177186
// value found, use type-scoped context 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)