fix: fill with reference

This commit is contained in:
Shibo Lyu 2024-12-29 23:13:00 +08:00
parent 587e0558e9
commit 46a647441b
3 changed files with 14 additions and 14 deletions

View file

@ -16,17 +16,17 @@ export function createSection(
const offsetX = sx * boardConfig.sectionWidth; const offsetX = sx * boardConfig.sectionWidth;
const offsetY = sy * boardConfig.sectionHeight; const offsetY = sy * boardConfig.sectionHeight;
const ch: string[][] = Array(boardConfig.sectionHeight).fill( const ch: string[][] = Array(boardConfig.sectionHeight).fill([]).map(() =>
Array(boardConfig.sectionWidth).fill(boardConfig.defaultCh), Array(boardConfig.sectionWidth).fill(boardConfig.defaultCh)
); );
const color: string[][] = Array(boardConfig.sectionHeight).fill( const color: string[][] = Array(boardConfig.sectionHeight).fill([]).map(() =>
Array(boardConfig.sectionWidth).fill(boardConfig.defaultColor), Array(boardConfig.sectionWidth).fill(boardConfig.defaultColor)
); );
const bgColor: string[][] = Array(boardConfig.sectionHeight).fill( const bgColor: string[][] = Array(boardConfig.sectionHeight).fill([]).map(
Array(boardConfig.sectionWidth).fill(boardConfig.defaultBgColor), () => Array(boardConfig.sectionWidth).fill(boardConfig.defaultBgColor)
); );
const width: number[][] = Array(boardConfig.sectionHeight).fill( const width: number[][] = Array(boardConfig.sectionHeight).fill([]).map(() =>
Array(boardConfig.sectionWidth).fill(boardConfig.defaultWidth), Array(boardConfig.sectionWidth).fill(boardConfig.defaultWidth)
); );
return { offsetX, offsetY, ch, color, bgColor, width }; return { offsetX, offsetY, ch, color, bgColor, width };

View file

@ -56,8 +56,7 @@ Deno.test("board", async (t) => {
assertEquals(board.sections[0][0].ch[0][0], "A"); assertEquals(board.sections[0][0].ch[0][0], "A");
assertEquals(board.sections[0][1].ch[0][0], "B"); assertEquals(board.sections[0][1].ch[0][0], "B");
assertEquals(board.sections[1][0].ch[0][0], "C"); 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], ["D", "E", " ", " "]);
assertEquals(board.sections[1][1].ch[0][1], "E");
applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board); applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board);
applyChangeOnBoard({ x: 4, y: 2, 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][0].ch[1][0], "你");
assertEquals(board.sections[0][1].ch[2][0], "好"); assertEquals(board.sections[0][1].ch[2][0], "好");
assertEquals(board.sections[1][0].ch[1][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); 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", () => { await t.step("getSectionOnBoard: existing section", () => {
@ -79,10 +78,10 @@ Deno.test("board", async (t) => {
const section = getSectionOnBoard({ sx: 1, sy: 1 }, board, { const section = getSectionOnBoard({ sx: 1, sy: 1 }, board, {
readOnly: true, readOnly: true,
}); });
assertEquals(section.ch[0][0], "啊"); assertEquals(section.ch[0], ["D", "E", " ", " "]);
assertEquals(section.color[0][0], "F"); assertEquals(section.color[0][0], "F");
assertEquals(section.bgColor[0][0], "0"); 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", () => { await t.step("getSectionOnBoard: non-existing row", () => {

View file

@ -91,6 +91,7 @@ Deno.test("section", async (t) => {
applyChange({ x: 0, y: 0, ch: "t" }, section); applyChange({ x: 0, y: 0, ch: "t" }, section);
assertEquals(section.ch[0], ["t", " ", " ", " "]); assertEquals(section.ch[0], ["t", " ", " ", " "]);
assertEquals(section.ch[1], [" ", " ", " ", " "]);
assertEquals(section.width[0], [1, 1, 1, 1]); assertEquals(section.width[0], [1, 1, 1, 1]);
}); });