-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #587 from tuanchauict/board
[WIP] Convert Board to TS
- Loading branch information
Showing
14 changed files
with
969 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { StringExt } from '$libs/string'; | ||
|
||
describe('StringExt', () => { | ||
test('trimMargin', () => { | ||
const input = ` | ||
| 1 | | ||
| 2 | | ||
| 3 | | ||
`; | ||
const expected = ` 1 \n 2 \n 3 `; | ||
expect(StringExt.trimMargin(input)).toStrictEqual(expected); | ||
}); | ||
|
||
test('trimMargin with custom prefix', () => { | ||
const input = ` | ||
# 1 | | ||
# 2 | | ||
# 3 | | ||
`; | ||
const expected = ` 1 \n 2 \n 3 `; | ||
expect(StringExt.trimMargin(input, '#')).toStrictEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export namespace StringExt { | ||
/** | ||
* Trims the margin of a string. | ||
* | ||
* The margin is defined by the first line that starts with a margin prefix. | ||
* @param input | ||
* @param marginPrefix The prefix of the margin. Default is `|`. | ||
* @param trimEnd The end of the line. Default is true. | ||
*/ | ||
export const trimMargin = ( | ||
input: string, | ||
marginPrefix: string = '|', | ||
trimEnd: boolean = true, | ||
): string => { | ||
const lines = input.split('\n'); | ||
|
||
const trimmedLines = lines.map((line) => { | ||
const index = line.indexOf(marginPrefix); | ||
if (index === -1) { | ||
return null; | ||
} | ||
const endExcluded = trimEnd ? line.length - 1 : line.length; | ||
return line.slice(index + marginPrefix.length, endExcluded); | ||
}); | ||
return trimmedLines.filter((line) => line !== null).join('\n'); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
monosketch-svelte/src/lib/mono/monobitmap/board/board.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { beforeEach, describe, expect, test } from 'vitest'; | ||
import { MonoBoard } from '$mono/monobitmap/board/board'; | ||
import { HighlightType, Pixel } from '$mono/monobitmap/board/pixel'; | ||
import { Point } from '$libs/graphics-geo/point'; | ||
import { Rect } from '$libs/graphics-geo/rect'; | ||
import { StringExt } from '$libs/string'; | ||
import trimMargin = StringExt.trimMargin; | ||
|
||
describe('MonoBoard', () => { | ||
let target: MonoBoard; | ||
|
||
beforeEach(() => { | ||
target = new MonoBoard(); | ||
target.clearAndSetWindow(Rect.byLTWH(-100, -100, 200, 200)); | ||
}); | ||
|
||
test('getSet', () => { | ||
const points = [-48, -32, -18, -16, 0, 16, 18, 32, 48].map( | ||
(value) => new Point(value, value), | ||
); | ||
|
||
points.forEach((point) => { | ||
expect(target.get(point.left, point.top)).toBe(Pixel.TRANSPARENT); | ||
}); | ||
|
||
const chars = '012345678'; | ||
chars.split('').forEach((char, index) => { | ||
target.setPoint(points[index], char, HighlightType.NO); | ||
}); | ||
chars.split('').forEach((char, index) => { | ||
expect(target.getPoint(points[index]).visualChar).toBe(char); | ||
}); | ||
|
||
expect(target.boardCount).toBe(7); | ||
}); | ||
|
||
test('fillRect', () => { | ||
target.fillRect(Rect.byLTWH(1, 1, 3, 3), 'A', HighlightType.NO); | ||
|
||
expect(target.toString()).toStrictEqual( | ||
trimMargin(` | ||
| | | ||
| AAA | | ||
| AAA | | ||
| AAA | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
`), | ||
); | ||
|
||
expect(target.boardCount).toBe(1); | ||
|
||
target.fillRect(Rect.byLTWH(-3, -3, 3, 3), 'B', HighlightType.NO); | ||
expect(target.toString()).toStrictEqual( | ||
trimMargin(` | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| BBB | | ||
| BBB | | ||
| BBB | | ||
| | | ||
| AAA | | ||
| AAA | | ||
| AAA | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
`), | ||
); | ||
|
||
expect(target.boardCount).toBe(2); | ||
|
||
target.fillRect(Rect.byLTWH(-1, 0, 3, 1), 'C', HighlightType.NO); | ||
expect(target.toString()).toStrictEqual( | ||
trimMargin(` | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| BBB | | ||
| BBB | | ||
| BBB | | ||
| CCC | | ||
| AAA | | ||
| AAA | | ||
| AAA | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
`), | ||
); | ||
|
||
expect(target.boardCount).toBe(3); | ||
}); | ||
}); |
Oops, something went wrong.