Skip to content

Commit e4a1376

Browse files
committed
Compact using @id and @type maps.
1 parent 3e730fe commit e4a1376

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

lib/compact.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,45 @@ api.compact = ({
368368
rval, itemActiveProperty, compactedItem,
369369
{propertyIsArray: (!options.compactArrays || container.includes('@set'))});
370370
}
371-
} else if(container.includes('@language') || container.includes('@index')) {
372-
// handle language and index maps
371+
} else if(container.includes('@language') || container.includes('@index') ||
372+
container.includes('@id') || container.includes('@type')) {
373+
// handle language and index maps
373374
// get or create the map object
374-
let mapObject;
375+
let mapObject, key;
375376
if(itemActiveProperty in rval) {
376377
mapObject = rval[itemActiveProperty];
377378
} else {
378379
rval[itemActiveProperty] = mapObject = {};
379380
}
380381

382+
if(container.includes('@language')) {
381383
// if container is a language map, simplify compacted value to
382384
// a simple string
383-
if(container.includes('@language') && _isValue(compactedItem)) {
384-
compactedItem = compactedItem['@value'];
385+
if(_isValue(compactedItem)) {
386+
compactedItem = compactedItem['@value'];
387+
}
388+
key = expandedItem['@language'];
389+
} else if(container.includes('@index')) {
390+
key = expandedItem['@index'];
391+
} else if(container.includes('@id')) {
392+
const idKey = api.compactIri({activeCtx, iri: '@id', vocab: true});
393+
key = compactedItem[idKey] || '@none';
394+
delete compactedItem[idKey];
395+
} else if(container.includes('@type')) {
396+
const typeKey = api.compactIri({activeCtx, iri: '@type', vocab: true});
397+
var types;
398+
[key, ...types] = [].concat(compactedItem[typeKey] || ['@none']);
399+
switch(types.length) {
400+
case 0: delete compactedItem[typeKey]; break;
401+
case 1: compactedItem[typeKey] = types[0]; break;
402+
default: compactedItem[typeKey] = types; break;
403+
}
385404
}
386405

387406
// add compact value to map object using key from expanded value
388407
// based on the container type
389-
const c = container.includes('@language') ? '@language' : '@index';
390-
_addValue(mapObject, expandedItem[c], compactedItem);
408+
_addValue(mapObject, key, compactedItem,
409+
{propertyIsArray: container.includes('@set')});
391410
} else {
392411
// use an array if: compactArrays flag is false,
393412
// @container is @set or @list , value is an empty
@@ -465,6 +484,11 @@ api.compactIri = ({
465484
containers.push('@graph@id');
466485
containers.push('@graph@set');
467486
containers.push('@graph');
487+
} else if(_isObject(value) && !_isValue(value)) {
488+
containers.push('@id@set');
489+
containers.push('@id');
490+
containers.push('@type@set');
491+
containers.push('@type');
468492
}
469493

470494
// defaults for term selection based on type/language

tests/test-common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const manifest = options.manifest || {
2929
const TEST_TYPES = {
3030
'jld:CompactTest': {
3131
skip: {
32-
regex: [/#t0073/, /#t[acmnps]/]
32+
regex: [/#t0073/, /#t[acnps]/, /#tm00[7-9]/, /#tm01[0-2]/]
3333
},
3434
fn: 'compact',
3535
params: [

0 commit comments

Comments
 (0)