-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): introduce Direction type and directional computation func…
…tion Added the Direction type to define the four cardinal directions in the swiperia-core package. Also implemented a function to compute the direction between two points, supported by unit tests to ensure accurate direction determination across various scenarios.
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Represents the four cardinal directions: right, left, up, and down. | ||
*/ | ||
export type Direction = 'right' | 'left' | 'up' | 'down'; |
34 changes: 34 additions & 0 deletions
34
packages/swiperia-core/src/lib/direction/direction.spec.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,34 @@ | ||
import { Point } from "../point/Point.type"; | ||
import { direction } from "./direction"; | ||
|
||
describe("direction", () => { | ||
it("should return the correct direction for positive coordinates", () => { | ||
const a: Point = [0, 0]; | ||
const b: Point = [3, 4]; | ||
expect(direction(a, b)).toEqual("down"); | ||
}); | ||
|
||
it("should return the correct direction for negative coordinates", () => { | ||
const a: Point = [-2, 3]; | ||
const b: Point = [1, -1]; | ||
expect(direction(a, b)).toEqual("up"); | ||
}); | ||
|
||
it("should return null for the same point", () => { | ||
const a: Point = [5, 5]; | ||
const b: Point = [5, 5]; | ||
expect(direction(a, b)).toBeNull(); | ||
}); | ||
|
||
it("should return the correct direction when x-coordinate is the same", () => { | ||
const a: Point = [0, 0]; | ||
const b: Point = [0, 5]; | ||
expect(direction(a, b)).toEqual("down"); | ||
}); | ||
|
||
it("should return the correct direction when y-coordinate is the same", () => { | ||
const a: Point = [0, 0]; | ||
const b: Point = [5, 0]; | ||
expect(direction(a, b)).toEqual("right"); | ||
}); | ||
}); |
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 type { Point } from '../point/Point.type'; | ||
import type { Direction } from './Direction.type'; | ||
|
||
/** | ||
* Determines the direction between two points. | ||
* | ||
* @param a - The starting point. | ||
* @param b - The ending point. | ||
* @returns The direction between the two points, or `null` if the points are the same. | ||
*/ | ||
export const direction = (a: Point, b: Point): Direction | null => { | ||
const [ax, ay] = a; | ||
const [bx, by] = b; | ||
const dx = bx - ax; | ||
const dy = by - ay; | ||
const absX = Math.abs(dx); | ||
const absY = Math.abs(dy); | ||
// If the points are the same, return null. | ||
if (absX === 0 && absY === 0) return null; | ||
if (absX > absY) { | ||
return dx > 0 ? 'right' : 'left'; | ||
} | ||
return dy > 0 ? 'down' : 'up'; | ||
}; |
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,2 @@ | ||
export type { Direction } from './Direction.type'; | ||
export { direction } from './direction'; |