Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 13d1ddd

Browse files
committedOct 1, 2024·
refactor: rewrite with latest standard
1 parent b5ee1f6 commit 13d1ddd

File tree

2 files changed

+151
-17
lines changed

2 files changed

+151
-17
lines changed
 

‎Geometry/Rectangle.js

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,85 @@
11
/**
2-
* This class represents a rectangle and can calculate its perimeter and area
3-
* https://en.wikipedia.org/wiki/Rectangle
4-
* @constructor
5-
* @param {number} length - The length of the rectangle.
6-
* @param {number} breadth - The breadth of the rectangle.
2+
* Represents a rectangle and provides methods to calculate its perimeter and area.
3+
* @see {@link https://en.wikipedia.org/wiki/Rectangle|Rectangle}
74
*/
85
export default class Rectangle {
6+
/** @private */
7+
#length
8+
9+
/** @private */
10+
#breadth
11+
12+
/**
13+
* Creates a new Rectangle instance.
14+
* @constructor
15+
* @param {number} length - The length of the rectangle.
16+
* @param {number} breadth - The breadth of the rectangle.
17+
* @throws {Error} Will throw an error if length or breadth is not a positive number.
18+
*/
919
constructor(length, breadth) {
10-
this.length = length
11-
this.breadth = breadth
20+
this.#validateDimension(length, 'length')
21+
this.#validateDimension(breadth, 'breadth')
22+
this.#length = length
23+
this.#breadth = breadth
24+
}
25+
26+
/**
27+
* Validates that a dimension is a positive number.
28+
* @private
29+
* @param {number} value - The value to validate.
30+
* @param {string} name - The name of the dimension (for error reporting).
31+
* @throws {Error} Will throw an error if the value is not a positive number.
32+
*/
33+
#validateDimension(value, name) {
34+
if (typeof value !== 'number' || isNaN(value) || value <= 0) {
35+
throw new Error(`${name} must be a positive number.`)
36+
}
37+
}
38+
39+
/**
40+
* Calculates the perimeter of the rectangle.
41+
* @public
42+
* @returns {number} The perimeter of the rectangle.
43+
*/
44+
perimeter() {
45+
return 2 * (this.#length + this.#breadth)
46+
}
47+
48+
/**
49+
* Calculates the area of the rectangle.
50+
* @public
51+
* @returns {number} The area of the rectangle.
52+
*/
53+
area() {
54+
return this.#length * this.#breadth
55+
}
56+
57+
/**
58+
* Gets the length of the rectangle.
59+
* @public
60+
* @returns {number} The length of the rectangle.
61+
*/
62+
get length() {
63+
return this.#length
1264
}
1365

14-
perimeter = () => {
15-
return 2 * (this.length + this.breadth)
66+
/**
67+
* Gets the breadth of the rectangle.
68+
* @public
69+
* @returns {number} The breadth of the rectangle.
70+
*/
71+
get breadth() {
72+
return this.#breadth
1673
}
1774

18-
area = () => {
19-
return this.length * this.breadth
75+
/**
76+
* Returns a string representation of the rectangle.
77+
* @public
78+
* @returns {string} A string describing the rectangle.
79+
*/
80+
toString() {
81+
return `Rectangle: length = ${this.#length}, breadth = ${
82+
this.#breadth
83+
}, area = ${this.area()}, perimeter = ${this.perimeter()}`
2084
}
2185
}

‎Geometry/Test/Rectangle.test.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,81 @@
11
import Rectangle from '../Rectangle'
22

3-
const rectangle = new Rectangle(3, 4)
3+
describe('Rectangle', () => {
4+
describe('Constructor', () => {
5+
test('creates a rectangle with valid dimensions', () => {
6+
const rectangle = new Rectangle(5, 3)
7+
expect(rectangle).toBeInstanceOf(Rectangle)
8+
expect(rectangle.length).toBe(5)
9+
expect(rectangle.breadth).toBe(3)
10+
})
411

5-
test('The perimeter of rectangle with length equal to 3 and breadth equal to 4', () => {
6-
expect(parseFloat(rectangle.perimeter().toFixed(2))).toEqual(14.0)
7-
})
12+
test('throws an error if length is not a positive number', () => {
13+
expect(() => new Rectangle(-1, 3)).toThrow(
14+
'length must be a positive number.'
15+
)
16+
expect(() => new Rectangle(0, 3)).toThrow(
17+
'length must be a positive number.'
18+
)
19+
expect(() => new Rectangle('5', 3)).toThrow(
20+
'length must be a positive number.'
21+
)
22+
})
23+
24+
test('throws an error if breadth is not a positive number', () => {
25+
expect(() => new Rectangle(5, -1)).toThrow(
26+
'breadth must be a positive number.'
27+
)
28+
expect(() => new Rectangle(5, 0)).toThrow(
29+
'breadth must be a positive number.'
30+
)
31+
expect(() => new Rectangle(5, '3')).toThrow(
32+
'breadth must be a positive number.'
33+
)
34+
})
35+
})
36+
37+
describe('Perimeter Calculation', () => {
38+
test('calculates perimeter correctly', () => {
39+
const rectangle = new Rectangle(5, 3)
40+
expect(rectangle.perimeter()).toBe(16)
41+
})
42+
43+
test('calculates perimeter correctly for a square', () => {
44+
const square = new Rectangle(4, 4)
45+
expect(square.perimeter()).toBe(16)
46+
})
47+
})
48+
49+
describe('Area Calculation', () => {
50+
test('calculates area correctly', () => {
51+
const rectangle = new Rectangle(5, 3)
52+
expect(rectangle.area()).toBe(15)
53+
})
54+
55+
test('calculates area correctly for a square', () => {
56+
const square = new Rectangle(4, 4)
57+
expect(square.area()).toBe(16)
58+
})
59+
})
60+
61+
describe('Getters', () => {
62+
test('length getter returns correct value', () => {
63+
const rectangle = new Rectangle(5, 3)
64+
expect(rectangle.length).toBe(5)
65+
})
66+
67+
test('breadth getter returns correct value', () => {
68+
const rectangle = new Rectangle(5, 3)
69+
expect(rectangle.breadth).toBe(3)
70+
})
71+
})
872

9-
test('The area of rectangle with length equal to 3 and breadth equal to 4', () => {
10-
expect(parseFloat(rectangle.area().toFixed(2))).toEqual(12.0)
73+
describe('toString Method', () => {
74+
test('returns correct string representation', () => {
75+
const rectangle = new Rectangle(5, 3)
76+
expect(rectangle.toString()).toBe(
77+
'Rectangle: length = 5, breadth = 3, area = 15, perimeter = 16'
78+
)
79+
})
80+
})
1181
})

0 commit comments

Comments
 (0)
Please sign in to comment.