Skip to content

Commit

Permalink
improved edge case handling where index outside bounds of found tags
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jun 2, 2024
1 parent 7928dbf commit 776882d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions find-tags-by-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ function findTagsByPath(xml, path, options) {

const path0 = typeof path[0] === "string" ? { name: path[0] } : path[0];
let tags = findTagsByName(xml, path0.name, { debug, nested: false });
if (typeof path0.index === "number") tags = [tags[path0.index]];
if (typeof tags !== "undefined" && typeof path0.index === "number") {
if (typeof tags[path0.index] === "undefined") {
tags = [];
} else {
tags = [tags[path0.index]];
}
}
if (debug) console.log("first tags are:", tags);

path = path.slice(1);
Expand All @@ -35,7 +41,13 @@ function findTagsByPath(xml, path, options) {
}
}
tags = allSubTags;
if (typeof part.index === "number") tags = [tags[part.index]];
if (typeof part.index === "number") {
if (typeof tags[part.index] === "undefined") {
tags = [];
} else {
tags = [tags[part.index]];
}
}
}
return tags;
}
Expand Down
16 changes: 14 additions & 2 deletions find-tags-by-path.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export default function findTagsByPath(xml, path, options) {

const path0 = typeof path[0] === "string" ? { name: path[0] } : path[0];
let tags = findTagsByName(xml, path0.name, { debug, nested: false });
if (typeof path0.index === "number") tags = [tags[path0.index]];
if (typeof tags !== "undefined" && typeof path0.index === "number") {
if (typeof tags[path0.index] === "undefined") {
tags = [];
} else {
tags = [tags[path0.index]];
}
}
if (debug) console.log("first tags are:", tags);

path = path.slice(1);
Expand All @@ -35,7 +41,13 @@ export default function findTagsByPath(xml, path, options) {
}
}
tags = allSubTags;
if (typeof part.index === "number") tags = [tags[part.index]];
if (typeof part.index === "number") {
if (typeof tags[part.index] === "undefined") {
tags = [];
} else {
tags = [tags[part.index]];
}
}
}
return tags;
}

0 comments on commit 776882d

Please sign in to comment.