Skip to content

Commit

Permalink
refactor: move formatting in google-rank.ts to utils/format.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Jul 17, 2023
1 parent 988474f commit 5ff44da
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 44 deletions.
48 changes: 27 additions & 21 deletions lib/google-rank.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/google-rank.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/utils/format.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { GoogleWebsiteRank } from "./google";
/**
* Formats the rank of a keyword as a string.
* @param keyword - The keyword string.
* @param rank - The rank of the keyword.
* @returns A formatted string. The rank will be displayed as a question mark if it is undefined.
*/
export declare function formatKeywordRank(keyword: string, rank?: GoogleWebsiteRank): string;
23 changes: 23 additions & 0 deletions lib/utils/format.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/utils/format.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { googleGetWebsiteRank, GoogleWebsiteRank } from "./google";
export { formatKeywordRank } from "./format";
4 changes: 3 additions & 1 deletion lib/utils/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/utils/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 7 additions & 20 deletions src/google-rank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import chalk from "chalk";
import { program } from "commander";
import { googleGetWebsiteRank, GoogleWebsiteRank } from "./utils";
import * as utils from "./utils";

type RankPromise = Promise<utils.GoogleWebsiteRank | undefined>;

async function run() {
program
Expand All @@ -17,31 +19,16 @@ async function run() {
const opts = program.opts();
const maxPage = Number.parseInt(opts.maxPage);

const rankByKeywords: [string, Promise<GoogleWebsiteRank | undefined>][] = [];
const rankByKeywords: [string, RankPromise][] = [];
for (const keyword of keywords) {
const prom = googleGetWebsiteRank(website, keyword, { maxPage });
const prom = utils.googleGetWebsiteRank(website, keyword, { maxPage });
rankByKeywords.push([keyword, prom]);
}

process.stdout.write(`Ranks for ${chalk.blueBright(website)} website:\n`);
for (const [keyword, prom] of rankByKeywords) {
const rank = await prom;
if (rank === undefined) {
process.stdout.write(`page ? rank ?`);
} else {
if (rank.page <= 0) {
process.stdout.write(`page ${chalk.greenBright(rank.page + 1)}`);
} else {
process.stdout.write(`page ${chalk.redBright(rank.page + 1)}`);
}

if (rank.page <= 0 && rank.rank <= 2) {
process.stdout.write(` rank ${chalk.greenBright(rank.rank + 1)}`);
} else {
process.stdout.write(` rank ${chalk.redBright(rank.rank + 1)}`);
}
}
process.stdout.write(` ${keyword}\n`);
const str = utils.formatKeywordRank(keyword, await prom);
process.stdout.write(`${str}\n`);
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from "@jest/globals";
import chalk from "chalk";
import { formatKeywordRank } from "./format";

describe("Format the rank of a keyword as a string", () => {
it("should format undefined rank correctly", () => {
const str = formatKeywordRank("a keyword", undefined);
expect(str).toBe("page ? rank ? a keyword");
});

it("should format green rank correctly", () => {
const str = formatKeywordRank("a keyword", { page: 0, rank: 2 });
expect(str).toBe(
`page ${chalk.greenBright(1)} rank ${chalk.greenBright(3)} a keyword`
);
});

it("should format green page and red rank correctly", () => {
const str = formatKeywordRank("a keyword", { page: 0, rank: 3 });
expect(str).toBe(
`page ${chalk.greenBright(1)} rank ${chalk.redBright(4)} a keyword`
);
});

it("should format red rank correctly", () => {
const str = formatKeywordRank("a keyword", { page: 1, rank: 0 });
expect(str).toBe(
`page ${chalk.redBright(2)} rank ${chalk.redBright(1)} a keyword`
);
});
});
27 changes: 27 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chalk from "chalk";
import { GoogleWebsiteRank } from "./google";

/**
* Formats the rank of a keyword as a string.
* @param keyword - The keyword string.
* @param rank - The rank of the keyword.
* @returns A formatted string. The rank will be displayed as a question mark if it is undefined.
*/
export function formatKeywordRank(
keyword: string,
rank?: GoogleWebsiteRank
): string {
if (rank === undefined) return `page ? rank ? ${keyword}`;

const pageStr =
rank.page <= 0
? `page ${chalk.greenBright(rank.page + 1)}`
: `page ${chalk.redBright(rank.page + 1)}`;

const rankStr =
rank.page <= 0 && rank.rank <= 2
? `rank ${chalk.greenBright(rank.rank + 1)}`
: `rank ${chalk.redBright(rank.rank + 1)}`;

return `${pageStr} ${rankStr} ${keyword}`;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { formatKeywordRank } from "./format";
export { googleGetWebsiteRank, GoogleWebsiteRank } from "./google";

0 comments on commit 5ff44da

Please sign in to comment.