-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
google-rank.ts
to utils/format.ts
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { googleGetWebsiteRank, GoogleWebsiteRank } from "./google"; | ||
export { formatKeywordRank } from "./format"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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` | ||
); | ||
}); | ||
}); |
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}`; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { formatKeywordRank } from "./format"; | ||
export { googleGetWebsiteRank, GoogleWebsiteRank } from "./google"; |