Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement prefix trimming logic for resolve flow.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |