Skip to content

Commit 9308adf

Browse files
committed
Stylistic improvements.
1 parent 00e321c commit 9308adf

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

lib/compact.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -315,31 +315,23 @@ api.compact = ({
315315

316316
// Graph object compaction cases
317317
if(isGraph) {
318-
if(container.includes('@graph') && container.includes('@id')) {
319-
// container includes @graph and @id
318+
if(container.includes('@graph') &&
319+
(container.includes('@id') || container.includes('@index') && _isSimpleGraph(expandedItem))) {
320320
// get or create the map object
321321
let mapObject;
322322
if(itemActiveProperty in rval) {
323323
mapObject = rval[itemActiveProperty];
324324
} else {
325325
rval[itemActiveProperty] = mapObject = {};
326326
}
327+
328+
// TODO: future change will use @none in both cases.
329+
const key = container.includes('@id') ?
330+
(expandedItem['@id'] || issuer.getId(null)) :
331+
(expandedItem['@index'] || '@none');
327332
// add compactedItem to map, using value of `@id` or a new blank node identifier
328333
_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,
334+
mapObject, key, compactedItem,
343335
{propertyIsArray: (!options.compactArrays || container.includes('@set'))});
344336
} else if(container.includes('@graph') && _isSimpleGraph(expandedItem)) {
345337
// container includes @graph but not @id or @index and value is a simple graph object
@@ -372,13 +364,14 @@ api.compact = ({
372364
container.includes('@id') || container.includes('@type')) {
373365
// handle language and index maps
374366
// get or create the map object
375-
let mapObject, key;
367+
let mapObject;
376368
if(itemActiveProperty in rval) {
377369
mapObject = rval[itemActiveProperty];
378370
} else {
379371
rval[itemActiveProperty] = mapObject = {};
380372
}
381373

374+
let key;
382375
if(container.includes('@language')) {
383376
// if container is a language map, simplify compacted value to
384377
// a simple string
@@ -394,19 +387,25 @@ api.compact = ({
394387
delete compactedItem[idKey];
395388
} else if(container.includes('@type')) {
396389
const typeKey = api.compactIri({activeCtx, iri: '@type', vocab: true});
397-
var types;
390+
let types;
398391
[key, ...types] = [].concat(compactedItem[typeKey] || ['@none']);
399392
switch(types.length) {
400-
case 0: delete compactedItem[typeKey]; break;
401-
case 1: compactedItem[typeKey] = types[0]; break;
402-
default: compactedItem[typeKey] = types; break;
393+
case 0:
394+
delete compactedItem[typeKey];
395+
break;
396+
case 1:
397+
compactedItem[typeKey] = types[0];
398+
break;
399+
default:
400+
compactedItem[typeKey] = types;
401+
break;
403402
}
404403
}
405404

406405
// add compact value to map object using key from expanded value
407406
// based on the container type
408-
_addValue(mapObject, key, compactedItem,
409-
{propertyIsArray: container.includes('@set')});
407+
_addValue(
408+
mapObject, key, compactedItem, {propertyIsArray: container.includes('@set')});
410409
} else {
411410
// use an array if: compactArrays flag is false,
412411
// @container is @set or @list , value is an empty

lib/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ api.createTermDefinition = (activeCtx, localCtx, term, defined) => {
400400
{code: 'invalid container mapping', context: localCtx});
401401
}
402402
} else if(container.includes('@graph')) {
403-
if(container.some(key => key != '@graph' && key != '@id' && key != '@index' && key != '@set')) {
403+
if(container.some(key => key !== '@graph' && key !== '@id' && key !== '@index' && key !== '@set')) {
404404
throw new JsonLdError(
405405
'Invalid JSON-LD syntax; @context @container with @graph must have no other values ' +
406406
'other than @id, @index, and @set',

0 commit comments

Comments
 (0)