-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪚 OmniGraph™ Add flattenTransactions utility (#58)
- Loading branch information
1 parent
ec8dfba
commit 09cb404
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './types' | ||
export * from './utils' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
) | ||
) | ||
}) | ||
}) | ||
}) |