rectangle-drawer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -0
- package/rectangle-drawer.mjs +121 -0
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rectangle-drawer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Draw rectangle shapes using dots and dashes",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "starlight-txt-to-pdf.mjs",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"rectangle",
|
|
9
|
+
"ascii",
|
|
10
|
+
"drawing",
|
|
11
|
+
"shapes"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Macedon"
|
|
15
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rectangle Drawer Library
|
|
3
|
+
* Draw rectangles using dots (.) and dashes (-)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* Solid filled rectangle */
|
|
7
|
+
function solidRectangle(width, height) {
|
|
8
|
+
validate(width, height);
|
|
9
|
+
|
|
10
|
+
const rows = [];
|
|
11
|
+
for (let y = 0; y < height; y++) {
|
|
12
|
+
rows.push(".".repeat(width));
|
|
13
|
+
}
|
|
14
|
+
return rows.join("\n");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* Hollow rectangle (border only) */
|
|
18
|
+
function hollowRectangle(width, height) {
|
|
19
|
+
validate(width, height);
|
|
20
|
+
|
|
21
|
+
if (width < 2 || height < 2) {
|
|
22
|
+
throw new Error("Hollow rectangles require width and height >= 2");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const rows = [];
|
|
26
|
+
rows.push("-".repeat(width));
|
|
27
|
+
|
|
28
|
+
for (let y = 0; y < height - 2; y++) {
|
|
29
|
+
rows.push("-" + ".".repeat(width - 2) + "-");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
rows.push("-".repeat(width));
|
|
33
|
+
return rows.join("\n");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* Dashed rectangle */
|
|
37
|
+
function dashedRectangle(width, height) {
|
|
38
|
+
validate(width, height);
|
|
39
|
+
|
|
40
|
+
const rows = [];
|
|
41
|
+
for (let y = 0; y < height; y++) {
|
|
42
|
+
let row = "";
|
|
43
|
+
for (let x = 0; x < width; x++) {
|
|
44
|
+
row += (x + y) % 2 === 0 ? "-" : ".";
|
|
45
|
+
}
|
|
46
|
+
rows.push(row);
|
|
47
|
+
}
|
|
48
|
+
return rows.join("\n");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* Border-only dotted rectangle */
|
|
52
|
+
function dottedBorderRectangle(width, height) {
|
|
53
|
+
validate(width, height);
|
|
54
|
+
|
|
55
|
+
if (width < 2 || height < 2) {
|
|
56
|
+
throw new Error("Border rectangles require width and height >= 2");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const rows = [];
|
|
60
|
+
rows.push(".".repeat(width));
|
|
61
|
+
|
|
62
|
+
for (let y = 0; y < height - 2; y++) {
|
|
63
|
+
rows.push("." + " ".repeat(width - 2) + ".");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
rows.push(".".repeat(width));
|
|
67
|
+
return rows.join("\n");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* Checkerboard rectangle */
|
|
71
|
+
function checkerRectangle(width, height) {
|
|
72
|
+
validate(width, height);
|
|
73
|
+
|
|
74
|
+
const rows = [];
|
|
75
|
+
for (let y = 0; y < height; y++) {
|
|
76
|
+
let row = "";
|
|
77
|
+
for (let x = 0; x < width; x++) {
|
|
78
|
+
row += (x % 2 === 0) ? "." : "-";
|
|
79
|
+
}
|
|
80
|
+
rows.push(row);
|
|
81
|
+
}
|
|
82
|
+
return rows.join("\n");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* Generic rectangle dispatcher */
|
|
86
|
+
function drawRectangle(width, height, type = "solid") {
|
|
87
|
+
switch (type) {
|
|
88
|
+
case "solid":
|
|
89
|
+
return solidRectangle(width, height);
|
|
90
|
+
case "hollow":
|
|
91
|
+
return hollowRectangle(width, height);
|
|
92
|
+
case "dashed":
|
|
93
|
+
return dashedRectangle(width, height);
|
|
94
|
+
case "dotted":
|
|
95
|
+
return dottedBorderRectangle(width, height);
|
|
96
|
+
case "checker":
|
|
97
|
+
return checkerRectangle(width, height);
|
|
98
|
+
default:
|
|
99
|
+
throw new Error(`Unknown rectangle type: ${type}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* Internal validation */
|
|
104
|
+
function validate(width, height) {
|
|
105
|
+
if (!Number.isInteger(width) || !Number.isInteger(height)) {
|
|
106
|
+
throw new Error("Width and height must be integers");
|
|
107
|
+
}
|
|
108
|
+
if (width <= 0 || height <= 0) {
|
|
109
|
+
throw new Error("Width and height must be positive");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/* Exports (ONLY at the end) */
|
|
114
|
+
export {
|
|
115
|
+
solidRectangle,
|
|
116
|
+
hollowRectangle,
|
|
117
|
+
dashedRectangle,
|
|
118
|
+
dottedBorderRectangle,
|
|
119
|
+
checkerRectangle,
|
|
120
|
+
drawRectangle
|
|
121
|
+
};
|