@@ -29,7 +29,8 @@ const {
29
29
} = require ( './context' ) ;
30
30
31
31
const {
32
- removeBase : _removeBase
32
+ removeBase : _removeBase ,
33
+ prependBase : _prependBase
33
34
} = require ( './url' ) ;
34
35
35
36
const {
@@ -226,7 +227,8 @@ api.compact = async ({
226
227
expandedIri => api . compactIri ( {
227
228
activeCtx,
228
229
iri : expandedIri ,
229
- relativeTo : { vocab : false }
230
+ relativeTo : { vocab : false } ,
231
+ base : options . base
230
232
} ) ) ;
231
233
if ( compactedValue . length === 1 ) {
232
234
compactedValue = compactedValue [ 0 ] ;
@@ -485,7 +487,8 @@ api.compact = async ({
485
487
// index on @id or @index or alias of @none
486
488
const key = ( container . includes ( '@id' ) ?
487
489
expandedItem [ '@id' ] : expandedItem [ '@index' ] ) ||
488
- api . compactIri ( { activeCtx, iri : '@none' , vocab : true } ) ;
490
+ api . compactIri ( { activeCtx, iri : '@none' ,
491
+ relativeTo : { vocab : true } } ) ;
489
492
// add compactedItem to map, using value of `@id` or a new blank
490
493
// node identifier
491
494
@@ -570,7 +573,7 @@ api.compact = async ({
570
573
const indexKey = _getContextValue (
571
574
activeCtx , itemActiveProperty , '@index' ) || '@index' ;
572
575
const containerKey = api . compactIri (
573
- { activeCtx, iri : indexKey , vocab : true } ) ;
576
+ { activeCtx, iri : indexKey , relativeTo : { vocab : true } } ) ;
574
577
if ( indexKey === '@index' ) {
575
578
key = expandedItem [ '@index' ] ;
576
579
delete compactedItem [ containerKey ] ;
@@ -595,14 +598,15 @@ api.compact = async ({
595
598
}
596
599
}
597
600
} else if ( container . includes ( '@id' ) ) {
598
- const idKey = api . compactIri ( { activeCtx, iri : '@id' , vocab : true } ) ;
601
+ const idKey = api . compactIri ( { activeCtx, iri : '@id' ,
602
+ relativeTo : { vocab : true } } ) ;
599
603
key = compactedItem [ idKey ] ;
600
604
delete compactedItem [ idKey ] ;
601
605
} else if ( container . includes ( '@type' ) ) {
602
606
const typeKey = api . compactIri ( {
603
607
activeCtx,
604
608
iri : '@type' ,
605
- vocab : true
609
+ relativeTo : { vocab : true }
606
610
} ) ;
607
611
let types ;
608
612
[ key , ...types ] = _asArray ( compactedItem [ typeKey ] || [ ] ) ;
@@ -634,7 +638,8 @@ api.compact = async ({
634
638
635
639
// if compacting this value which has no key, index on @none
636
640
if ( ! key ) {
637
- key = api . compactIri ( { activeCtx, iri : '@none' , vocab : true } ) ;
641
+ key = api . compactIri ( { activeCtx, iri : '@none' ,
642
+ relativeTo : { vocab : true } } ) ;
638
643
}
639
644
// add compact value to map object using key from expanded value
640
645
// based on the container type
@@ -676,6 +681,7 @@ api.compact = async ({
676
681
* @param relativeTo options for how to compact IRIs:
677
682
* vocab: true to split after @vocab, false not to.
678
683
* @param reverse true if a reverse property is being compacted, false if not.
684
+ * @param base the absolute URL to use for compacting document-relative IRIs.
679
685
*
680
686
* @return the compacted term, prefix, keyword alias, or the original IRI.
681
687
*/
@@ -684,7 +690,8 @@ api.compactIri = ({
684
690
iri,
685
691
value = null ,
686
692
relativeTo = { vocab : false } ,
687
- reverse = false
693
+ reverse = false ,
694
+ base = null
688
695
} ) => {
689
696
// can't compact null
690
697
if ( iri === null ) {
@@ -933,7 +940,16 @@ api.compactIri = ({
933
940
934
941
// compact IRI relative to base
935
942
if ( ! relativeTo . vocab ) {
936
- return _removeBase ( activeCtx [ '@base' ] , iri ) ;
943
+ if ( '@base' in activeCtx ) {
944
+ if ( ! activeCtx [ '@base' ] ) {
945
+ // The None case preserves rval as potentially relative
946
+ return iri ;
947
+ } else {
948
+ return _removeBase ( _prependBase ( base , activeCtx [ '@base' ] ) , iri ) ;
949
+ }
950
+ } else {
951
+ return _removeBase ( base , iri ) ;
952
+ }
937
953
}
938
954
939
955
// return IRI as is
@@ -1050,8 +1066,11 @@ api.compactValue = ({activeCtx, activeProperty, value, options}) => {
1050
1066
const expandedProperty = _expandIri ( activeCtx , activeProperty , { vocab : true } ,
1051
1067
options ) ;
1052
1068
const type = _getContextValue ( activeCtx , activeProperty , '@type' ) ;
1053
- const compacted = api . compactIri (
1054
- { activeCtx, iri : value [ '@id' ] , relativeTo : { vocab : type === '@vocab' } } ) ;
1069
+ const compacted = api . compactIri ( {
1070
+ activeCtx,
1071
+ iri : value [ '@id' ] ,
1072
+ relativeTo : { vocab : type === '@vocab' } ,
1073
+ base : options . base } ) ;
1055
1074
1056
1075
// compact to scalar
1057
1076
if ( type === '@id' || type === '@vocab' || expandedProperty === '@graph' ) {
0 commit comments