Skip to content

Commit

Permalink
feat: Add areSameEndpoint and vectorFromNodes coordinate helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 25, 2023
1 parent 594ff7f commit 98d2669
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/ua-utils/src/omnigraph/coordinates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OmniVector, OmniPoint } from './types'
import { OmniVector, OmniPoint, OmniNode } from './types'

/**
* Compares two points by value
Expand All @@ -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
*
Expand Down Expand Up @@ -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 })
28 changes: 27 additions & 1 deletion packages/ua-utils/test/omnigraph/coordinates.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 98d2669

Please sign in to comment.