@@ -45,7 +45,8 @@ module.exports = api;
45
45
* @param activeProperty the compacted property associated with the element
46
46
* to compact, null for none.
47
47
* @param element the element to compact.
48
- * @param options the compaction options.
48
+ * @param options the compaction options:
49
+ * [issuer] a jsonld.IdentifierIssuer to use to label blank nodes.
49
50
* @param compactionMap the compaction map to use.
50
51
*
51
52
* @return the compacted value.
@@ -126,6 +127,9 @@ api.compact = ({
126
127
127
128
const rval = { } ;
128
129
130
+ // produce a map of all subjects and name each bnode
131
+ const issuer = options . issuer || new util . IdentifierIssuer ( '_:b' ) ;
132
+
129
133
if ( options . link && '@id' in element ) {
130
134
// store linked element
131
135
if ( ! ( element [ '@id' ] in options . link ) ) {
@@ -262,20 +266,20 @@ api.compact = ({
262
266
activeCtx , itemActiveProperty , '@container' ) || [ ] ;
263
267
264
268
// get simple @graph or @list value if appropriate
265
- const isSimpleGraph = _isSimpleGraph ( expandedItem ) ;
269
+ const isGraph = _isGraph ( expandedItem ) ;
266
270
const isList = _isList ( expandedItem ) ;
267
271
let inner ;
268
272
if ( isList ) {
269
273
inner = expandedItem [ '@list' ] ;
270
- } else if ( isSimpleGraph ) {
274
+ } else if ( isGraph ) {
271
275
inner = expandedItem [ '@graph' ] ;
272
276
}
273
277
274
278
// recursively compact expanded item
275
279
let compactedItem = api . compact ( {
276
280
activeCtx,
277
281
activeProperty : itemActiveProperty ,
278
- element : ( isList || isSimpleGraph ) ? inner : expandedItem ,
282
+ element : ( isList || isGraph ) ? inner : expandedItem ,
279
283
options,
280
284
compactionMap
281
285
} ) ;
@@ -309,22 +313,63 @@ api.compact = ({
309
313
}
310
314
}
311
315
312
- // handle simple @graph
313
- if ( isSimpleGraph && ! container . includes ( '@graph' ) ) {
314
- // wrap using @graph alias
315
- compactedItem = {
316
- [ api . compactIri ( { activeCtx, iri : '@graph' } ) ] : compactedItem
317
- } ;
318
-
319
- // include @index from expanded @graph, if any
320
- if ( '@index' in expandedItem ) {
321
- compactedItem [ api . compactIri ( { activeCtx, iri : '@index' } ) ] =
322
- expandedItem [ '@index' ] ;
323
- }
324
- }
316
+ // Graph object compaction cases
317
+ if ( isGraph ) {
318
+ if ( container . includes ( '@graph' ) && container . includes ( '@id' ) ) {
319
+ // container includes @graph and @id
320
+ // get or create the map object
321
+ let mapObject ;
322
+ if ( itemActiveProperty in rval ) {
323
+ mapObject = rval [ itemActiveProperty ] ;
324
+ } else {
325
+ rval [ itemActiveProperty ] = mapObject = { } ;
326
+ }
327
+ // add compactedItem to map, using value of `@id` or a new blank node identifier
328
+ _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 ,
343
+ { propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
344
+ } else if ( container . includes ( '@graph' ) && _isSimpleGraph ( expandedItem ) ) {
345
+ // container includes @graph but not @id or @index and value is a simple graph object
346
+ // add compact value
347
+ _addValue (
348
+ rval , itemActiveProperty , compactedItem ,
349
+ { propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
350
+ } else {
351
+ // wrap using @graph alias
352
+ compactedItem = {
353
+ [ api . compactIri ( { activeCtx, iri : '@graph' } ) ] : compactedItem
354
+ } ;
325
355
356
+ // include @id from expanded graph, if any
357
+ if ( '@id' in expandedItem ) {
358
+ compactedItem [ api . compactIri ( { activeCtx, iri : '@id' } ) ] =
359
+ expandedItem [ '@id' ] ;
360
+ }
361
+
362
+ // include @index from expanded graph, if any
363
+ if ( '@index' in expandedItem ) {
364
+ compactedItem [ api . compactIri ( { activeCtx, iri : '@index' } ) ] =
365
+ expandedItem [ '@index' ] ;
366
+ }
367
+ _addValue (
368
+ rval , itemActiveProperty , compactedItem ,
369
+ { propertyIsArray : ( ! options . compactArrays || container . includes ( '@set' ) ) } ) ;
370
+ }
371
+ } else if ( container . includes ( '@language' ) || container . includes ( '@index' ) ) {
326
372
// handle language and index maps
327
- if ( container . includes ( '@language' ) || container . includes ( '@index' ) ) {
328
373
// get or create the map object
329
374
let mapObject ;
330
375
if ( itemActiveProperty in rval ) {
@@ -412,9 +457,12 @@ api.compactIri = ({
412
457
containers . push ( '@index' ) ;
413
458
}
414
459
415
- // prefer `['@graph', '@set']` and then `@graph` if value is a simple graph
416
- // TODO: support `@graphId`?
460
+ // prefer most specific container including @graph , prefering @set variations
417
461
if ( _isGraph ( value ) ) {
462
+ containers . push ( '@graph@index@set' ) ;
463
+ containers . push ( '@graph@index' ) ;
464
+ containers . push ( '@graph@id@set' ) ;
465
+ containers . push ( '@graph@id' ) ;
418
466
containers . push ( '@graph@set' ) ;
419
467
containers . push ( '@graph' ) ;
420
468
}
0 commit comments