Skip to content

Commit e3d8c15

Browse files
tplookerdavidlehn
authored andcommitted
feat: add prependIri expansionMap hook and tests
1 parent 16f3367 commit e3d8c15

File tree

3 files changed

+486
-213
lines changed

3 files changed

+486
-213
lines changed

lib/context.js

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,18 +1043,96 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10431043

10441044
if(relativeTo.vocab && '@vocab' in activeCtx) {
10451045
// prepend vocab
1046-
value = activeCtx['@vocab'] + value;
1046+
const prependedResult = activeCtx['@vocab'] + value;
1047+
let expansionMapResult = undefined;
1048+
if(options && options.expansionMap) {
1049+
// if we are about to expand the value by prepending
1050+
// @vocab then call the expansion map to inform
1051+
// interested callers that this is occurring
1052+
1053+
// TODO: use `await` to support async
1054+
expansionMapResult = options.expansionMap({
1055+
prependedIri: {
1056+
type: '@vocab',
1057+
vocab: activeCtx['@vocab'],
1058+
value,
1059+
result: prependedResult
1060+
},
1061+
activeCtx,
1062+
options
1063+
});
1064+
1065+
}
1066+
if(expansionMapResult !== undefined) {
1067+
value = expansionMapResult;
1068+
} else {
1069+
// the null case preserves value as potentially relative
1070+
value = prependedResult;
1071+
}
10471072
} else if(relativeTo.base && '@base' in activeCtx) {
10481073
// prepend base
10491074
if(activeCtx['@base']) {
1050-
// The null case preserves value as potentially relative
1051-
value = prependBase(prependBase(options.base, activeCtx['@base']), value);
1075+
const prependedResult = prependBase(
1076+
prependBase(options.base, activeCtx['@base']), value);
1077+
1078+
let expansionMapResult = undefined;
1079+
if(options && options.expansionMap) {
1080+
// if we are about to expand the value by prepending
1081+
// @base then call the expansion map to inform
1082+
// interested callers that this is occurring
1083+
1084+
// TODO: use `await` to support async
1085+
expansionMapResult = options.expansionMap({
1086+
prependedIri: {
1087+
type: '@base',
1088+
base: activeCtx['@base'],
1089+
value,
1090+
result: prependedResult
1091+
},
1092+
activeCtx,
1093+
options
1094+
});
1095+
}
1096+
if(expansionMapResult !== undefined) {
1097+
value = expansionMapResult;
1098+
} else {
1099+
// the null case preserves value as potentially relative
1100+
value = prependedResult;
1101+
}
10521102
}
10531103
} else if(relativeTo.base) {
1054-
value = prependBase(options.base, value);
1104+
const prependedResult = prependBase(options.base, value);
1105+
let expansionMapResult = undefined;
1106+
if(options && options.expansionMap) {
1107+
// if we are about to expand the value by prepending
1108+
// @base then call the expansion map to inform
1109+
// interested callers that this is occurring
1110+
1111+
// TODO: use `await` to support async
1112+
expansionMapResult = options.expansionMap({
1113+
prependedIri: {
1114+
type: '@base',
1115+
base: options.base,
1116+
value,
1117+
result: prependedResult
1118+
},
1119+
activeCtx,
1120+
options
1121+
});
1122+
}
1123+
if(expansionMapResult !== undefined) {
1124+
value = expansionMapResult;
1125+
} else {
1126+
value = prependedResult;
1127+
}
10551128
}
10561129

1057-
if(!_isAbsoluteIri(value) && options.expansionMap) {
1130+
if(!_isAbsoluteIri(value) && options && options.expansionMap) {
1131+
// if the result of the expansion is not an absolute iri then
1132+
// call the expansion map to inform interested callers that
1133+
// the resulting value is a relative iri, which can result in
1134+
// it being dropped when converting to other RDF representations
1135+
10581136
// TODO: use `await` to support async
10591137
const expandedResult = options.expansionMap({
10601138
relativeIri: value,

lib/expand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ api.expand = async ({
7777
}) => {
7878

7979
// add expansion map to the processing options
80-
options = { ...options, expansionMap };
80+
options = {...options, expansionMap};
8181

8282
// nothing to expand
8383
if(element === null || element === undefined) {

0 commit comments

Comments
 (0)