@@ -315,31 +315,23 @@ api.compact = ({
315
315
316
316
// Graph object compaction cases
317
317
if ( isGraph ) {
318
- if ( container . includes ( '@graph' ) && container . includes ( '@id' ) ) {
319
- // container includes @graph and @id
318
+ if ( container . includes ( '@graph' ) &&
319
+ ( container . includes ( '@id' ) || container . includes ( '@index' ) && _isSimpleGraph ( expandedItem ) ) ) {
320
320
// get or create the map object
321
321
let mapObject ;
322
322
if ( itemActiveProperty in rval ) {
323
323
mapObject = rval [ itemActiveProperty ] ;
324
324
} else {
325
325
rval [ itemActiveProperty ] = mapObject = { } ;
326
326
}
327
+
328
+ // TODO: future change will use @none in both cases.
329
+ const key = container . includes ( '@id' ) ?
330
+ ( expandedItem [ '@id' ] || issuer . getId ( null ) ) :
331
+ ( expandedItem [ '@index' ] || '@none' ) ;
327
332
// add compactedItem to map, using value of `@id` or a new blank node identifier
328
333
_addValue (
329
- mapObject , ( expandedItem [ '@id' ] || issuer . getId ( null ) ) , compactedItem ,
330
- { propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
331
- } else if ( container . includes ( '@graph' ) && container . includes ( '@index' ) && _isSimpleGraph ( expandedItem ) ) {
332
- // container includes @graph and @index and item is a simple graph
333
- // get or create the map object
334
- let mapObject ;
335
- if ( itemActiveProperty in rval ) {
336
- mapObject = rval [ itemActiveProperty ] ;
337
- } else {
338
- rval [ itemActiveProperty ] = mapObject = { } ;
339
- }
340
- // add compactedItem to map, using value of `@index` or `@none`
341
- _addValue (
342
- mapObject , ( expandedItem [ '@index' ] || '@none' ) , compactedItem ,
334
+ mapObject , key , compactedItem ,
343
335
{ propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
344
336
} else if ( container . includes ( '@graph' ) && _isSimpleGraph ( expandedItem ) ) {
345
337
// container includes @graph but not @id or @index and value is a simple graph object
@@ -372,13 +364,14 @@ api.compact = ({
372
364
container . includes ( '@id' ) || container . includes ( '@type' ) ) {
373
365
// handle language and index maps
374
366
// get or create the map object
375
- let mapObject , key ;
367
+ let mapObject ;
376
368
if ( itemActiveProperty in rval ) {
377
369
mapObject = rval [ itemActiveProperty ] ;
378
370
} else {
379
371
rval [ itemActiveProperty ] = mapObject = { } ;
380
372
}
381
373
374
+ let key ;
382
375
if ( container . includes ( '@language' ) ) {
383
376
// if container is a language map, simplify compacted value to
384
377
// a simple string
@@ -394,19 +387,25 @@ api.compact = ({
394
387
delete compactedItem [ idKey ] ;
395
388
} else if ( container . includes ( '@type' ) ) {
396
389
const typeKey = api . compactIri ( { activeCtx, iri : '@type' , vocab : true } ) ;
397
- var types ;
390
+ let types ;
398
391
[ key , ...types ] = [ ] . concat ( compactedItem [ typeKey ] || [ '@none' ] ) ;
399
392
switch ( types . length ) {
400
- case 0 : delete compactedItem [ typeKey ] ; break ;
401
- case 1 : compactedItem [ typeKey ] = types [ 0 ] ; break ;
402
- default : compactedItem [ typeKey ] = types ; break ;
393
+ case 0 :
394
+ delete compactedItem [ typeKey ] ;
395
+ break ;
396
+ case 1 :
397
+ compactedItem [ typeKey ] = types [ 0 ] ;
398
+ break ;
399
+ default :
400
+ compactedItem [ typeKey ] = types ;
401
+ break ;
403
402
}
404
403
}
405
404
406
405
// add compact value to map object using key from expanded value
407
406
// based on the container type
408
- _addValue ( mapObject , key , compactedItem ,
409
- { propertyIsArray : container . includes ( '@set' ) } ) ;
407
+ _addValue (
408
+ mapObject , key , compactedItem , { propertyIsArray : container . includes ( '@set' ) } ) ;
410
409
} else {
411
410
// use an array if: compactArrays flag is false,
412
411
// @container is @set or @list , value is an empty
0 commit comments