// =============================================================================
// =ALEKSI - MAP-SOME
// =============================================================================
////
//// @group aleksi-maps
//// @author [Yoannis Jamar](https://yoannis.com)

@import "aleksi/lists/prepend";

// =map-some( $map, $test[, $args... ])
// -----------------------------------------------------------------------------
/// Checks whether at least one value in a map passes the given test function.
///
/// @param {map} $map - The map to analyze
/// @param {string} $test - The name of the test to run on each item
/// @param {arglist} $args - additional arguments for the test function
///
/// @return {bool} - Whether at least one value in $map passes $test
/// @throw {error} if `$map` is not a map
/// @throw {error} - If $test is not a string, or doesnt correspond to an existing function
///
/// @access public
/// @since 0.4.2

@function map-some( $map, $test, $args... )
{
  @if not is-map($map) {
    @return throw-error('map-some(): `$map` must be a map – was #{inspect($map)}');
  }

  @if type-of($test) != 'string' {
    @return throw-error('map-some(): `$test` must be a string – was #{inspect($test)}'); 
  }

  @if function-exists($test) == false {
    @return throw-error('map-some(): can\'t find test function #{inspect($test)}');
  }

  @if type-of($test) == 'string' {
    $test: get-function($test);
  }

  @each $key, $val in $map
  {
    // return true as soon as one value passes given test
    $test-args: prepend($args, $val);
    @if call($test, $test-args...) {
      @return true;
    }
  }

  // no value passed the test
  @return false;
}