♻️ refactor getGithubUserContribution

This commit is contained in:
platane
2022-04-21 19:18:27 +02:00
committed by Platane
parent d6e930af5b
commit 40b26d0110
6 changed files with 2572 additions and 2620 deletions

View File

@@ -7,41 +7,22 @@ describe("getGithubUserContribution", () => {
await promise;
});
it("should get colorScheme", async () => {
const { colorScheme } = await promise;
expect(colorScheme).toEqual([
"#ebedf0",
"#9be9a8",
"#40c463",
"#30a14e",
"#216e39",
]);
});
it("should get around 365 cells", async () => {
const { cells } = await promise;
const cells = await promise;
expect(cells.length).toBeGreaterThanOrEqual(365);
expect(cells.length).toBeLessThanOrEqual(365 + 7);
});
it("cells should have x / y coords representing to a 7 x (365/7) (minus unfilled last row)", async () => {
const { cells, colorScheme } = await promise;
const cells = await promise;
expect(cells.length).toBeGreaterThan(300);
expect(colorScheme).toEqual([
"#ebedf0",
"#9be9a8",
"#40c463",
"#30a14e",
"#216e39",
]);
const undefinedDays = Array.from({ length: Math.floor(365 / 7) })
.map((x) => Array.from({ length: 7 }).map((y) => ({ x, y })))
.flat()
.filter(({ x, y }) => cells.some((c) => c.x === x && c.y === y));
.filter(({ x, y }) => cells.some((c: any) => c.x === x && c.y === y));
expect(undefinedDays).toEqual([]);
});

View File

@@ -39,21 +39,9 @@ export const getGithubUserContribution = async (
return parseUserPage(resText);
};
const defaultColorScheme = [
"#ebedf0",
"#9be9a8",
"#40c463",
"#30a14e",
"#216e39",
];
const parseUserPage = (content: string) => {
const $ = cheerio.load(content);
//
// "parse" colorScheme
const colorScheme = [...defaultColorScheme];
//
// parse cells
const rawCells = $(".js-calendar-graph rect[data-count]")
@@ -63,13 +51,9 @@ const parseUserPage = (content: string) => {
const count = +x.attribs["data-count"];
const date = x.attribs["data-date"];
const color = colorScheme[level];
if (!color) throw new Error("could not determine the color of the cell");
return {
svgPosition: getSvgPosition(x),
color,
level,
count,
date,
};
@@ -95,7 +79,7 @@ const parseUserPage = (content: string) => {
y: yRange.indexOf(svgPosition.y),
}));
return { cells, colorScheme };
return cells;
};
// returns the position of the svg elements, accounting for it's transform and it's parent transform
@@ -126,4 +110,4 @@ const getSvgPosition = (
export type Res = Awaited<ReturnType<typeof getGithubUserContribution>>;
export type Cell = Res["cells"][number];
export type Cell = Res[number];