Skip to content

Commit dc01cdc

Browse files
dlongleydavidlehn
authored andcommitted
Simplify find context url code.
1 parent a57f2a5 commit dc01cdc

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

lib/context.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,8 @@ async function _retrieveContextUrls(input, options) {
10551055
*/
10561056
function _findContextUrls(input, urls, replace, base) {
10571057
if(_isArray(input)) {
1058-
for(let i = 0; i < input.length; ++i) {
1059-
_findContextUrls(input[i], urls, replace, base);
1058+
for(const element of input) {
1059+
_findContextUrls(element, urls, replace, base);
10601060
}
10611061
return;
10621062
}
@@ -1074,31 +1074,29 @@ function _findContextUrls(input, urls, replace, base) {
10741074
}
10751075

10761076
// get @context
1077-
let ctx = input[key];
1077+
const ctx = input[key];
10781078

10791079
if(_isArray(ctx)) {
10801080
// array @context
10811081
let length = ctx.length;
10821082
for(let i = 0; i < length; ++i) {
1083-
let _ctx = ctx[i];
1083+
const _ctx = ctx[i];
10841084
if(_isString(_ctx)) {
1085-
_ctx = prependBase(base, _ctx);
1085+
const prepended = prependBase(base, _ctx);
1086+
const resolved = urls[prepended];
10861087
// replace w/@context if requested
10871088
if(replace) {
1088-
if(urls[_ctx] !== false) {
1089-
_ctx = urls[_ctx];
1090-
if(_isArray(_ctx)) {
1091-
// add flattened context
1092-
Array.prototype.splice.apply(ctx, [i, 1].concat(_ctx));
1093-
i += _ctx.length - 1;
1094-
length = ctx.length;
1095-
} else {
1096-
ctx[i] = _ctx;
1097-
}
1089+
if(_isArray(resolved)) {
1090+
// add flattened context
1091+
Array.prototype.splice.apply(ctx, [i, 1].concat(resolved));
1092+
i += resolved.length - 1;
1093+
length = ctx.length;
1094+
} else {
1095+
ctx[i] = resolved;
10981096
}
1099-
} else if(!(_ctx in urls)) {
1097+
} else if(resolved === undefined) {
11001098
// @context URL found
1101-
urls[_ctx] = false;
1099+
urls[prepended] = false;
11021100
}
11031101
} else {
11041102
// look for scoped context
@@ -1111,15 +1109,16 @@ function _findContextUrls(input, urls, replace, base) {
11111109
}
11121110
} else if(_isString(ctx)) {
11131111
// string @context
1114-
ctx = prependBase(base, ctx);
1112+
const prepended = prependBase(base, ctx);
1113+
const resolved = urls[prepended];
11151114
// replace w/@context if requested
11161115
if(replace) {
1117-
if(urls[ctx] !== false) {
1118-
input[key] = urls[ctx];
1116+
if(resolved !== false) {
1117+
input[key] = resolved;
11191118
}
1120-
} else if(!(ctx in urls)) {
1119+
} else if(resolved === undefined) {
11211120
// @context URL found
1122-
urls[ctx] = false;
1121+
urls[prepended] = false;
11231122
}
11241123
} else {
11251124
// look for scoped context

0 commit comments

Comments
 (0)