Skip to content

Commit cc9e819

Browse files
committed
simplified path stripping logic
- #8: simplified and cleaned up path stripping code - #1: side effect removed unnecessary condition - #10: side effect removed unnecessary code
1 parent 2f28040 commit cc9e819

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

harextract.html

+7-21
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
// extract and parse interesting entries
153153
let harents = [];
154154
let skipped = [];
155+
let commonpath = null;
155156
for ([id,ent] of har.log.entries.entries()) {
156157
let harent = { id: id };
157158
let fullpath = new URL(ent.request.url).pathname.split('/');
@@ -165,32 +166,17 @@
165166
harent.error = "Can't decode.";
166167
if (harent.error) {
167168
console.log(`harextract: skipped ${harent.name} (${id}): ${harent.error}`);
168-
harent.path = harent.pathcomps.join('/'); // TODO: fix me! kludge to get paths into skipped.
169169
skipped.push(harent);
170170
} else {
171-
harents.push(harent);
171+
commonpath = fullpath.slice(0, [...fullpath,null].findIndex((p, n) => p !== (commonpath||fullpath)[n])); // ;)
172+
harents.push(harent); // note: we can now guarantee that if harents has items, commonpath is set.
172173
}
173174
}
174175

175-
// now make a few passes to strip common path prefixes
176-
// todo: code can be simplified by maintaining common components above then
177-
// doing a single second pass to slice + join.
178-
if (harents.length > 0) {
179-
for (var ncommon = 0, allsame = true; allsame && ncommon < harents[0].pathcomps.length; ++ ncommon) {
180-
allsame = true;
181-
let refpart = harents[0].pathcomps[ncommon];
182-
for (ent of harents) {
183-
if (ent.pathcomps[ncommon] != refpart) {
184-
allsame = false;
185-
break;
186-
}
187-
}
188-
}
189-
for (ent of harents) {
190-
ent.path = (ncommon < 0 ? '' : ent.pathcomps.slice(ncommon - 1).join('/'));
191-
}
192-
}
193-
176+
// set path strings for all files; for non-skipped files, also strip common path prefixes.
177+
harents.forEach(ent => ent.path = ent.pathcomps.slice(commonpath.length).join('/'));
178+
skipped.forEach(ent => ent.path = ent.pathcomps.join('/'));
179+
194180
let entsort = function (a,b) {
195181
let p = strCompare(a.path, b.path);
196182
return (p != 0) ? p : strCompare(a.name, b.name);

0 commit comments

Comments
 (0)