rectangle-drawer 1.0.0 → 1.0.1

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/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # Rectangle Drawer Library – Usage Guide
2
+
3
+ *rectangle-drawer.mjs* is a utility module used to generate text-based rectangles using dots (.) and dashes (-).
4
+ It returns rectangles as strings, which can be printed directly to the terminal or styled using color libraries such as **starlight-color**.
5
+
6
+
7
+
8
+
9
+ ## Importing the Library
10
+
11
+ Use a relative import path when importing the module:
12
+
13
+ `
14
+ import {
15
+ solidRectangle,
16
+ hollowRectangle,
17
+ dashedRectangle,
18
+ dottedBorderRectangle,
19
+ checkerRectangle,
20
+ drawRectangle
21
+ } from "./rectangle-drawer.mjs";
22
+ `
23
+
24
+
25
+
26
+
27
+ ## Available Rectangle Types
28
+
29
+
30
+ - **solidRectangle(width, height)** – creates a fully filled rectangle using dots
31
+ - **hollowRectangle(width, height)** – creates a rectangle with dashed borders and dotted interior
32
+ - **dashedRectangle(width, height)** – creates a diagonal dashed pattern
33
+ - **dottedBorderRectangle(width, height)** – creates a dotted border with empty space inside
34
+ - **checkerRectangle(width, height)** – creates a checkerboard pattern using dots and dashes
35
+
36
+
37
+
38
+
39
+
40
+ ## Generic Rectangle Function
41
+
42
+ **drawRectangle(width, height, type)** allows selecting a rectangle style dynamically.
43
+
44
+ Supported *type* values:
45
+
46
+ - solid
47
+ - hollow
48
+ - dashed
49
+ - dotted
50
+ - checker
51
+
52
+
53
+
54
+
55
+
56
+ ## Example Usage
57
+
58
+ `
59
+ let rect = drawRectangle(10, 5, "hollow");
60
+ print(rect);
61
+ `
62
+
63
+
64
+
65
+
66
+ ## Validation Rules
67
+
68
+
69
+ - Width and height must be positive integers
70
+ - Hollow and border-based rectangles require width ≥ 2 and height ≥ 2
71
+ - An error is thrown for unknown rectangle types
72
+
73
+
74
+
75
+
76
+
77
+ ## Styling with Colors
78
+
79
+ The returned rectangle is a plain string. You can apply colors using **starlight-color**:
80
+
81
+ `
82
+ print(green(solidRectangle(8, 4)));
83
+ print(red(hollowRectangle(10, 5)));
84
+ `
85
+
86
+
87
+
88
+
89
+ ### Summary
90
+
91
+ This module is ideal for:
92
+
93
+ - CLI visual output
94
+ - ASCII art
95
+ - Teaching patterns and loops
96
+ - Terminal UI components
97
+
98
+
99
+ **rectangle-drawer** focuses on simplicity, flexibility, and clean terminal rendering.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "rectangle-drawer",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Draw rectangle shapes using dots and dashes",
5
5
  "type": "module",
6
- "main": "starlight-txt-to-pdf.mjs",
6
+ "main": "rectangle-drawer.mjs",
7
7
  "keywords": [
8
8
  "rectangle",
9
9
  "ascii",
@@ -1,116 +1,109 @@
1
- /**
2
- * Rectangle Drawer Library
3
- * Draw rectangles using dots (.) and dashes (-)
4
- */
5
-
6
- /* Solid filled rectangle */
7
1
  function solidRectangle(width, height) {
8
- validate(width, height);
2
+ validate(width, height)
9
3
 
10
- const rows = [];
4
+ const rows = []
11
5
  for (let y = 0; y < height; y++) {
12
- rows.push(".".repeat(width));
6
+ rows.push(".".repeat(width))
13
7
  }
14
- return rows.join("\n");
8
+ return rows.join("\n")
15
9
  }
16
10
 
17
- /* Hollow rectangle (border only) */
18
11
  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));
12
+ validate(width, height)
13
+ if (width < 2 || height < 2) throw new Error("Hollow rectangles require width and height >= 2")
27
14
 
15
+ const rows = []
16
+ rows.push("-".repeat(width))
28
17
  for (let y = 0; y < height - 2; y++) {
29
- rows.push("-" + ".".repeat(width - 2) + "-");
18
+ rows.push("-" + ".".repeat(width - 2) + "-")
30
19
  }
31
-
32
- rows.push("-".repeat(width));
33
- return rows.join("\n");
20
+ rows.push("-".repeat(width))
21
+ return rows.join("\n")
34
22
  }
