Skip to content

Commit

Permalink
Add alt text for each game (#29)
Browse files Browse the repository at this point in the history
* Proposal: add alt text to describe the board

* Copyedit

* Clean up

* Update documentation

* Simplify

* Update index.js

* Clean up

* Clean up

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
katydecorah and actions-user authored Feb 20, 2022
1 parent b7b8557 commit 4a95b7f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
13 changes: 12 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8706,13 +8706,16 @@ function parseGame(title, body) {
const number = parseInt(split[1]);
const score = split[2][0] === "X" ? "X" : parseInt(split[2][0]);
const board = checkBoard(body);
const won = score !== "X";
const boardWords = board.map(emojiToWord);
const altText = createAltText(boardWords, won);
return {
number,
score,
won: score !== "X",
won,
board,
boardWords,
altText,
date: new Date().toISOString().slice(0, 10),
};
}
Expand Down Expand Up @@ -8741,6 +8744,14 @@ function emojiToWord(row) {
.replace(/🟧/g, "almost ")
.trim();
}
function createAltText(boardWords, won) {
const gameStatus = won ? "won" : "lost";
const gameRows = boardWords.length;
const gameGuesses = won
? ` in ${gameRows} guess${gameRows === 1 ? "" : "es"}`
: "";
return `The player ${gameStatus} the game${gameGuesses}.`;
}

;// CONCATENATED MODULE: external "fs/promises"
const promises_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("fs/promises");
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe("index", () => {
"won": true,
},
Object {
"altText": "The player won the game in 5 guesses.",
"board": Array [
"🟩⬛🟨⬛⬛",
"🟩🟩⬛⬛⬛",
Expand Down
36 changes: 35 additions & 1 deletion src/__tests__/parse-game.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import parseGame, { checkBoard, emojiToWord } from "../parse-game";
import parseGame, {
checkBoard,
emojiToWord,
createAltText,
} from "../parse-game";

jest.mock("@actions/core");

Expand All @@ -13,6 +17,7 @@ describe("parseGame", () => {
🟩🟩🟩🟩🟩`
)
).toEqual({
altText: "The player won the game in 3 guesses.",
board: ["🟩⬛⬛⬛⬛", "⬛⬛🟨🟩🟨", "🟩🟩🟩🟩🟩"],
boardWords: [
"yes no no no no",
Expand All @@ -35,6 +40,7 @@ describe("parseGame", () => {
🟩⬛⬛🟩⬛`
)
).toEqual({
altText: "The player lost the game.",
board: [
"⬛⬛⬛⬛🟨",
"⬛🟨⬛⬛⬛",
Expand Down Expand Up @@ -70,6 +76,8 @@ describe("parseGame", () => {
`
)
).toEqual({
altText: "The player won the game in 6 guesses.",

board: [
"🟩⬛🟨⬛🟨",
"🟩🟩⬛⬛⬛",
Expand Down Expand Up @@ -136,3 +144,29 @@ test("emojiToWord", () => {
"almost almost almost almost almost"
);
});

test("createAltText", () => {
expect(
createAltText(
["yes no no no no", "no no almost yes almost", "yes yes yes yes yes"],
true
)
).toMatchInlineSnapshot(`"The player won the game in 3 guesses."`);
expect(createAltText(["yes yes yes yes yes"], true)).toMatchInlineSnapshot(
`"The player won the game in 1 guess."`
);
expect(
createAltText(
[
"yes no no no no",
"no no almost yes almost",
"no no almost yes almost",
"no no almost yes almost",
"no no almost yes almost",
"no yes yes yes yes",
],

false
)
).toMatchInlineSnapshot(`"The player lost the game."`);
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type Game = {
boardWords: Board;
date: string;
won: boolean;
altText: string;
};

export type SquareEmoji = "⬜" | "⬛" | "🟨" | "🟩";
Expand Down
15 changes: 14 additions & 1 deletion src/parse-game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ export default function parseGame(title: string, body: string): Game {
const number = parseInt(split[1]);
const score = split[2][0] === "X" ? "X" : parseInt(split[2][0]);
const board = checkBoard(body);
const won = score !== "X";
const boardWords = board.map(emojiToWord);
const altText = createAltText(boardWords, won);
return {
number,
score,
won: score !== "X",
won,
board,
boardWords,
altText,
date: new Date().toISOString().slice(0, 10),
};
} catch (error) {
Expand Down Expand Up @@ -47,3 +50,13 @@ export function emojiToWord(row: string) {
.replace(/🟧/g, "almost ")
.trim();
}

export function createAltText(boardWords: string[], won: boolean) {
const gameStatus = won ? "won" : "lost";
const gameRows = boardWords.length;
const gameGuesses = won
? ` in ${gameRows} guess${gameRows === 1 ? "" : "es"}`
: "";

return `The player ${gameStatus} the game${gameGuesses}.`;
}

0 comments on commit 4a95b7f

Please sign in to comment.