Skip to content

Commit 6b5dbd3

Browse files
committed
refactor!: move generic equals methods to util package
Moved generic utilities for equality check to util package. BREAKING CHANGE: moved strictEquals, sameArrayItems, shallowArrayEquals to util package
1 parent e581f51 commit 6b5dbd3

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

packages/util/src/equals.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import isEqual from 'lodash/isEqual';
22

33
export const deepEquals = <T>(a: T, b: T) => isEqual(a, b);
4+
5+
export const strictEquals = <T>(a: T, b: T) => a === b;
6+
7+
export const sameArrayItems = <T>(arrayA: T[], arrayB: T[], itemEquals: (a: T, b: T) => boolean) =>
8+
arrayA.length === arrayB.length && arrayA.every((a) => arrayB.some((b) => itemEquals(a, b)));
9+
10+
export const shallowArrayEquals = <T>(a: T[], b: T[]) => sameArrayItems(a, b, strictEquals);

packages/util/test/equals.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepEquals } from '../src';
1+
import { deepEquals, sameArrayItems, shallowArrayEquals, strictEquals } from '../src';
22

33
describe('equals', () => {
44
test('deepEquals', () => {
@@ -7,4 +7,24 @@ describe('equals', () => {
77
expect(deepEquals([{ prop: 'prop' }], [{ prop: 'prop' }])).toBe(true);
88
expect(deepEquals([{ prop: 'prop' }], [{ prop: 'prop2' }])).toBe(false);
99
});
10+
11+
test('strictEquals', () => {
12+
expect(strictEquals('1', 1 as unknown as string)).toBe(false);
13+
expect(strictEquals('1', '1')).toBe(true);
14+
});
15+
16+
test('sameArrayItems', () => {
17+
expect(sameArrayItems([], [], strictEquals)).toBe(true);
18+
expect(sameArrayItems(['a'], ['a', 'b'], strictEquals)).toBe(false);
19+
expect(sameArrayItems(['a', 'b'], ['a', 'b'], strictEquals)).toBe(true);
20+
expect(sameArrayItems(['a', 'b'], ['b', 'a'], strictEquals)).toBe(true);
21+
});
22+
23+
test('shallowArrayEquals', () => {
24+
expect(shallowArrayEquals([], [])).toBe(true);
25+
const a = { prop: 'prop' };
26+
const b = { prop: 'prop' };
27+
expect(shallowArrayEquals([a], [b])).toBe(false);
28+
expect(shallowArrayEquals([a], [a])).toBe(true);
29+
});
1030
});

packages/wallet/src/services/DelegationTracker/DelegationDistributionTracker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BigIntMath, Percent, calcPercentages } from '@cardano-sdk/util';
1+
import { BigIntMath, Percent, calcPercentages, sameArrayItems } from '@cardano-sdk/util';
22
import { Cardano } from '@cardano-sdk/core';
33
import { DelegatedStake } from '../types';
44
import { DelegationTrackerProps } from './DelegationTracker';
@@ -14,7 +14,7 @@ import {
1414
withLatestFrom
1515
} from 'rxjs';
1616
import { createUtxoBalanceByAddressTracker } from '../BalanceTracker';
17-
import { delegatedStakeEquals, sameArrayItems } from '../util';
17+
import { delegatedStakeEquals } from '../util';
1818
import _groupBy from 'lodash/groupBy';
1919
import _map from 'lodash/map';
2020

packages/wallet/src/services/DelegationTracker/RewardAccounts.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable unicorn/no-nested-ternary */
2-
import { BigIntMath, deepEquals, isNotNil } from '@cardano-sdk/util';
2+
import { BigIntMath, deepEquals, isNotNil, shallowArrayEquals } from '@cardano-sdk/util';
33
import { Cardano, RewardsProvider, StakePoolProvider } from '@cardano-sdk/core';
44
import {
55
EMPTY,
@@ -28,7 +28,7 @@ import {
2828
import { RetryBackoffConfig } from 'backoff-rxjs';
2929
import { TrackedStakePoolProvider } from '../ProviderTracker';
3030
import { TxWithEpoch } from './types';
31-
import { coldObservableProvider, shallowArrayEquals } from '../util';
31+
import { coldObservableProvider } from '../';
3232
import findLast from 'lodash/findLast';
3333
import isEqual from 'lodash/isEqual';
3434
import uniq from 'lodash/uniq';

packages/wallet/src/services/HandlesTracker.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
withLatestFrom
1313
} from 'rxjs';
1414
import { Logger } from 'ts-log';
15-
import { deepEquals, isNotNil } from '@cardano-sdk/util';
16-
import { sameArrayItems, strictEquals } from './util';
15+
import { deepEquals, isNotNil, sameArrayItems, strictEquals } from '@cardano-sdk/util';
1716
import uniqBy from 'lodash/uniqBy';
1817

1918
export interface HandlesTrackerProps {

packages/wallet/src/services/util/coldObservableProvider.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InvalidStringError } from '@cardano-sdk/util';
1+
import { InvalidStringError, strictEquals } from '@cardano-sdk/util';
22
import {
33
NEVER,
44
Observable,
@@ -15,7 +15,6 @@ import {
1515
throwError
1616
} from 'rxjs';
1717
import { RetryBackoffConfig, retryBackoff } from 'backoff-rxjs';
18-
import { strictEquals } from './equals';
1918

2019
export interface ColdObservableProviderProps<T> {
2120
provider: () => Promise<T>;

packages/wallet/src/services/util/equals.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { Cardano, EpochInfo, EraSummary } from '@cardano-sdk/core';
22
import { DelegatedStake } from '../types';
33
import { GroupedAddress } from '@cardano-sdk/key-management';
4-
5-
export const strictEquals = <T>(a: T, b: T) => a === b;
6-
7-
export const sameArrayItems = <T>(arrayA: T[], arrayB: T[], itemEquals: (a: T, b: T) => boolean) =>
8-
arrayA.length === arrayB.length && arrayA.every((a) => arrayB.some((b) => itemEquals(a, b)));
9-
10-
export const shallowArrayEquals = <T>(a: T[], b: T[]) => sameArrayItems(a, b, strictEquals);
4+
import { sameArrayItems } from '@cardano-sdk/util';
115

126
export const tipEquals = (a: Cardano.Tip, b: Cardano.Tip) => a.hash === b.hash;
137

packages/wallet/test/services/util/equals.test.ts

-23
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import {
66
epochInfoEquals,
77
eraSummariesEquals,
88
groupedAddressesEquals,
9-
sameArrayItems,
10-
shallowArrayEquals,
11-
strictEquals,
129
tipEquals,
1310
transactionsEquals,
1411
txEquals,
@@ -21,26 +18,6 @@ describe('equals', () => {
2118
const txId1 = Cardano.TransactionId('4123d70f66414cc921f6ffc29a899aafc7137a99a0fd453d6b200863ef5702d6');
2219
const txId2 = Cardano.TransactionId('01d7366549986d83edeea262e97b68eca3430d3bb052ed1c37d2202fd5458872');
2320

24-
test('strictEquals', () => {
25-
expect(strictEquals('1', 1 as unknown as string)).toBe(false);
26-
expect(strictEquals('1', '1')).toBe(true);
27-
});
28-
29-
test('sameArrayItems', () => {
30-
expect(sameArrayItems([], [], strictEquals)).toBe(true);
31-
expect(sameArrayItems(['a'], ['a', 'b'], strictEquals)).toBe(false);
32-
expect(sameArrayItems(['a', 'b'], ['a', 'b'], strictEquals)).toBe(true);
33-
expect(sameArrayItems(['a', 'b'], ['b', 'a'], strictEquals)).toBe(true);
34-
});
35-
36-
test('shallowArrayEquals', () => {
37-
expect(shallowArrayEquals([], [])).toBe(true);
38-
const a = { prop: 'prop' };
39-
const b = { prop: 'prop' };
40-
expect(shallowArrayEquals([a], [b])).toBe(false);
41-
expect(shallowArrayEquals([a], [a])).toBe(true);
42-
});
43-
4421
test('txEquals', () => {
4522
expect(txEquals({ id: txId1 } as Cardano.HydratedTx, { id: txId2 } as Cardano.HydratedTx)).toBe(false);
4623
expect(txEquals({ id: txId1 } as Cardano.HydratedTx, { id: txId1 } as Cardano.HydratedTx)).toBe(true);

0 commit comments

Comments
 (0)