Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a test for solution-alternate.ts and fix its bugs #272

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions projects/arrays/text-processor/src/index-alternate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { describe, expect, test } from "@jest/globals";
import { expectType } from "tsd";

import * as index from "./index";
import * as alternate from "./solution-alternate";

const { alignTexts } = process.env.TEST_SOLUTIONS ? alternate : index;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI scripts were renaming solution_alternate to index_alternate, so I just renamed it to alternate.


describe(alignTexts, () => {
describe("types", () => {
test("function type", () => {
expectType<
(
texts: string[],
options: { align?: "left" | "middle" | "right"; width: number }
) => string[][]
>(alignTexts);
});

test("return type", () => {
expectType<string[][]>(alignTexts([], { width: 0 }));
});
});

const cases: [string[], index.AlignmentOptions, string[][]][] = [
[[""], { width: 0 }, [[""]]],
[["", ""], { width: 0 }, [[""], [""]]],
[[""], { width: 2 }, [[" "]]],
[["abc"], { width: 5 }, [["abc "]]],
[["abc def"], { width: 8 }, [["abc def "]]],
[
["ab de", "abc def"],
{ width: 4 },
[
["ab ", "de "],
["abc ", "def "],
],
],
[
["ab de", "abc def", "abcd ef"],
{ width: 4 },
[
["ab ", "de "],
["abc ", "def "],
["abcd", "ef "],
],
],
[["ab de", "abc def"], { width: 5 }, [["ab de"], ["abc ", "def "]]],
[["abc def", "abc def"], { width: 8 }, [["abc def "], ["abc def "]]],
[
["abc def", "abc def ghi"],
{ width: 3 },
[
["abc", "def"],
["abc", "def", "ghi"],
],
],
[
["abc def", "abc def ghi"],
{ width: 5 },
[
["abc ", "def "],
["abc ", "def ", "ghi "],
],
],
[["abc def", "abcdefghi"], { width: 8 }, [["abc def "], ["abcdefghi"]]],
[[""], { align: "left", width: 0 }, [[""]]],
[[""], { align: "left", width: 1 }, [[" "]]],
[[""], { align: "left", width: 2 }, [[" "]]],
[["abc"], { align: "left", width: 3 }, [["abc"]]],
[["abc"], { align: "left", width: 4 }, [["abc "]]],
[["abc"], { align: "left", width: 5 }, [["abc "]]],
[["abc def"], { align: "left", width: 7 }, [["abc def"]]],
[["abc def"], { align: "left", width: 8 }, [["abc def "]]],
[["abc def"], { align: "left", width: 9 }, [["abc def "]]],
[["abc def"], { align: "left", width: 10 }, [["abc def "]]],
[["abc def"], { align: "left", width: 11 }, [["abc def "]]],
[
["a", "ab", "abc", "abcd"],
{ align: "middle", width: 4 },
[[" a "], [" ab "], ["abc "], ["abcd"]],
],
[[""], { align: "middle", width: 0 }, [[""]]],
[[""], { align: "middle", width: 1 }, [[" "]]],
[[""], { align: "middle", width: 2 }, [[" "]]],
[["abc"], { align: "middle", width: 3 }, [["abc"]]],
[["abc"], { align: "middle", width: 4 }, [["abc "]]],
[["abc"], { align: "middle", width: 5 }, [[" abc "]]],
[["abc def"], { align: "middle", width: 7 }, [["abc def"]]],
[["abc def"], { align: "middle", width: 8 }, [["abc def "]]],
[["abc def"], { align: "middle", width: 9 }, [[" abc def "]]],
[["abc def"], { align: "middle", width: 10 }, [[" abc def "]]],
[["abc def"], { align: "middle", width: 11 }, [[" abc def "]]],
[
["abc def", "abc def ghi"],
{ align: "middle", width: 5 },
[
[" abc ", " def "],
[" abc ", " def ", " ghi "],
],
],
[[""], { align: "right", width: 0 }, [[""]]],
[[""], { align: "right", width: 1 }, [[" "]]],
[[""], { align: "right", width: 2 }, [[" "]]],
[["abc"], { align: "right", width: 3 }, [["abc"]]],
[["abc"], { align: "right", width: 4 }, [[" abc"]]],
[["abc"], { align: "right", width: 5 }, [[" abc"]]],
[["abc def"], { align: "right", width: 7 }, [["abc def"]]],
[["abc def"], { align: "right", width: 8 }, [[" abc def"]]],
[["abc def"], { align: "right", width: 9 }, [[" abc def"]]],
[["abc def"], { align: "right", width: 10 }, [[" abc def"]]],
[["abc def"], { align: "right", width: 11 }, [[" abc def"]]],
[
["abc def", "abc def ghi"],
{ align: "right", width: 5 },
[
[" abc", " def"],
[" abc", " def", " ghi"],
],
],
];

test.each(cases)("%j %o", (lines, options, aligned) => {
expect(alignTexts(lines, options)).toEqual(aligned);
});
});
10 changes: 5 additions & 5 deletions projects/arrays/text-processor/src/solution-alternate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ const aligners = {

return remainingSpaces
? [
Array.from({ length: Math.floor(remainingSpaces / 2) }, () => " "),
" ".repeat(Math.floor(remainingSpaces / 2)),
line,
Array.from({ length: Math.ceil(remainingSpaces / 2) }, () => " "),
].join(" ")
" ".repeat(Math.ceil(remainingSpaces / 2)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

].join("")
: line;
},
right: (line: string, width: number) => line.padEnd(width),
right: (line: string, width: number) => line.padStart(width),
};

function alignLines(
lines: string[],
{ align = "left", width }: AlignmentOptions
) {
return lines.map(aligners[align], width);
return lines.map((line) => aligners[align](line, width));
}
Loading