Skip to content

Commit c9b7a11

Browse files
committed
Index maps on @none or an alias for json-ld/json-ld.org#569.
1 parent 04fca55 commit c9b7a11

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

lib/compact.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ module.exports = api;
4747
* to compact, null for none.
4848
* @param element the element to compact.
4949
* @param options the compaction options:
50-
* [issuer] a jsonld.IdentifierIssuer to use to label blank nodes.
5150
* @param compactionMap the compaction map to use.
5251
*
5352
* @return the compacted value.
@@ -134,9 +133,6 @@ api.compact = ({
134133

135134
const rval = {};
136135

137-
// produce a map of all subjects and name each bnode
138-
const issuer = options.issuer || new util.IdentifierIssuer('_:b');
139-
140136
if(options.link && '@id' in element) {
141137
// store linked element
142138
if(!(element['@id'] in options.link)) {
@@ -340,10 +336,9 @@ api.compact = ({
340336
rval[itemActiveProperty] = mapObject = {};
341337
}
342338

343-
// TODO: future change will use @none in both cases.
344-
const key = container.includes('@id') ?
345-
(expandedItem['@id'] || issuer.getId(null)) :
346-
(expandedItem['@index'] || '@none');
339+
// index on @id or @index or alias of @none
340+
const key = (container.includes('@id') ? expandedItem['@id'] : expandedItem['@index']) ||
341+
api.compactIri({activeCtx, iri: '@none', vocab: true});
347342
// add compactedItem to map, using value of `@id` or a new blank node identifier
348343
_addValue(
349344
mapObject, key, compactedItem,
@@ -398,12 +393,12 @@ api.compact = ({
398393
key = expandedItem['@index'];
399394
} else if(container.includes('@id')) {
400395
const idKey = api.compactIri({activeCtx, iri: '@id', vocab: true});
401-
key = compactedItem[idKey] || '@none';
396+
key = compactedItem[idKey];
402397
delete compactedItem[idKey];
403398
} else if(container.includes('@type')) {
404399
const typeKey = api.compactIri({activeCtx, iri: '@type', vocab: true});
405400
let types;
406-
[key, ...types] = [].concat(compactedItem[typeKey] || ['@none']);
401+
[key, ...types] = [].concat(compactedItem[typeKey] || []);
407402
switch(types.length) {
408403
case 0:
409404
delete compactedItem[typeKey];
@@ -417,6 +412,10 @@ api.compact = ({
417412
}
418413
}
419414

415+
// if compacting this value which has no key, index on @none
416+
if(!key) {
417+
key = api.compactIri({activeCtx, iri: '@none', vocab: true});
418+
}
420419
// add compact value to map object using key from expanded value
421420
// based on the container type
422421
_addValue(

lib/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ api.isKeyword = v => {
843843
case '@index':
844844
case '@language':
845845
case '@list':
846+
case '@none':
846847
case '@omitDefault':
847848
case '@preserve':
848849
case '@requireAll':

lib/expand.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ api.expand = ({
341341

342342
if(container.includes('@language') && _isObject(value)) {
343343
// handle language map container (skip if value is not an object)
344-
expandedValue = _expandLanguageMap(value);
344+
expandedValue = _expandLanguageMap(termCtx, value);
345345
} else if(container.includes('@index') && _isObject(value)) {
346346
// handle index container (skip if value is not an object)
347347
const asGraph = container.includes('@graph');
@@ -665,21 +665,21 @@ function _expandValue({activeCtx, activeProperty, value}) {
665665
/**
666666
* Expands a language map.
667667
*
668+
* @param activeCtx the active context to use.
668669
* @param languageMap the language map to expand.
669670
*
670671
* @return the expanded language map.
671672
*/
672-
function _expandLanguageMap(languageMap) {
673+
function _expandLanguageMap(activeCtx, languageMap) {
673674
const rval = [];
674675
const keys = Object.keys(languageMap).sort();
675-
for(let ki = 0; ki < keys.length; ++ki) {
676-
const key = keys[ki];
676+
for(let key of keys) {
677+
const expandedKey = _expandIri(activeCtx, key, {vocab: true})
677678
let val = languageMap[key];
678679
if(!_isArray(val)) {
679680
val = [val];
680681
}
681-
for(let vi = 0; vi < val.length; ++vi) {
682-
const item = val[vi];
682+
for(let item of val) {
683683
if(item === null) {
684684
// null values are allowed (8.5) but ignored (3.1)
685685
continue;
@@ -690,10 +690,11 @@ function _expandLanguageMap(languageMap) {
690690
'jsonld.SyntaxError',
691691
{code: 'invalid language map value', languageMap: languageMap});
692692
}
693-
rval.push({
694-
'@value': item,
695-
'@language': key.toLowerCase()
696-
});
693+
const val = {'@value': item};
694+
if(expandedKey !== '@none') {
695+
val['@language'] = key.toLowerCase();
696+
}
697+
rval.push(val);
697698
}
698699
}
699700
return rval;
@@ -703,21 +704,27 @@ function _expandIndexMap(
703704
{activeCtx, options, activeProperty, value, expansionMap, asGraph, indexKey}) {
704705
const rval = [];
705706
const keys = Object.keys(value).sort();
706-
for(let ki = 0; ki < keys.length; ++ki) {
707+
for(let key of keys) {
707708
// if indexKey is @type, there may be a context defined for it
708-
const ctx = _getContextValue(activeCtx, keys[ki], '@context');
709+
const ctx = _getContextValue(activeCtx, key, '@context');
709710
if(ctx) {
710711
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
711712
}
712713

713-
// if indexKey is @id or @type, expand the key appropriately
714-
const key = (indexKey === '@id' || indexKey === '@type') ?
715-
_expandIri(activeCtx, keys[ki], {vocab: (indexKey === '@type'), base: true}) :
716-
keys[ki];
717-
let val = value[keys[ki]];
714+
let val = value[key];
718715
if(!_isArray(val)) {
719716
val = [val];
720717
}
718+
719+
// expand for @type, but also for @none
720+
const expandedKey = _expandIri(activeCtx, key, {vocab: true})
721+
if(indexKey === '@id' ) {
722+
// expand document relative
723+
key = _expandIri(activeCtx, key, {base: true});
724+
} else if(indexKey === '@type') {
725+
key = expandedKey;
726+
}
727+
721728
val = api.expand({
722729
activeCtx,
723730
activeProperty,
@@ -726,20 +733,20 @@ function _expandIndexMap(
726733
insideList: false,
727734
expansionMap
728735
});
729-
for(let vi = 0; vi < val.length; ++vi) {
730-
let item = val[vi];
731-
736+
for(let item of val) {
732737
// If this is also a @graph container, turn items into graphs
733738
if(asGraph && !_isGraph(item)) {
734739
item = {'@graph': [item]};
735740
}
736741
if(indexKey === '@type') {
737-
if(item['@type']) {
742+
if(expandedKey === '@none') {
743+
// ignore @none
744+
} else if(item['@type']) {
738745
item['@type'] = [key].concat(item['@type']);
739746
} else {
740747
item['@type'] = [key];
741748
}
742-
} else if(!(indexKey in item)) {
749+
} else if(expandedKey !== '@none' && !(indexKey in item)) {
743750
item[indexKey] = key;
744751
}
745752
rval.push(item);

0 commit comments

Comments
 (0)