# All files / src/utils filterUnspent.js

100% Statements 13/13
100% Branches 7/7
100% Functions 6/6
100% Lines 11/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
    31
    32
    33
    34

    18x
    33x
    28x

    18x
    15x
    7x

    18x
    11x

    33x

    15x

    5x

     /**
     * Copyright (c) 2018-present, Leap DAO (leapdao.org)
     *
     * This source code is licensed under the Mozilla Public License Version 2.0
     * found in the LICENSE file in the root directory of this source tree.
     */

    const hasAddress = (utxo, address) => {
      if (!address) return true;
      return utxo.address.toLowerCase() === address.toLowerCase();
    };

    const hasColor = (utxo, color) => {
      if (color === undefined) return true;
      return Number(utxo.color) === Number(color);
    };

    module.exports = function filterUnspent(unspent, address, color) {
      return Object.keys(unspent)
        .filter(
          k =>
            unspent[k] &&
            hasAddress(unspent[k], address) &&
            hasColor(unspent[k], color)
        )
        .map(k => ({
          outpoint: k,
          output: unspent[k],
        }))
        .sort((a, b) => {
          return a.output.value - b.output.value;
        });
    };