Skip to content

Commit

Permalink
🪚 OmniGraph™ Add flattenTransactions utility (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Dec 5, 2023
1 parent ec8dfba commit 09cb404
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/utils/src/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './types'
export * from './utils'
7 changes: 7 additions & 0 deletions packages/utils/src/transactions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OmniTransaction } from './types'

const isNonNullable = <T>(value: T | null | undefined): value is T => value != null

export const flattenTransactions = (
transations: (OmniTransaction | OmniTransaction[] | null | undefined)[]
): OmniTransaction[] => transations.filter(isNonNullable).flat()
53 changes: 53 additions & 0 deletions packages/utils/test/transactions/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fc from 'fast-check'
import { pointArbitrary } from '@layerzerolabs/test-utils'
import { OmniTransaction, flattenTransactions } from '@/transactions'

describe('transactions/utils', () => {
const nullableArbitrary = fc.constantFrom(null, undefined)
const transactionArbitrary: fc.Arbitrary<OmniTransaction> = fc.record({
point: pointArbitrary,
data: fc.hexaString(),
})

describe('flattenTransactions', () => {
it('should return an empty array when called with an empty array', () => {
expect(flattenTransactions([])).toEqual([])
})

it('should return an empty array when called with an array of nulls and undefineds', () => {
fc.assert(
fc.property(fc.array(nullableArbitrary), (transactions) => {
expect(flattenTransactions(transactions)).toEqual([])
})
)
})

it('should remove any nulls or undefineds', () => {
fc.assert(
fc.property(fc.array(fc.oneof(transactionArbitrary, nullableArbitrary)), (transactions) => {
const flattened = flattenTransactions(transactions)

for (const transaction of flattened) {
expect(transaction).not.toBeNull()
expect(transaction).not.toBeUndefined()
}
})
)
})

it('should flatten any arrays', () => {
fc.assert(
fc.property(
fc.array(fc.oneof(transactionArbitrary, nullableArbitrary, fc.array(transactionArbitrary))),
(transactions) => {
const flattened = flattenTransactions(transactions)

for (const transaction of flattened) {
expect(transaction).not.toBeInstanceOf(Array)
}
}
)
)
})
})
})

0 comments on commit 09cb404

Please sign in to comment.