Skip to content

Commit f283b61

Browse files
committed
(#53) Refactored Location class into functions
1 parent 4f0dd01 commit f283b61

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/location.class.spec.ts renamed to lib/location.function.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Location } from "./location.class";
1+
import { centerOf, randomPointIn } from "./location.function";
22
import { Point } from "./point.class";
33
import { Region } from "./region.class";
44

@@ -7,12 +7,12 @@ describe("Location", () => {
77
const expected = new Point(2, 2);
88
const testRegion = new Region(0, 0, 4, 4);
99

10-
expect(Location.centerOf(testRegion)).toEqual(expected);
10+
expect(centerOf(testRegion)).resolves.toEqual(expected);
1111
});
1212

13-
it("should return a random point inside of an area.", () => {
13+
it("should return a random point inside of an area.", async () => {
1414
const testRegion = new Region(100, 20, 50, 35);
15-
const result = Location.randomPointIn(testRegion);
15+
const result = await randomPointIn(testRegion);
1616
expect(result.x).toBeGreaterThanOrEqual(testRegion.left);
1717
expect(result.x).toBeLessThanOrEqual(testRegion.left + testRegion.width);
1818
expect(result.y).toBeGreaterThanOrEqual(testRegion.top);

lib/location.function.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Point } from "./point.class";
2+
import { Region } from "./region.class";
3+
4+
export const centerOf = async (target: Region | Promise<Region>): Promise<Point> => {
5+
const targetRegion = await target;
6+
const x = Math.floor(targetRegion.left + targetRegion.width / 2);
7+
const y = Math.floor(targetRegion.top + targetRegion.height / 2);
8+
9+
return new Point(x, y);
10+
};
11+
12+
export const randomPointIn = async (target: Region | Promise<Region>): Promise<Point> => {
13+
const targetRegion = await target;
14+
const x = Math.floor(targetRegion.left + Math.random() * targetRegion.width);
15+
const y = Math.floor(targetRegion.top + Math.random() * targetRegion.height);
16+
17+
return new Point(x, y);
18+
};

0 commit comments

Comments
 (0)