Skip to content

Commit 3e730fe

Browse files
committed
Support @id and @type map expansion.
1 parent 8ba5954 commit 3e730fe

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

lib/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
389389
// JSON-LD 1.1 support
390390
if(activeCtx.processingMode && activeCtx.processingMode.indexOf('json-ld-1.1') === 0) {
391391
// TODO: @id and @type
392-
validContainers.push('@graph', '@id');
392+
validContainers.push('@graph', '@id', '@type');
393393

394394
// check container length
395395
if(container.includes('@list')) {

lib/expand.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ api.expand = ({
344344
asGraph,
345345
indexKey: '@id'
346346
});
347+
} else if(container.includes('@type') && _isObject(value)) {
348+
// handle type container (skip if value is not an object)
349+
expandedValue = _expandIndexMap({
350+
activeCtx,
351+
options,
352+
activeProperty: key,
353+
value,
354+
expansionMap,
355+
asGraph: false,
356+
indexKey: '@type'
357+
});
347358
} else {
348359
// recurse into @list or @set
349360
const isList = (expandedProperty === '@list');
@@ -671,8 +682,11 @@ function _expandIndexMap(
671682
const rval = [];
672683
const keys = Object.keys(value).sort();
673684
for(let ki = 0; ki < keys.length; ++ki) {
674-
const key = keys[ki];
675-
let val = value[key];
685+
// If indexKey is @id or @type, expand the key appropriately
686+
const key = (indexKey === '@id' || indexKey === '@type') ?
687+
_expandIri(activeCtx, keys[ki], {vocab: (indexKey === '@type'), base: true}) :
688+
keys[ki];
689+
let val = value[keys[ki]];
676690
if(!_isArray(val)) {
677691
val = [val];
678692
}
@@ -691,7 +705,13 @@ function _expandIndexMap(
691705
if(asGraph && !_isGraph(item)) {
692706
item = {'@graph': [item]};
693707
}
694-
if(!(indexKey in item)) {
708+
if(indexKey === '@type') {
709+
if(item['@type']) {
710+
item['@type'] = [key].concat(item['@type']);
711+
} else {
712+
item['@type'] = [key];
713+
}
714+
} else if(!(indexKey in item)) {
695715
item[indexKey] = key;
696716
}
697717
rval.push(item);

tests/test-common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const TEST_TYPES = {
4141
},
4242
'jld:ExpandTest': {
4343
skip: {
44-
regex: [/#t[cmn]/]
44+
regex: [/#t[cn]/, /#tm00[89]/, /#tm01[0-3]/]
4545
},
4646
fn: 'expand',
4747
params: [

0 commit comments

Comments
 (0)