Skip to content

Commit

Permalink
Implement prefix trimming logic for resolve flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
samccone committed Jun 1, 2019
1 parent 6d7f417 commit 9a85841
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
28 changes: 28 additions & 0 deletions explorer/app/src/resolve/trim.test.ts
@@ -0,0 +1,28 @@
import { findTrims } from "./trim";

it("finds no trims when no /", () => {
expect(findTrims(["wow"], ["wo"])).toEqual({});
});

it("finds no trims", () => {
expect(findTrims(["wo/ok"], ["../ok"])).toEqual({});
});

it("finds trims", () => {
expect(findTrims(["a/b/ok"], ["b/ok"])).toEqual({
"a/": 1
});

expect(findTrims(["b/ok"], ["a/b/ok"])).toEqual({
"a/": 1
});
});

it("finds multi trims", () => {
expect(
findTrims(["a/b/ok", "nothing", "../a/b.js"], ["b/ok", "a/b.js"])
).toEqual({
"a/": 1,
"../": 1
});
});
56 changes: 56 additions & 0 deletions explorer/app/src/resolve/trim.ts
@@ -0,0 +1,56 @@
function generatePrefixList(items: string[]) {
const kk: { [prefix: string]: number } = {};

for (const i of items) {
const components = i.split("/");
let prev = components[0] + "/";
for (const c of components.slice(1)) {
kk[prev + c] = kk[prev + c] || 0;
kk[prev + c]++;
prev = prev + c + "/";
}
}

let prefixItems = [];
for (const k of Object.keys(kk)) {
prefixItems.push({ prefix: k, count: kk[k] });
}

return prefixItems.sort((a, b) => b.count - a.count);
}

/**
* Given two lists of strings find common prefixes that when removed would
* align the two lists of strings.
* @param items
* @param items2
*/
export function findTrims(items: string[], items2: string[]) {
const prefixList1 = generatePrefixList(items);
const prefixList2 = generatePrefixList(items2);
const recommendedTrims: { [prefix: string]: number } = {};
for (let i = 0; i < prefixList1.length; i++) {
for (let j = 0; j < prefixList2.length; j++) {
if (prefixList1[i].prefix.length > prefixList2[j].prefix.length) {
const idx = prefixList1[i].prefix.indexOf(prefixList2[j].prefix);
if (idx > -1) {
const str = prefixList1[i].prefix.slice(0, idx);
if (str !== "") {
recommendedTrims[str] = recommendedTrims[str] || 0;
recommendedTrims[str]++;
}
}
} else {
const idx = prefixList2[j].prefix.indexOf(prefixList1[i].prefix);
if (idx > -1) {
const str = prefixList2[j].prefix.slice(0, idx);
if (str !== "") {
recommendedTrims[str] = recommendedTrims[str] || 0;
recommendedTrims[str]++;
}
}
}
}
}
return recommendedTrims;
}

0 comments on commit 9a85841

Please sign in to comment.