All files / src/services/util sortAddresses.ts

58.33% Statements 7/12
40% Branches 4/10
100% Functions 2/2
54.54% Lines 6/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                  43x 1638x 478x 138x     340x 340x                          
import { GroupedAddress } from '@cardano-sdk/key-management';
 
/**
 * Sorts an array of addresses by their primary index and, if available, by the
 * index of their stakeKeyDerivationPath.
 *
 * @param addresses - The array of addresses to sort.
 * @returns A new sorted array of addresses.
 */
export const sortAddresses = (addresses: GroupedAddress[]): GroupedAddress[] =>
  [...addresses].sort((a, b) => {
    if (a.index !== b.index) {
      return a.index - b.index;
    }
 
    if (a.stakeKeyDerivationPath && b.stakeKeyDerivationPath) {
      return a.stakeKeyDerivationPath.index - b.stakeKeyDerivationPath.index;
    }
 
    Iif (a.stakeKeyDerivationPath && !b.stakeKeyDerivationPath) {
      return -1;
    }
 
    Iif (!a.stakeKeyDerivationPath && b.stakeKeyDerivationPath) {
      return 1;
    }
 
    return 0;
  });