35
23
 
36
- /* Dashed rectangle */
37
24
  function dashedRectangle(width, height) {
38
- validate(width, height);
25
+ validate(width, height)
39
26
 
40
- const rows = [];
27
+ const rows = []
41
28
  for (let y = 0; y < height; y++) {
42
- let row = "";
29
+ let row = ""
43
30
  for (let x = 0; x < width; x++) {
44
- row += (x + y) % 2 === 0 ? "-" : ".";
31
+ row += (x + y) % 2 === 0 ? "-" : "."
45
32
  }
46
- rows.push(row);
33
+ rows.push(row)
47
34
  }
48
- return rows.join("\n");
35
+ return rows.join("\n")
49
36
  }
50
37
 
51
- /* Border-only dotted rectangle */
52
38
  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));
39
+ validate(width, height)
40
+ if (width < 2 || height < 2) throw new Error("Border rectangles require width and height >= 2")
61
41
 
42
+ const rows = []
43
+ rows.push(".".repeat(width))
62
44
  for (let y = 0; y < height - 2; y++) {
63
- rows.push("." + " ".repeat(width - 2) + ".");
45
+ rows.push("." + " ".repeat(width - 2) + ".")
64
46
  }
65
-
66
- rows.push(".".repeat(width));
67
- return rows.join("\n");
47
+ rows.push(".".repeat(width))
48
+ return rows.join("\n")
68
49
  }
69
50
 
70
- /* Checkerboard rectangle */
71
51
  function checkerRectangle(width, height) {
72
- validate(width, height);
52
+ validate(width, height)
73
53
 
74
- const rows = [];
54
+ const rows = []
75
55
  for (let y = 0; y < height; y++) {
76
- let row = "";
56
+ let row = ""
77
57
  for (let x = 0; x < width; x++) {
78
- row += (x % 2 === 0) ? "." : "-";
58
+ row += (x % 2 === 0) ? "." : "-"
79
59
  }
80
- rows.push(row);
60
+ rows.push(row)
81
61
  }
82
- return rows.join("\n");
62
+ return rows.join("\n")
63
+ }
64
+
65
+ // Draw text inside a rectangle, automatically centered
66
+ function rectangleWithText(rect, text) {
67
+ const lines = rect.split("\n")
68
+ const mid = Math.floor(lines.length / 2)
69
+ const raw = lines[mid]
70
+ let pad = Math.floor((raw.length - text.length) / 2)
71
+ if (pad < 0) pad = 0
72
+ const newLine = raw.slice(0, pad) + text + raw.slice(pad + text.length)
73
+ lines[mid] = newLine
74
+ return lines.join("\n")
83
75
  }
84
76
 
85
- /* Generic rectangle dispatcher */
86
- function drawRectangle(width, height, type = "solid") {
77
+ function drawRectangle(width, height, type = "solid", text = "") {
78
+ let rect
87
79
  switch (type) {
88
80
  case "solid":
89
- return solidRectangle(width, height);
81
+ rect = solidRectangle(width, height)
82
+ break
90
83
  case "hollow":
91
- return hollowRectangle(width, height);
84
+ rect = hollowRectangle(width, height)
85
+ break
92
86
  case "dashed":
93
- return dashedRectangle(width, height);
87
+ rect = dashedRectangle(width, height)
88
+ break
94
89
  case "dotted":
95
- return dottedBorderRectangle(width, height);
90
+ rect = dottedBorderRectangle(width, height)
91
+ break
96
92
  case "checker":
97
- return checkerRectangle(width, height);
93
+ rect = checkerRectangle(width, height)
94
+ break
98
95
  default:
99
- throw new Error(`Unknown rectangle type: ${type}`);
96
+ throw new Error(`Unknown rectangle type: ${type}`)
100
97
  }
98
+ if (text) rect = rectangleWithText(rect, text)
99
+ return rect
101
100
  }
102
101
 
103
- /* Internal validation */
104
102
  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
- }
103
+ if (!Number.isInteger(width) || !Number.isInteger(height)) throw new Error("Width and height must be integers")
104
+ if (width <= 0 || height <= 0) throw new Error("Width and height must be positive")
111
105
  }
112
106
 
113
- /* Exports (ONLY at the end) */
114
107
  export {
115
108
  solidRectangle,
116
109
  hollowRectangle,
@@ -118,4 +111,4 @@ export {
118
111
  dottedBorderRectangle,
119
112
  checkerRectangle,
120
113
  drawRectangle
121
- };
114
+ }