Skip to content

Commit

Permalink
feat(core): introduce Direction type and directional computation func…
Browse files Browse the repository at this point in the history
…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
samavati committed Apr 13, 2024
1 parent 5e76a64 commit ad06a8d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/swiperia-core/src/lib/direction/Direction.type.ts
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 packages/swiperia-core/src/lib/direction/direction.spec.ts
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");
});
});
24 changes: 24 additions & 0 deletions packages/swiperia-core/src/lib/direction/direction.ts
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';
};
2 changes: 2 additions & 0 deletions packages/swiperia-core/src/lib/direction/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { Direction } from './Direction.type';
export { direction } from './direction';

0 comments on commit ad06a8d

Please sign in to comment.