# All files / src/utils filterUnspent.js

100% Statements 13/13
100% Branches 7/7
100% Functions 6/6
100% Lines 11/11

    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

    13x
    12x
    9x

    13x
    7x
    1x

    13x
    5x

    12x

    7x

    3x

     /**
     * 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 === undefined) return true;
      return utxo.address.toLowerCase() === address.toLowerCase();
    };

    const hasColor = (utxo, color) => {
      if (color === undefined) return true;
      return utxo.color === 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;
        });
    };