From 46a647441bbc95eb97f737e90fb62874429699d7 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Sun, 29 Dec 2024 23:13:00 +0800 Subject: [PATCH] fix: fill with reference --- logic/section.ts | 16 ++++++++-------- tests/board.test.ts | 11 +++++------ tests/section.test.ts | 1 + 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/logic/section.ts b/logic/section.ts index f2463ff..32256a6 100644 --- a/logic/section.ts +++ b/logic/section.ts @@ -16,17 +16,17 @@ export function createSection( const offsetX = sx * boardConfig.sectionWidth; const offsetY = sy * boardConfig.sectionHeight; - const ch: string[][] = Array(boardConfig.sectionHeight).fill( - Array(boardConfig.sectionWidth).fill(boardConfig.defaultCh), + const ch: string[][] = Array(boardConfig.sectionHeight).fill([]).map(() => + Array(boardConfig.sectionWidth).fill(boardConfig.defaultCh) ); - const color: string[][] = Array(boardConfig.sectionHeight).fill( - Array(boardConfig.sectionWidth).fill(boardConfig.defaultColor), + const color: string[][] = Array(boardConfig.sectionHeight).fill([]).map(() => + Array(boardConfig.sectionWidth).fill(boardConfig.defaultColor) ); - const bgColor: string[][] = Array(boardConfig.sectionHeight).fill( - Array(boardConfig.sectionWidth).fill(boardConfig.defaultBgColor), + const bgColor: string[][] = Array(boardConfig.sectionHeight).fill([]).map( + () => Array(boardConfig.sectionWidth).fill(boardConfig.defaultBgColor) ); - const width: number[][] = Array(boardConfig.sectionHeight).fill( - Array(boardConfig.sectionWidth).fill(boardConfig.defaultWidth), + const width: number[][] = Array(boardConfig.sectionHeight).fill([]).map(() => + Array(boardConfig.sectionWidth).fill(boardConfig.defaultWidth) ); return { offsetX, offsetY, ch, color, bgColor, width }; diff --git a/tests/board.test.ts b/tests/board.test.ts index 886a2f4..735bf32 100644 --- a/tests/board.test.ts +++ b/tests/board.test.ts @@ -56,8 +56,7 @@ Deno.test("board", async (t) => { assertEquals(board.sections[0][0].ch[0][0], "A"); assertEquals(board.sections[0][1].ch[0][0], "B"); assertEquals(board.sections[1][0].ch[0][0], "C"); - assertEquals(board.sections[1][1].ch[0][0], "D"); - assertEquals(board.sections[1][1].ch[0][1], "E"); + assertEquals(board.sections[1][1].ch[0], ["D", "E", " ", " "]); applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board); applyChangeOnBoard({ x: 4, y: 2, ch: "好" }, board); @@ -67,10 +66,10 @@ Deno.test("board", async (t) => { assertEquals(board.sections[0][0].ch[1][0], "你"); assertEquals(board.sections[0][1].ch[2][0], "好"); assertEquals(board.sections[1][0].ch[1][0], "嗎"); - assertEquals(board.sections[1][1].ch[2], ["嘛", "E", " ", " "]); + assertEquals(board.sections[1][1].ch[1], ["嘛", " ", " ", " "]); applyChangeOnBoard({ x: 5, y: 4, ch: "啊" }, board); - assertEquals(board.sections[1][1].ch[2], ["啊", "E", " ", " "]); + assertEquals(board.sections[1][1].ch[1], ["啊", " ", " ", " "]); }); await t.step("getSectionOnBoard: existing section", () => { @@ -79,10 +78,10 @@ Deno.test("board", async (t) => { const section = getSectionOnBoard({ sx: 1, sy: 1 }, board, { readOnly: true, }); - assertEquals(section.ch[0][0], "啊"); + assertEquals(section.ch[0], ["D", "E", " ", " "]); assertEquals(section.color[0][0], "F"); assertEquals(section.bgColor[0][0], "0"); - assertEquals(section.width[0][0], 2); + assertEquals(section.width[0], [1, 1, 1, 1]); }); await t.step("getSectionOnBoard: non-existing row", () => { diff --git a/tests/section.test.ts b/tests/section.test.ts index 07999f7..a70da5b 100644 --- a/tests/section.test.ts +++ b/tests/section.test.ts @@ -91,6 +91,7 @@ Deno.test("section", async (t) => { applyChange({ x: 0, y: 0, ch: "t" }, section); assertEquals(section.ch[0], ["t", " ", " ", " "]); + assertEquals(section.ch[1], [" ", " ", " ", " "]); assertEquals(section.width[0], [1, 1, 1, 1]); });