@@ -143,8 +143,7 @@ api.compact = ({
143
143
144
144
// process element keys in order
145
145
const keys = Object . keys ( element ) . sort ( ) ;
146
- for ( let ki = 0 ; ki < keys . length ; ++ ki ) {
147
- const expandedProperty = keys [ ki ] ;
146
+ for ( let expandedProperty of keys ) {
148
147
const expandedValue = element [ expandedProperty ] ;
149
148
150
149
// compact @id and @type(s)
@@ -161,9 +160,9 @@ api.compact = ({
161
160
} else {
162
161
// expanded value must be a @type array
163
162
compactedValue = [ ] ;
164
- for ( let vi = 0 ; vi < expandedValue . length ; ++ vi ) {
163
+ for ( let expandedIri of expandedValue ) {
165
164
const compactedType = api . compactIri (
166
- { activeCtx, iri : expandedValue [ vi ] , relativeTo : { vocab : true } } )
165
+ { activeCtx, iri : expandedIri , relativeTo : { vocab : true } } )
167
166
168
167
// Use any scoped context defined on this value
169
168
const ctx = _getContextValue ( activeCtx , compactedType , '@context' ) ;
@@ -257,14 +256,21 @@ api.compact = ({
257
256
relativeTo : { vocab : true } ,
258
257
reverse : insideReverse
259
258
} ) ;
259
+ const nestProperty = ( itemActiveProperty in activeCtx . mappings ) ? activeCtx . mappings [ itemActiveProperty ] [ '@nest' ] : null ;
260
+ let nestResult = rval ;
261
+ if ( nestProperty ) {
262
+ _checkNestProperty ( activeCtx , nestProperty ) ;
263
+ if ( ! _isObject ( rval [ nestProperty ] ) ) {
264
+ rval [ nestProperty ] = { } ;
265
+ }
266
+ nestResult = rval [ nestProperty ] ;
267
+ }
260
268
_addValue (
261
- rval , itemActiveProperty , expandedValue , { propertyIsArray : true } ) ;
269
+ nestResult , itemActiveProperty , expandedValue , { propertyIsArray : true } ) ;
262
270
}
263
271
264
272
// recusively process array values
265
- for ( let vi = 0 ; vi < expandedValue . length ; ++ vi ) {
266
- const expandedItem = expandedValue [ vi ] ;
267
-
273
+ for ( let expandedItem of expandedValue ) {
268
274
// compact property and get container type
269
275
const itemActiveProperty = api . compactIri ( {
270
276
activeCtx,
@@ -273,6 +279,18 @@ api.compact = ({
273
279
relativeTo : { vocab : true } ,
274
280
reverse : insideReverse
275
281
} ) ;
282
+
283
+ // if itemActiveProperty is a @nest property, add values to nestResult, otherwise rval
284
+ const nestProperty = ( itemActiveProperty in activeCtx . mappings ) ? activeCtx . mappings [ itemActiveProperty ] [ '@nest' ] : null ;
285
+ let nestResult = rval ;
286
+ if ( nestProperty ) {
287
+ _checkNestProperty ( activeCtx , nestProperty ) ;
288
+ if ( ! _isObject ( rval [ nestProperty ] ) ) {
289
+ rval [ nestProperty ] = { } ;
290
+ }
291
+ nestResult = rval [ nestProperty ] ;
292
+ }
293
+
276
294
const container = _getContextValue (
277
295
activeCtx , itemActiveProperty , '@container' ) || [ ] ;
278
296
@@ -313,7 +331,7 @@ api.compact = ({
313
331
compactedItem [ api . compactIri ( { activeCtx, iri : '@index' , relativeTo : { vocab : true } } ) ] =
314
332
expandedItem [ '@index' ] ;
315
333
}
316
- } else if ( itemActiveProperty in rval ) {
334
+ } else if ( itemActiveProperty in nestResult ) {
317
335
// can't use @list container for more than 1 list
318
336
throw new JsonLdError (
319
337
'JSON-LD compact error; property has a "@list" @container ' +
@@ -330,10 +348,10 @@ api.compact = ({
330
348
( container . includes ( '@id' ) || container . includes ( '@index' ) && _isSimpleGraph ( expandedItem ) ) ) {
331
349
// get or create the map object
332
350
let mapObject ;
333
- if ( itemActiveProperty in rval ) {
334
- mapObject = rval [ itemActiveProperty ] ;
351
+ if ( itemActiveProperty in nestResult ) {
352
+ mapObject = nestResult [ itemActiveProperty ] ;
335
353
} else {
336
- rval [ itemActiveProperty ] = mapObject = { } ;
354
+ nestResult [ itemActiveProperty ] = mapObject = { } ;
337
355
}
338
356
339
357
// index on @id or @index or alias of @none
@@ -347,7 +365,7 @@ api.compact = ({
347
365
// container includes @graph but not @id or @index and value is a simple graph object
348
366
// add compact value
349
367
_addValue (
350
- rval , itemActiveProperty , compactedItem ,
368
+ nestResult , itemActiveProperty , compactedItem ,
351
369
{ propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
352
370
} else {
353
371
// wrap using @graph alias
@@ -367,18 +385,18 @@ api.compact = ({
367
385
expandedItem [ '@index' ] ;
368
386
}
369
387
_addValue (
370
- rval , itemActiveProperty , compactedItem ,
388
+ nestResult , itemActiveProperty , compactedItem ,
371
389
{ propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
372
390
}
373
391
} else if ( container . includes ( '@language' ) || container . includes ( '@index' ) ||
374
392
container . includes ( '@id' ) || container . includes ( '@type' ) ) {
375
393
// handle language and index maps
376
394
// get or create the map object
377
395
let mapObject ;
378
- if ( itemActiveProperty in rval ) {
379
- mapObject = rval [ itemActiveProperty ] ;
396
+ if ( itemActiveProperty in nestResult ) {
397
+ mapObject = nestResult [ itemActiveProperty ] ;
380
398
} else {
381
- rval [ itemActiveProperty ] = mapObject = { } ;
399
+ nestResult [ itemActiveProperty ] = mapObject = { } ;
382
400
}
383
401
384
402
let key ;
@@ -431,7 +449,7 @@ api.compact = ({
431
449
432
450
// add compact value
433
451
_addValue (
434
- rval , itemActiveProperty , compactedItem ,
452
+ nestResult , itemActiveProperty , compactedItem ,
435
453
{ propertyIsArray : isArray } ) ;
436
454
}
437
455
}
@@ -907,3 +925,18 @@ function _selectTerm(
907
925
908
926
return null ;
909
927
}
928
+
929
+ /**
930
+ * The value of `@nest` in the term definition must either be `@nest`, or a term
931
+ * which resolves to `@nest`.
932
+ *
933
+ * @param activeCtx the active context.
934
+ * @param nestProperty a term in the active context or `@nest`.
935
+ */
936
+ function _checkNestProperty ( activeCtx , nestProperty ) {
937
+ if ( _expandIri ( activeCtx , nestProperty , { vocab : true } ) !== '@nest' ) {
938
+ throw new JsonLdError (
939
+ 'JSON-LD compact error; nested property must have an @nest value resolving to @nest.' ,
940
+ 'jsonld.SyntaxError' , { code : 'invalid @nest value' } ) ;
941
+ }
942
+ }
0 commit comments