@@ -141,41 +141,43 @@ api.compact = ({
141
141
options . link [ element [ '@id' ] ] . push ( { expanded : element , compacted : rval } ) ;
142
142
}
143
143
144
+ // apply any context defined on an alias of @type
145
+ // if key is @type and any compacted value is a term having a local
146
+ // context, overlay that context
147
+ const types = element [ '@type' ] || [ ] ;
148
+ for ( const type of types ) {
149
+ const compactedType = api . compactIri (
150
+ { activeCtx, iri : type , relativeTo : { vocab : true } } )
151
+
152
+ // Use any scoped context defined on this value
153
+ const ctx = _getContextValue ( activeCtx , compactedType , '@context' ) ;
154
+ if ( ctx ) {
155
+ activeCtx = _processContext ( { activeCtx, localCtx : ctx , options} ) ;
156
+ }
157
+ }
158
+
144
159
// process element keys in order
145
160
const keys = Object . keys ( element ) . sort ( ) ;
146
161
for ( const expandedProperty of keys ) {
147
162
const expandedValue = element [ expandedProperty ] ;
148
163
149
164
// compact @id and @type(s)
150
165
if ( expandedProperty === '@id' || expandedProperty === '@type' ) {
151
- let compactedValue ;
152
-
153
- // compact single @id
154
- if ( _isString ( expandedValue ) ) {
155
- compactedValue = api . compactIri ( {
166
+ let compactedValue = [ ] . concat ( expandedValue ) . map (
167
+ expandedIri => api . compactIri ( {
156
168
activeCtx,
157
- iri : expandedValue ,
158
- relativeTo : { vocab : ( expandedProperty === '@type' ) }
159
- } ) ;
160
- } else {
161
- // expanded value must be a @type array
162
- compactedValue = [ ] ;
163
- for ( const expandedIri of expandedValue ) {
164
- const compactedType = api . compactIri (
165
- { activeCtx, iri : expandedIri , relativeTo : { vocab : true } } )
166
-
167
- // Use any scoped context defined on this value
168
- const ctx = _getContextValue ( activeCtx , compactedType , '@context' ) ;
169
- if ( ctx ) {
170
- activeCtx = _processContext ( { activeCtx, localCtx : ctx , options} ) ;
169
+ iri : expandedIri ,
170
+ relativeTo : {
171
+ vocab : expandedProperty === '@type'
171
172
}
172
-
173
- compactedValue . push ( compactedType ) ;
174
- }
173
+ } ) ) ;
174
+ if ( compactedValue . length === 1 ) {
175
+ compactedValue = compactedValue [ 0 ] ;
175
176
}
176
177
177
178
// use keyword alias and add value
178
- const alias = api . compactIri ( { activeCtx, iri : expandedProperty , relativeTo : { vocab : true } } ) ;
179
+ const alias = api . compactIri (
180
+ { activeCtx, iri : expandedProperty , relativeTo : { vocab : true } } ) ;
179
181
const isArray = _isArray ( compactedValue ) && expandedValue . length === 0 ;
180
182
_addValue ( rval , alias , compactedValue , { propertyIsArray : isArray } ) ;
181
183
continue ;
0 commit comments