diff --git a/packages/ua-utils/src/omnigraph/coordinates.ts b/packages/ua-utils/src/omnigraph/coordinates.ts index 946efcb0b..acd7ecb68 100644 --- a/packages/ua-utils/src/omnigraph/coordinates.ts +++ b/packages/ua-utils/src/omnigraph/coordinates.ts @@ -1,4 +1,4 @@ -import { OmniVector, OmniPoint } from './types' +import { OmniVector, OmniPoint, OmniNode } from './types' /** * Compares two points by value @@ -10,6 +10,16 @@ import { OmniVector, OmniPoint } from './types' */ export const arePointsEqual = (a: OmniPoint, b: OmniPoint): boolean => a.address === b.address && a.eid === b.eid +/** + * Checks if two points are on the same endpoint + * + * @param a `OmniPoint` + * @param b `OmniPoint` + * + * @returns `true` if the vector point to the same point in omniverse + */ +export const areSameEndpoint = (a: OmniPoint, b: OmniPoint): boolean => a.eid === b.eid + /** * Compares two vectors by value * @@ -40,3 +50,12 @@ export const serializePoint = ({ address, eid }: OmniPoint): string => `${eid}|$ * @returns `string` */ export const serializeVector = ({ from, to }: OmniVector): string => `${serializePoint(from)} → ${serializePoint(to)}` + +/** + * Helper function to quickly convert a pair of nodes to a vector + * + * @param a `OmniNode` + * @param b `OmniNode` + * @returns `OmniVector` + */ +export const vectorFromNodes = (a: OmniNode, b: OmniNode): OmniVector => ({ from: a.point, to: b.point }) diff --git a/packages/ua-utils/test/omnigraph/coordinates.test.ts b/packages/ua-utils/test/omnigraph/coordinates.test.ts index e345569a1..6b057574b 100644 --- a/packages/ua-utils/test/omnigraph/coordinates.test.ts +++ b/packages/ua-utils/test/omnigraph/coordinates.test.ts @@ -1,5 +1,11 @@ import fc from 'fast-check' -import { areVectorsEqual, arePointsEqual, serializePoint, serializeVector } from '@/omnigraph/coordinates' +import { + areVectorsEqual, + arePointsEqual, + serializePoint, + serializeVector, + areSameEndpoint, +} from '@/omnigraph/coordinates' import { pointArbitrary, addressArbitrary, endpointArbitrary, vectorArbitrary } from '../__utils__/arbitraries' describe('omnigraph/vector', () => { @@ -79,6 +85,26 @@ describe('omnigraph/vector', () => { ) }) }) + + describe('areSameEndpoint', () => { + it('should return true if the eids match', () => { + fc.assert( + fc.property(pointArbitrary, pointArbitrary, (pointA, pointB) => { + expect(areSameEndpoint(pointA, { ...pointB, eid: pointA.eid })).toBeTruthy() + }) + ) + }) + + it('should return false if the eids differ', () => { + fc.assert( + fc.property(pointArbitrary, pointArbitrary, (pointA, pointB) => { + fc.pre(pointA.eid !== pointB.eid) + + expect(areSameEndpoint(pointA, pointB)).toBeFalsy() + }) + ) + }) + }) }) describe('serialization', () => {