Skip to content

Commit

Permalink
🪚 Small stuff (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Nov 25, 2023
1 parent 4bc34b7 commit 437a1a3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 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 })
16 changes: 12 additions & 4 deletions packages/ua-utils/src/omnigraph/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export const OmniVectorSchema: z.ZodSchema<OmniVector, z.ZodTypeDef, unknown> =
to: OmniPointSchema,
})

export const EmptyOmniNodeSchema = z.object({
point: OmniPointSchema,
config: z.unknown(),
})

export const EmptyOmniEdgeSchema = z.object({
vector: OmniVectorSchema,
config: z.unknown(),
})

/**
* Factory for OmniNode schemas
*
Expand All @@ -28,8 +38,7 @@ export const OmniVectorSchema: z.ZodSchema<OmniVector, z.ZodTypeDef, unknown> =
export const createOmniNodeSchema = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef, unknown> =>
z.object({
point: OmniPointSchema,
EmptyOmniNodeSchema.extend({
config: configSchema,
}) as z.ZodSchema<OmniNode<TConfig>, z.ZodTypeDef, unknown>

Expand All @@ -43,7 +52,6 @@ export const createOmniNodeSchema = <TConfig = unknown>(
export const createOmniEdgeSchema = <TConfig = unknown>(
configSchema: z.ZodSchema<TConfig, z.ZodTypeDef, unknown>
): z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef, unknown> =>
z.object({
vector: OmniVectorSchema,
EmptyOmniEdgeSchema.extend({
config: configSchema,
}) as z.ZodSchema<OmniEdge<TConfig>, z.ZodTypeDef, unknown>
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 437a1a3

Please sign in to comment.